Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

《Maven实战》学习笔记(5)--不同环境配置

来源: hanqunfeng 分享至:

一般来说,项目的配置文件都在resources下面,默认maven会从下面路径查找资源文件,这是超级pom中的配置
<resources>
<resource>
<directory>F:\maven_workspace\workspace\piaoyi\service\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>F:\maven_workspace\workspace\piaoyi\service\src\test\resources</directory>
</testResource>
</testResources>

可以将不同环境的配置,通过属性定义的方式配置在profiles中(pom),然后在项目配置文件中使用这些mavne属性,在构建不同的环境时,通过激活不同的frofile来决定使用哪个环境的配置,比如(该profiles不能被子项目继承,只对当前pom项目有效,所以推荐配置在settings中):
<profiles>
<profile>
<id>dev</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.url>jdbc:mysql://localhost:3306/test</db.url>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<db.driver>com.mysql.jdbc.Driver</db.driver>
<db.url>jdbc:mysql://192.168.0.10:3306/test</db.url>
</properties>
</profile>
</profiles>

同时,需要在pom中的修改资源配置(定义在聚会项目中即可,可以被继承),如下,增加在pom中:
<build>
<resources>
<resource>
<directory>${project.basedir}\src\main\resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}\src\test\resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>


然后在执行构建时,执行如下命令
mvn clean install -Pdev
激活该profile

也可以在settings.xml中进行配置,以使某个profile一直处于激活状态
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>dev</activeProfile>

</activeProfiles>

以上配置,只能替换classes下的资源文件,如果希望替换webApp下的文件内容,需要增加如下插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
</resource>
</webResources>
</configuration>
</plugin>


习惯ant的可以使用如下方式进行资源文件替换
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<!-- 执行的动作 -->
<execution>
<id>ant-copy</id>
<phase>prepare-package</phase> <!-- 绑定到prepare-package 阶段-->
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks> <!-- 调用ant的copy任务执行资源文件替换 -->
<echo message="prepare-release run" />
<copy todir="${project.build.directory}/classes/config" overwrite="true">
<fileset dir="${project.basedir}/config/release" />
</copy>
<echo message="prepare-release completed!" />
</tasks>
</configuration>
</execution>
可以定义多个动作
</executions>
</plugin>





Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史