八. 灵活的构建

Maven为了支持构建的灵活性,适应开发环境、测试环境和生产环境内置了三大特性,即属性、Profile和资源过滤

 

1.Maven的属性 <properties>

 

Maven有6类属性

(1)内置属性:主要有两个常用内置属性

    ${basedir}:表示项目根目录,即包含pom.xml文件的目录;

    ${version}:表示项目版本

(2)POM属性:该类属性引用POM文件中对应元素的值,它们中的一些属性的默认值都是定义在超级POM中,常用的POM属性包括:

    ${project.build.sourceDirectory}:项目的主源码路径,默认为 src/main/java/ ;

    ${project.build.testSourceDirectory}:项目的测试源码目录,默认为 src/test/java/ ;

    ${project.build.directory}:项目构建输出目录,默认为 target/ ;

    ${project.outputDirectory}:主代码编译输出目录,默认为 target/classes/ ;

    ${project.testOutputDirectory}:测试代码编译输出目录,默认为 target/test-classes/ ;

    ${project.groupId}:项目的groupId ;

    ${project.artifactId}:项目的artifactId ;

    ${project.version}:项目的version,与${version}等价 ;

    ${project.build.finalName}:项目的打包输出文件名称,默认为${project.artifactId}-${project.version} 。

(3)自定义属性

<project>  
    <properties>  
        <my.prop>hello</my.prop>  
    </properties>  
</project>

然后在POM中其它地方使用${my.prop}的时候会被替换成hello

 

(4)Setting属性:与POM属性同理,用户使用settings,开头的属性引用settings.xml文件中XML元素的值。常用的有:${settings.localRepository}指向用户本地仓库的地址。

(5)Java系统属性:所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录。可以通过命令行mvn help:system查看所有的Java系统属性。

(6)环境变量属性:所有环境变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。也可以通过命令行mvn help:system查看所有环境变量。

 

2.资源过滤

 

(1)Maven属性默认情况下只有在pom.xml文件中才能使用。如果想根据不同的环境自动配置properties文件中也使用Maven的属性(即,将Maven Profile中配置替换到资源中${xxx}上)。首先需要开启资源过滤

开启资源过滤的方法如下(通过maven-resources-plugin实现):

 

<build>  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <filtering>true</filtering>  
        </resource>  
    </resources>  
    <testResources>  
        <testResource>  
            <directory>src/test/resources</directory>  
            <filtering>true</filtering>  
        </testResource>  
    </testResources>  
</build>  

    filtering的值true/false对应开启/关闭

 

 

(2)还可以开启web项目的资源过滤,方法如下:

 

<plugin>  
    <groupId>org.apache.maven.plugins</groupId>  
    <artifactId>maven-war-plugin</artifactId>  
    <version>2.3</version>  
    <configuration>  
        <webResources>  
            <resource>  
                <filtering>true</filtering>  
                <directory>src/main/webapp</directory>  
                <includes>  
                    <include>**/*.css</include>  
                    <include>**/*.js</include>  
                </includes>  
            </resource>  
        </webResources>  
    </configuration>  
</plugin> 

 

 

(3)自定义基于Maven Profile的属性

为了替换开启资源过滤的目录下进行替换,需要自定义Profile的属性

Profile属性与properties标签定义属性类似,但是需要一个id,定义样式如下:

 

<project>
	<profiles>
		<profile>
			<id>myId</id>
			<properties>
				<my.property>myPropertyValue</my.property>
			</properties>
		</profile>
	</profiles>
</project>

 

 

(4)运行命令

通过-P参数表示在命令行激活一个profile,可用逗号分隔激活多个运行命令如下:

mvn clean compile -PmyId,myId2

 

3.Maven Profile

 

为了能够构建在各个环境下方便移植,Maven引入了profile的概念。

首先需要开启资源过滤

profile能够在构建的时候修改POM的一个子集,或者添加额外的配置元素。注意的一点是,profile在pom.xml中可声明的元素在settings.xml中可声明的元素是不一样的:

 

profile在pom.xml中可声明的元素

<project>  
    <repositories></repositories>  
    <pluginRepositories></pluginRepositories>  
    <distributionManagement></distributionManagement>  
    <dependencies></dependencies>  
    <dependencyManagement></dependencyManagement>  
    <modules></modules>  
    <properties></properties>  
    <reporting></reporting>  
    <build>  
        <plugins></plugins>  
        <defaultGoal></defaultGoal>  
        <resources></resources>  
        <testResources></testResources>  
        <finalName></finalName>  
    </build>  
</project>  

 

profile在settings.xml中可声明的元素:

<project>  
    <repositories></repositories>  
    <pluginRepositories></pluginRepositories>  
    <properties></properties>  
</project>  

 

激活profile

 

(1)命令行激活

使用mvn命令行参数-P加上profile的id来激活profile,多个id直接以逗号分隔

mvn clean install  -Pdevx,devy  

 

(2)settings文件显式激活

如果用户希望某个profile默认一直处于激活状态,就可以配置setting.xml文件的activeProfiles元素,表示配置的profile对于所有项目都是处于激活状态

<settings>  
    ...  
    <activeProfiles>  
        <activeProfile>devx</activeProfile>  
        <activeProfile>devy</activeProfile>  
    </activeProfiles>  
    ...  
</settings>  

 

(3)系统属性激活,用户可以配置当某系统属性存在或其值等于期望值时激活profile

<profiles>  
    <profile>  
        <activation>  
            <property>  
                <name>actProp</name>  
                <!-- 当存在value时,表示某系统属性存在且其值等于期望值时激活 -->
                <value>x</value>  
            </property>  
        </activation>  
    </profile>  
</profiles>  

 不要忘了,可以在命令行声明系统属性。如:

mvn clean install -DactProp=x 

 这其实也是一种从命令行激活profile的方法,而且多个profile完全可以使用同一个系统属性来激活。别忘了,系统属性可以通过mvn help:system来查看

 

(4)操作系统环境激活

<profiles>  
    <profile>  
        <activation>  
            <os>  
                <name>Windows XP</name>  
                <family>Windows</family>  
                <arch>x86</arch>  
                <version>5.1.2600</version>  
            </os>  
        </activation>  
    </profile>  
</profiles>  

 这里的family值包括Window、UNIX和Mac等,而其他几项对应系统属性的os.name、os.arch、os.version

 

(5)文件存在与否激活,Maven能根据项目中某个文件存在与否来决定是否激活profile

<profiles>  
    <profile>  
        <activation>  
            <file>  
                <missing>x.properties</missing>  
                <exists>y.properties</exists>  
            </file>  
        </activation>  
    </profile>  
</profiles>  

 

(6)默认激活

<!--默认启用的是dev环境配置-->
<profile>
   <id>dev</id>
   <properties>
	  <env>dev</env>
   </properties>
   <activation>
	  <activeByDefault>true</activeByDefault>
   </activation>
</profile>

    使用activeByDefault元素用户可以指定profile自动激活。

    注意:如果POM中有任何一个profile通过以上其他任意一种方式被激活了,所有的默认激活配置都会失效。

 

查看当前激活的profile

mvn help:active-profiles

 

列出当前所有的profile:

mvn help:all-profiles  

 

4.profile的种类

 

(1)pom.xml:只对当前项目有效

(2)用户setting.xml:本机上该用户所有的Maven项目有效

(3)全局setting.xml:本机上所有用户的Maven项目有效

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值