通过maven可以选择使用的环境配置。
可以通过 mvn clean install -DskipTests -Denv=test命令来指定所使用的环境。这里指定的是test环境。
然而,test环境又是在哪里定义的呢?
通过profiles来注册maven中可能使用到的环境。
通过profiles中注册的profile,maven在打包时,可以通过-Denv选项指定其中的Id。
示例如下:
mvn clean install -DskipTests -Denv=test
<profiles> <profile> <id>test</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <echo message="copy log4j2.xml and test.properties to ${project.build.directory}/classes/" /> <copy file="src/main/config/log4j2.xml" tofile="${project.build.directory}/classes/log4j2.xml" overwrite="true" /> <copy file="src/main/config/test.properties" tofile="${project.build.directory}/classes/application.properties" overwrite="true"/> </target> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>pre</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <target name="pre-copying"> <echo message="copy log4j2.xml and pre.properties to /classes/" /> <copy file="src/main/config/log4j2.xml" tofile="${project.build.directory}/classes/log4j2.xml" overwrite="true" /> <copy file="src/main/config/pre.properties" tofile="${project.build.directory}/classes/application.properties" overwrite="true"/> </target> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.7</version> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> <configuration> <target name="prod-copying"> <echo message="copy log4j2.xml and prod.properties to /classes/" /> <copy file="src/main/config/log4j2.xml" tofile="${project.build.directory}/classes/log4j2.xml" overwrite="true" /> <copy file="src/main/config/prod.properties" tofile="${project.build.directory}/classes/application.properties" overwrite="true"/> </target> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>