profile允许为特殊的移植或者目的,自定义构建,一般位于pom.xml或者settings.xml中(maven3中已经不再支持profiles.xml这种外部profile)。
profile标签几乎可以覆盖所有的元素,在设置好profile之后,我们需要将其激活。激活的方式有以下几种:
一是命令行激活:mvn install -Pprofile -id; 二是在profile中设置<activation/><activeByDefault/>(如果默认为true则自动激活,如果默认false则根据其它条件是否符合来判断是否激活);
三是在settings.xml中根据<activeProfiles/><activeProfile/>profile-id中进行统一激活(要求这个profile-id必须是在settings.xml中所定义)。
下面根据实际的场景来对profile的应用做进一步的说明。
场景一:开发环境与生产环境的数据库连接设置不同,如何用profile实现?
settings.xml与pom.xml配合实现,设置如下:
<profiles> <profile> <id>environment</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment.type>pro</environment.type> <property1>2</property1> </properties> </profile> ...
<profiles>
<profile>
<id>devlopment</id>
<activation>
<property>
<name>environment.type</name>
<value>dev</value>
</property>
</activation>
<properties>
<jdbc.driverClassName>oracle.Driver</jdbc.driverClassName>
<jdbc.url>oracle.URL</jdbc.url>
<jdbc.name>oracle</jdbc.name>
<jdbc.password>oracle</jdbc.password>
</properties>
</profile>
<profile>
<id>production</id>
<activation>
<property>
<name>environment.type</name>
<value>pro</value>
</property>
</activation>
<properties>
<jdbc.driverClassName>mysql.Driver</jdbc.driverClassName>
<jdbc.url>mysql.URL</jdbc.url>
<jdbc.name>mysql</jdbc.name>
<jdbc.password>mysql</jdbc.password>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
这样可以很容易地转换环境变量的配置,要注意的是build一定要加,这是利用了Maven的Resource Filter功能,从而找到<directory/>下的文件,替代${}占位符的内容。如果此时需要修改dev或者pro,最方便的办法就是直接用命令行覆盖environment.type的值,虽然settings.xml默认的值是pro但如果用:mvn install -Denvironment.type=dev来编译打包,那么最终的值即为dev这一套。另外,由于settings.xml中的环境变量值可以覆盖pom.xml中的值,所以最终确定是以前者为准。例如我们在settings.xml中设置:
<profile>
<id>product</id>
<activation>
<property>
<name>environment.type</name>
<value>pro</value>
</property>
</activation>
<properties>
<jdbc.password>djksjdkldj</jdbc.password>
<property1>80</property1>
</properties>
</profile>
那么,当maven命令指定-Denvironment.type=pro时,它的密码和property1的值会以这里的值为准,pom.xml中的值作废,以此,也可以加强密码保护,不在实际的工程而是在服务器的settings.xml中存放密码。
场景二:在打jar包时,增加分类符,windows平台下打jar包之前带‘win’,linux平台下打jar包带‘lin’
添加profile:
<profile> <id>win</id> <activation> <os> <family>windows</family> </os> </activation> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>win</classifier> </configuration> </plugin> </plugins> </build> </profile> <profile> <id>linux</id> <activation> <os> <family>unix</family> </os> </activation> <build> <plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>lin</classifier> </configuration> </plugin> </plugins> </build> </profile>
本机编译打包,最终jar包名称为:TestMaven-0.0.1-SNAPSHOT-win.jar。上述平台分类器其实是在pom.xml中完成的,但如果需要每个项目都完整这个分类,很容易想到把这两个profile放到settings.xml中去,但遗憾的是,这么做并不能生效。因为settings.xml可以控制确定的值只有三类,properties,repositories以及pluginRepositories,其余的值都要在pom中定义。所以我们可以将要修改的值放到properties中去,而在pom中使用通配符来解决上述问题:
<plugins> <plugin> <artifactId>maven-jar-plugin</artifactId> <configuration> <classifier>${envClassifier}</classifier> </configuration> </plugin> </plugins> </build>
settings.xml中控制envClassifier的值
<profile> <id>win</id> <activation> <os> <family>windows</family> </os> </activation> <properties> <envClassifier>wins</envClassifier> </properties> </profile> <profile> <id>linux</id> <activation> <os> <family>unix</family> </os> </activation> <properties> <envClassifier>linx</envClassifier> </properties> </profile>
即可达到效果。
在前文提到了可以用settings中的属性值来覆盖pom中属性值来达到设置假密码,这就引出来一个属性覆盖的问题,到底是哪些profile在生效呢?
这可以用命令mvn help:active-profiles来查看:
E:\Users\BruceChan\workspace\dev01\TestMaven>mvn help:active-profiles [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.changjiang.test:TestMaven:jar:0.0.1-SNAPSHOT [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 67, column 12 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building TestMaven 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:2.2:active-profiles (default-cli) @ TestMaven --- [INFO] Active Profiles for Project 'com.changjiang.test:TestMaven:jar:0.0.1-SNAPSHOT': The following profiles are active: - jdk17 (source: external) - central-repos (source: external) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.691s [INFO] Finished at: Thu Aug 25 15:12:19 CST 2016 [INFO] Final Memory: 8M/244M [INFO] ------------------------------------------------------------------------ E:\Users\BruceChan\workspace\dev01\TestMaven>
mvn help:all-profiles可以查看所有的profile以及他们所归属的文件。