思路一——向MAVEN中传递参数,通过参数实现插件的不同行为
操作
<plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <delete file="${project.build.outputDirectory}/log4j.properties"/> <copy file="src/main/resources/${environment.type}/config.properties" tofile="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/${environment.type}/log4j.properties" tofile="${project.build.outputDirectory}/log4j.properties"/> </tasks> </configuration> </execution> </executions> </plugin>antrun都是向根目录拷贝配置文件,但是不同的环境需要拷贝的配置文件存在不同的配置目录下,文件名称相同。
通过mvn -Denvironment.type=dist传递参数来实现拷贝不同的配置文件
缺点
不传递参数的时候会报错
思路二——使用PROFILE
操作
<profiles> <profile> <id>dist</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <delete file="${project.build.outputDirectory}/log4j.properties"/> <copy file="src/main/resources/dist/config.properties" tofile="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/dist/log4j.properties" tofile="${project.build.outputDirectory}/log4j.properties"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>product</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <delete file="${project.build.outputDirectory}/config.properties"/> <delete file="${project.build.outputDirectory}/log4j.properties"/> <copy file="src/main/resources/product/config.properties" tofile="${project.build.outputDirectory}/config.properties"/> <copy file="src/main/resources/product/log4j.properties" tofile="${project.build.outputDirectory}/log4j.properties"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles>mav -Pdist,这样就会激活dist prifle中配置的插件
优点
没有指定的环境的时候,也就没有-P参数的时候,PROFILE中的内容不会激活
不同环境可以有不同的行为
几乎支持所有的默认配置项
缺点
不明显