本例中,我们添加了一个名为production的profile,它覆盖了Maven Compiler插件的默认配置,现在仔细看一下这个profile的语法。
pom.xml中的profiles元素,它包含了一个或者多个profile元素。由于profile覆盖了pom.xml中的默认设置,profiles通常是pom.xml中的最后一个元素。
每个profile必须要有一个id元素。这个id元素包含的名字将在命令行调用profile时被用到。我们可以通过传给Maven一个-P<profile_id>参数来调用profile。
一个profile元素可以包含很多其它元素,只要这些元素可以出现在POM XML文档的project元素下面。本例中,我们覆盖了Compiler插件的行为,因此必须覆盖插件配置,该配置通常位于一个build和一个plugins元素下面。
我们覆盖了Maven Compiler插件的配置。确保通过production profile产生的字节码不会包含调试信息,并且字节码会被编译器优化。
要使用production profile来运行mvn install,你需要在命令行传入-Pproduction参数。要验证production profile覆盖了默认的Compiler插件配置,可以像这样以开启调试输出(-X) 的方式运行Maven。
~/examples/profile $ mvn clean install -Pproduction -X ... (omitting debugging output) ... [DEBUG] Configuring mojo 'o.a.m.plugins:maven-compiler-plugin:2.0.2:testCompile' [DEBUG] (f) basedir = ~\examples\profile [DEBUG] (f) buildDirectory = ~\examples\profile\target ... [DEBUG] (f) compilerId = javac [DEBUG] (f) debug = false [DEBUG] (f) failOnError = true [DEBUG] (f) fork = false [DEBUG] (f) optimize = true [DEBUG] (f) outputDirectory = \ ~\svnw\sonatype\examples\profile\target\test-classes [DEBUG] (f) outputFileName = simple-1.0-SNAPSHOT [DEBUG] (f) showDeprecation = false [DEBUG] (f) showWarnings = false [DEBUG] (f) staleMillis = 0 [DEBUG] (f) verbose = false [DEBUG] -- end configuration -- ... (omitting debugging output) ...Maven的调试输出体现了production profile下Compiler插件的配置。可以看到,debug被设置成false,optimize设置成true。
Example 11.2. Profile中允许出现的元素
<project> <profiles> <profile> <build> <defaultGoal>...</defaultGoal> <finalName>...</finalName> <resources>...</resources> <testResources>...</testResources> <plugins>...</plugins> </build> <reporting>...</reporting> <modules>...</modules> <dependencies>...</dependencies> <dependencyManagement>...</dependencyManagement> <distributionManagement>...</distributionManagement> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <properties>...</properties> </profile> </profiles> </project>