【maven实战】44-构件环境的差异

本文介绍如何使用Maven的profile特性及资源过滤功能,根据不同环境自动调整数据库配置,确保项目的顺利部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在不同的环境中,项目的源码应该使用不同的方式进行构建。最常见的就是数据库配置了。例如在开发的过程中,有些项目会在src/main/resources/目录下放置带有如下内容的数据库配置文件。

开发环境:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test
jdbc.user=dev
jdbc.password=dev-pwd

测试环境:

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://192.168.1.100:3306/test
jdbc.user=test
jdbc.password=test-pwd

为了应对环境的变化,首先需要使用Maven属性将这些将会发生变化的部分提取出来。上面的配置中连接数据库使用的驱动类,URL,用户名和密码都可能发生变化,因此用Maven属性取代它们:

jdbc.driverClass=${db.driver}
jdbc.jdbcUrl=${db.url}
jdbc.user=${db.username}
jdbc.password=${db.username}

既然使用了Maven属性,就应该在某个地方定义它们。这里要做的是使用一个额外的profile将其包裹。

 

<project>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3306/test</db.url>
            <db.username>dev</db.username>
            <db.password>dev-pwd</db.password>
        </properties>
    </profile>
</profiles>
</project>

Maven属性默认只有在POM中才会被解析,也就是说${db.username}放到POM中会变成dev,但是如果放到src/main/resources/目录下构建的时候它将仍然还是${db.username}。因此需要让Maven解析资源文件中的Maven属性。

资源文件的处理其实是maven-resources-plugin做的事情,它默认的行为只是将项目主资源文件复制到主代码编译输出目录中,将测试代码输出到测试代码输出目录中。不过只要通过一些简单的POM配置,该插件就能够解析资源文件中的Maven属性,即可开启资源过滤。

Maven默认的主资源目录和测试目录的定义在超级POM中,要为资源目录开启过滤,只要在此基础上添加一行filtering配置即可,如下:

<build>  
        <resources>  
            <resource>  
                <directory>${basedir}/src/main/resources</directory>  
                <filtering>true</filtering>  
            </resource>  
        </resources>  
        <testResources>  
            <testResource>  
                <directory>${basedir}/src/test/resources</directory>  
                <filtering>true</filtering>  
            </testResource>  
        </testResources>  
 </build>

主资源目录和测试资源目录都可以超过一个,虽然会破坏Maven的约定,但Maven允许用户声明多个资源目录,并且为每个资源目录提供不同的过滤配置,

<build>  
        <resources>  
            <resource>  
                <directory>${basedir}/src/main/resources</directory>  
                <filtering>true</filtering>  
            </resource> 
            <resource>  
                <directory>${basedir}/src/main/sql</directory>  
                <filtering>false</filtering>  
            </resource> 
        </resources>  
</build>

最后只需要在命令行激活profile,Maven就能够在构建项目的时候使用profile中属性值替换数据库配置文件中的属性引用。运行命令如下:

mvn clean install -Pdev

mvn的-P参数表示在命令行激活一个profile。这里激活了id为dev的profile。构建完成后,输出目录中的数据库配置就是开发环境的配置了:

 

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test
jdbc.user=dev
jdbc.password=dev-pwd

为了能让构建在各个环境下方便地移植,Maven引入了profile的概念。profile能够在构建的时候修改POM的一个子集,或者添加额外的配置元素。用户可以使用很多方式激活profile,以实现构建在不同环境下的移植。

如下代码引入了一个针对开发环境的profile,类似地可以加入测试环境和产品环境的profile。

<project>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3306/test</db.url>
            <db.username>dev</db.username>
            <db.password>dev-pwd</db.password>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://192.168.1.100:3306/test</db.url>
            <db.username>test</db.username>
            <db.password>test-pwd</db.password>
        </properties>
    </profile>
</profiles>
</project>

现在开发人员可以在使用mvn命令的时候在后面加上-Pdev激活dev profile,而测试人员可以使用-Ptest激活test profile。

多种方式激活profile

命令行激活:用户可以使用mvn命令行参数-P加上profile的id来激活profile,多个id之间以逗号分隔,例如下面的命令激活了dev-x和dev-y两个profile。

mvn clean install -Pdev-x,dev-y

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

<settings>
...
<activeProfiles>
    <activeProfile>dev-x</activeProfile>
</activeProfiles>
...
</settings>

系统属性激活:用户可以配置当某系统属性存在的时候,自动激活profile。

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name>
            </property>
        </activation>
    </profile>
</profiles>

可进一步配置当系统属性test存在且等于x的时候激活profile:

 

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name><value>x</value>
            </property>
        </activation>
    </profile>
</profiles>

不要忘了用户可以在命令行生命系统属性。

mvn clean install -Dtest=x

操作系统环境激活:profile还可以自动根据操作系统环境激活,如果构建在不同的残做系统有差异,用户完全可以将这些差异写进profile,然后配置他们自动基于操作系统环境构建。


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

这里family的值包括Windows,UNIX和Mac等,而其他几项name,arch,version,用户可以通过查看环境中的系统属性os.name,os.arch,os.version获得。

文件存在与否激活:Maven能够根据项目中某个文件存在与否来决定是否激活profile:

 

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

默认激活:用户可以定义profile的时候指定其默认激活:

 

<profiles>
    <profile>
        <id>dev</id>
        <activation>
           <activeByDefault>true</activeByDefault>
        </activation>
        ...
    </profile>
</profiles

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

如果项目中有很多的profi;e,他们的激活方式各异,用户怎么知道哪些profile被激活了呢?maven-help-plugin提供了一个目标帮助用户了解当前激活的profile:

mvn help:active-profiles

maven-help-plugin还有另外一个目标来列出当前所有的profile:

mvn help:all-profiles

profile的种类

 

  • pom.xml:很显然,pom.xml中声明的profile只对当前项目有效。
  • 用户settings.xml:用户目录下的.m2/settings.xml中的profile对本机上该用户所有的Maven项目有效。
  • 全局settings.xml:Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值