Maven灵活构建

Maven内置了三大特性:属性、Profile和资源过滤来支持构建的灵活性。

Maven属性

事实上有六种类型的Maven属性:
  • 内置属性:主要有两个常用内置属性——${basedir}表示项目根目录,即包含pom.xml文件的目录;${version}表示项目版本。
  • POM属性:pom中对应元素的值。例如${project.artifactId}对应了<project><artifactId>元素的值。具体有哪些POM属性可以用,可以查看本页末的附件——超级POM
  • 自定义属性:在pom中<properties>元素下自定义的Maven属性。例如
    
        
        
    1. <project>
    2. <properties>
    3. <my.prop>hello </my.prop>
    4. </properties>
    5. </project>

  • Settings属性:与POM属性同理。如${settings.localRepository}指向用户本地仓库的地址。
  • Java系统属性:所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录。可以通过命令行mvn help:system查看所有的Java系统属性
  • 环境变量属性:所有环境变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。也可以通过命令行mvn help:system查看所有环境变量。

资源过滤

默认情况下,Maven属性只有在POM中才会被解析。资源过滤就是指让Maven属性在资源文件(src/main/resources、src/test/resources)中也能被解析。
在POM中添加下面的配置便可以开启资源过滤

  
  
  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>${project.basedir}/src/main/resources </directory>
  5. <filtering>true </filtering>
  6. </resource>
  7. </resources>
  8. <testResources>
  9. <testResource>
  10. <directory>${project.basedir}/src/test/resources </directory>
  11. <filtering>true </filtering>
  12. </testResource>
  13. </testResources>
  14. </build>


从上面的配置中可以看出,我们其实可以配置多个主资源目录和多个测试资源目录。

Maven除了可以对主资源目录、测试资源目录过滤外,还能对Web项目的资源目录(如css、js目录)进行过滤。这时需要对maven-war-plugin插件进行配置

  
  
  1. <plugin>
  2. <groupId>org.apache.maven.plugins </groupId>
  3. <artifactId>maven-war-plugin </artifactId>
  4. <version>2.1-beta-1 </version>
  5. <configuration>
  6. <webResources>
  7. <resource>
  8. <filtering>true </filtering>
  9. <directory>src/main/webapp </directory>
  10. <includes>
  11. <include>**/*.css </include>
  12. <include>**/*.js </include>
  13. </includes>
  14. </resource>
  15. </webResources>
  16. </configuration>
  17. </plugin>


Maven Profile

每个Profile可以看作是POM的一部分配置,我们可以根据不同的环境应用不同的Profile,从而达到不同环境使用不同的POM配置的目的。

profile可以声明在以下这三个文件中:
  • pom.xml:很显然,这里声明的profile只对当前项目有效
  • 用户settings.xml:.m2/settings.xml中的profile对该用户的Maven项目有效
  • 全局settings.xml:conf/settings.xml,对本机上所有Maven项目有效
非常值得注意的一点是,profile在pom.xml中可声明的元素在settings.xml中可声明的元素是不一样的:
  • profile在pom.xml中可声明的元素:
    
        
        
    1. <project>
    2. <repositories> </repositories>
    3. <pluginRepositories> </pluginRepositories>
    4. <distributionManagement> </distributionManagement>
    5. <dependencies> </dependencies>
    6. <dependencyManagement> </dependencyManagement>
    7. <modules> </modules>
    8. <properties> </properties>
    9. <reporting> </reporting>
    10. <build>
    11. <plugins> </plugins>
    12. <defaultGoal> </defaultGoal>
    13. <resources> </resources>
    14. <testResources> </testResources>
    15. <finalName> </finalName>
    16. </build>
    17. </project>

  • profile在settings.xml中可声明的元素:
    
        
        
    1. <project>
    2. <repositories> </repositories>
    3. <pluginRepositories> </pluginRepositories>
    4. <properties> </properties>
    5. </project>


激活Profile

有多种激活Profile的方式:
  1. 命令行方式激活,如有两个profile id为devx和devy的profile:
    mvn clean install  -Pdevx,devy

  2. settings文件显式激活
    
        
        
    1. <settings>
    2. ...
    3. <activeProfiles>
    4. <activeProfile>devx </activeProfile>
    5. <activeProfile>devy </activeProfile>
    6. </activeProfiles>
    7. ...
    8. </settings>

  3. 系统属性激活,用户可以配置当某系统属性存在或其值等于期望值时激活profile,如:
    
        
        
    1. <profiles>
    2. <profile>
    3. <activation>
    4. <property>
    5. <name>actProp </name>
    6. <value>x </value>
    7. </property>
    8. </activation>
    9. </profile>
    10. </profiles>
    不要忘了,可以在命令行声明系统属性。如:
    mvn clean install -DactProp=x
    这其实也是一种从命令行激活profile的方法,而且多个profile完全可以使用同一个系统属性来激活。别忘了,系统属性可以通过mvn help:system来查看
  4. 操作系统环境激活,如
    
        
        
    1. <profiles>
    2. <profile>
    3. <activation>
    4. <os>
    5. <name>Windows XP </name>
    6. <family>Windows </family>
    7. <arch>x86 </arch>
    8. <version>5.1.2600 </version>
    9. </os>
    10. </activation>
    11. </profile>
    12. </profiles>
    这里的family值包括Window、UNIX和Mac等,而其他几项对应系统属性的os.name、os.arch、os.version
  5. 文件存在与否激活,Maven能根据项目中某个文件存在与否来决定是否激活profile
    
        
        
    1. <profiles>
    2. <profile>
    3. <activation>
    4. <file>
    5. <missing>x.properties </missing>
    6. <exists>y.properties </exists>
    7. </file>
    8. </activation>
    9. </profile>
    10. </profiles>

Notice:插件maven-help-plugin提供了一个目标帮助用户了解当前激活的profile:
mvn help:active-profiles
另外还有一个目标来列出当前所有的profile:
mvn help:all-profiles


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值