【Maven实战】之灵活的构建

前言

  • Maven为了支持构建的灵活性,内置三大属性,即:
  • 属性
  • Profile
  • 资源过滤

Maven属性

  • 六大属性

  1. 内置属性
  • 主要有两个常用属性【${basedir}】表示系统根目录和【${version}】表示项目版本
  1. 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。
${project.build.finalName}:项目打包输出文件的名称,默认为${project.artifactId}-${project.version}。

  1. 自定义属性
  • 用户可以在POM的<properties>元素下自定义Maven属性。例如:
<project>
    ...
     <properties>
          <my.prop>hello</my.prop>
     </properties>
     ...
</project>
  1. Settings属性
  • 与POM属性同理,用户使用以settings.开头的属性引用settings.xml文件中XML元素的值,如常用的${settings.localRepository}指向用户本地仓库的地址
  1. Java系统属性
  • 所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录。用户可以使用mvn help:system查看所有的Java系统属性。文章后面列出了查询的结果。
  1. 环境变量属性
  • 所有环境变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。用户可以使用mvn help:system查看所有的Java系统属性。

构建环境的差异

  • 通过Maven过滤相关目录下的资源,替换指定字符,以达到通过maven命令控制构建环境的不同,自动化切换环境的效果。
  • 流程如下:
资源过滤流程
  • 更换db.properties文件内数据库自动化定义的内容,如下
dataSource.driverClassName=${db.driverClassName}
dataSource.url=${db.url}
dataSource.username=${db.username}
dataSource.password=${db.password}
  • Pom文件定义如下:
  <profiles>
        <profile>
            <id>dev</id>
            <properties>
               <db.driverClassName>com.mysql.jdbc.Driver</db.driver>
                <db.url>jdbc:mysql://112.74.170.152:3306/andacredit</db.url>
                <db.username>dba</db.username>
                <db.password>AndaTech2017,./</db.password>
            </properties>
        </profile>
    </profiles>
    
    <resources>
            <resource>
               <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- 是否替换资源中的属性-->
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
  • 执行Maven命令如下:
mvn clean install -Pdev 
-P参数表示在命令行激活一个Profile
  • 查看class输出路径下的db.properties

Maven Profile

  • Maven Profile是为了能让构建在各个环境下方便地移植而产生的一个Maven元素。
Profile的六种激活方式
  1. 命令行激活
mvn clean install -Pdev-one,-Pdev-two
多个id之间用【,】隔开
  1. settings文件显式激活
  • 使某个Profile一直处于激活状态,使用如下配置:
<settings>
    ...
    <activeProfiles>
        <activeProfile>dev-x</activeProfile>
    </activeProfiles>
    ...
</settings>
  1. 系统属性激活
<profiles>
	<profile>
		<activation>
			<property>test</property>
		</activation>
		...
	</profile>
</profiles>
配置当【test】系统属性存在时,激活该profile,或者
<profiles>
	<profile>
		<activation>
			<property>test</property>
			<value>x</value>
		</activation>
		...
	</profile>
</profiles>
当【test】系统属性存在且值等于X时,激活该profile
  1. 操作系统环境激活
<profiles>
	<profile>
		<activation>
			<os>
				<name>Window XP</name>
				<family>Windows</family>
				<arch>x86</arch>
				<version>5.1.2600</version>
			</os>
		</activation>
		...
	</profile>
</profiles>
  1. 文件存在与否激活
<profiles>
	<profile>
		<activation>
		    <file>
		        <missing>x.properties</missing>
		        <exists>y.properties</exists>
		    </file>
		</activation>
		...
	</profile>
</profiles>
  1. 默认激活
<profiles>
	<profile>
	    <id>dev</id>
		<activation>
		    <activeByDefault>true</activeByDefault>
		</activation>
		...
	</profile>
</profiles>
  • 注意:如果Pom中有任何一个profile通过以上其他任意一种方式激活,所有默认激活的配置都会失效。

  • 了解当前激活的profile
mvn help:active-profiles
  • 列出当前所有的profile
mvn help:all-profiles
profile的种类
  • pom.xml 当前项目有效
  • 用户settings.xml .m2/settings.xml 本机上该用户所有项目有效
  • 全局settings.xml ${MAVEN_HOME}/conf/settings.xml 本机上所有项目有效
  • profiles.xml

Web资源过滤

  • 通过resource属性指定资源目录并开启过滤和指定文件,就可以通过命令行过滤指定文件中的特定字段。
<profiles>
    <profile>
        <id>css-a</id>
        <properties>
              <img.logo>a.jpg</img.logo>
              <img.color>red</img.color>
        </properties>
    </profile>
        <profile>
        <id>css-a</id>
        <properties>
              <img.logo>b.jpg</img.logo>
              <img.color>blue</img.color>
        </properties>
    </profile>
</profiles>
    
<resources>
    <resource>
        <directory>src/main/web</directory>
        <!--指定要过滤的文件-->
        <includes>
            <include>**/*.css</include>
            <include>**/*.js</include>
        </includes>
        <!-- 是否替换资源中的属性-->
        <filtering>true</filtering>
    </resource>
</resources>

转载于:https://my.oschina.net/u/3413394/blog/1588798

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值