明确的配置文件激活
在以下示例中,我们将附加maven-antrun-plugin:run目标到测试阶段。这将使我们能够回显不同配置文件的文本消息。我们将使用pom.xml定义不同的配置文件,并使用maven命令在命令控制台中激活配置文件。
假设,我们在C:\mav\project文件夹中创建了以下pom.xml。
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.projectgroup</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Using env.test.properties</echo>
<copy file="src/main/resources/env.test.properties"
tofile="${project.build.outputDirectory}
/env.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
现在打开命令控制台,转到包含pom.xml的文件夹并执行以下mvn命令。使用-P选项将概要文件名称作为参数传递。
C:\MVN\project>mvn test -Ptest
Maven将开始处理并显示测试构建概要文件的结果。
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------
[INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 3 resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\MVN\project\src\test\resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: C:\MVN\project\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[echo] Using env.test.properties
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Sun Jul 08 14:55:41 IST 2012
[INFO] Final Memory: 8M/64M
[INFO] ------------------------------------------------------------------
现在作为练习,您可以执行以下步骤
- 向pom.xml的profiles元素添加另一个profile元素(复制现有profile元素并将其粘贴到profile元素结束的位置)。
- 将此配置文件元素的ID从test更新为normal。
- 更新任务部分以回显env.properties,并将env.properties复制到目标目录。
- 再次重复上述三个步骤,将id更新为prov,然后将env.prod.properties的task部分更新。
- 就这样。现在,您已经准备好三个构建配置文件(normal/test/prod)。
现在打开命令控制台,转到包含pom.xml的文件夹并执行以下mvn命令。使用-P选项将概要文件名称作为参数传递。
C:\MVN\project>mvn test -Pnormal
C:\MVN\project>mvn test -Pprod
检查构建的输出以查看差异。