一般来说,项目的配置文件都在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>
分享到:
相关推荐
maven-project-info-reports-plugin-2.2.jar
maven-archetype-quickstart-1.1.jar包下载
Maven-apache-maven-3.8.1-Repository-配置好的-Window10 解压配置一下环境变量就可以用
maven-deploy-plugin-2.7.jar
解决tomcat8-maven-plugin-3.0-r1655215.jar阿里云同有的问题。放到路径org\apache\tomcat\maven\tomcat8-maven-plugin\3.0-r1655215\就可以了
maven-site-plugin-3.3.jar
maven-jar-plugin-3.1.1.jar
maven-resources-plugin-2.4.1.jar
maven-archetype-webapp-1.0.jar,下载,如果有需要的同志可以自己下载,。
maven-deploy-plugin-2.8.2.jar
maven-maven-plugin-1.4-sources.jar
maven-project-info-reports-plugin-2.1.jar
maven-plugin-parameter-documenter-2.0.jar
maven资源 apache-maven-3.3.9-bin.zip
maven-shared-utils-3.2.1.jar
maven-notice-plugin-1.0.1.jar
解决Unable to create project from archetype [org.apache.maven.archetypes:maven-archetype-quickstart:1.1] 1. 下载maven-archetype-quickstart-1.1.jar 文件地址: 2.cmd窗口执行mvn install:install-file -...
maven-scm-client-1.4.jar
Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project
新建maven项目失败,不能下载maven-archetype-webapp-1.0.jar包