maven属性、profile、资源过滤、不同环境构建项目

maven针对不同环境构建项目

maven使用属性、profile及资源过滤支持针对不同环境构建项目


maven属性

maven共有六类属性

1、最常见的是自定义属性,即在pom文件里通过<properties>元素定义的属性

2、环境变量属性,可以使用env.引用,可以使用mvn help:system查看所有的环境变量

java.env=${env.JAVA_HOME}

3、系统属性,也可以使用mvn help:system查看所有的系统属性

${user.home}指向了用户目录,即C:/Users/Administrator
user.home=${user.home}

4、setting属性

localRepository=${settings.localRepository}

5、maven内置属性

${basedir}表示根目录
project.basedir=${basedir}
${version}表示项目版本
project.version=${version}

6、pom属性

源码目录
sourceDirectory=${project.build.sourceDirectory}
测试类源码目录
testSourceDirectory=${project.build.testSourceDirectory}
构建目录
directory=${project.build.directory}
构建后class所在的目录
outputDirectory=${project.build.outputDirectory}
构建后测试类class所在的目录
testOutputDirectory=${project.build.testOutputDirectory}
项目组Id
groupId=${project.groupId}
构件Id
artifactId=${project.artifactId}
项目版本
version=${project.version}
构建后jar包或war包的名称,默认为${project.artifactId}-${project.version}
finalName=${project.build.finalName}
project可以省略
View Code

profile

(1) profile在pom.xml或settings.xml中都可以声明

pom.xml中的profile只对当前项目有效,用户settings.xml中的profile对该用户所有的maven项目有效,全局settings.xml中的profile对本机上所有的maven项目有效
由于pom.xml中的profile能随着pom.xml一起提交到代码仓库中、被安装到本地仓库中、被部署到远程仓库中,所以pom.xml中的frofile可以声明的元素很多,如下所示:

<project>
    <repositories></repositories>
    <pluginRepositories></pluginRepositories>
    <distributionManagement></distributionManagement>
    <dependencies></dependencies>
    <dependenyManagement></dependenyManagement>
    <modules></modules>
    <properties></properties>
    <reporting></reporting>
    <build>
        <plugins></plugins>
        <defalutGoal></defalutGoal>
        <resources></resources>
        <testResources></testResources>
        <finalName></finalName>
    </build>
</project>
View Code

而settings.xml中可以声明的元素很少,只支持以下几个

<project>
    <repositories></repositories>
    <pluginRepositories></pluginRepositories>
    <properties></properties>
</project>
View Code

(2)demo

<project>
  ...
  <profiles>
      <profile>
          <id>dev</id>
          <properties>
              <db.url>jdbc:mysql://localhost:3306/dev</db.url>
              <db.username>root</db.username>
              <db.password>root</db.password>
          </properties>
      </profile>
      <profile>
          <id>test</id>
          <activation>
              <activeByDefault>true</activeByDefault>
          </activation>
          <properties>
              <db.url>jdbc:mysql://localhost:3306/test</db.url>
              <db.username>test</db.username>
              <db.password>test</db.password>
          </properties>
      </profile>
  </profiles>
</project>
View Code

 使用profile包裹其它元素声明与直接使用这些元素声明并无二致


 资源过滤

1、maven属性默认只在pom中被解析,也就是说在profile中声明的<db.username>root</db.username>,只能在pom文件中使用${db.username}引用,如果想在src/main/resources/目录下的资源文件里引用,需要开启资源过滤。

2、如果想在src/main/webapp/目录下的文件里引用maven属性,比如需要在构建项目的时候,给不同的客户使用不一样的资源文件,可能客户的logo图片不同或者css主题不同,想要在一个html文件中使用形如${client.logo}的方式来引用已经由profile定义好的内容,也需要开启资源过滤。

提示:web项目中有两种资源文件,一般资源文件和web资源文件。

一般资源文件同样位于src/main/resources/目录下,它们经过处理后会位于WAR包的WEB-INF/classes目录下,这也是java代码编译打包后的目录。也就是说,这类资源文件在打包后会位于应用程序的classpath中。

web资源文件的源码默认位于src/main/webapp/目录,经打包后位于WAR包的根目录。比如,一个web项目的css源码文件在src/main/webapp/css/目录,项目打包后可以在WAR包的css/目录下找到对应的css文件。

3、开启资源过滤只要给对应插件添加<filtering>true</filtering>就可以了,示例如下:

    <build>
        <finalName>maven-profile-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <!-- 为web资源开启资源过滤 -->
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <!-- 下面这些文件要过滤 -->
                                <include>**/*.css</include>
                                <include>**/*.js</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
        <!-- 为主资源目录开启资源过滤 -->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <!-- 为测试资源目录开启资源过滤 -->
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>
View Code

项目demo

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test.profile</groupId>
    <artifactId>maven-profile-demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-profile-demo</name>

    <properties>
        <my.car>bike</my.car>
    </properties>
    <build>
        <finalName>maven-profile-demo</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <!-- 为web资源开启资源过滤 -->
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <!-- 下面这些文件要过滤 -->
                                <include>**/*.jsp</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
        <!-- 为主资源目录开启资源过滤 -->
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <db.url>jdbc:mysql://localhost:3306/dev</db.url>
                <db.username>root</db.username>
                <db.password>root</db.password>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <db.url>jdbc:mysql://localhost:3306/test</db.url>
                <db.username>test</db.username>
                <db.password>test</db.password>
            </properties>
        </profile>
    </profiles>
</project>
pom.xml
# 一、profile
jdbc.url=${db.url}
jdbc.username=${db.username}
jdbc.password=${db.password}

# 二、maven共有六类属性支持
# 1、最常见的是自定义属性,即在pom文件里通过<properties>元素定义的属性
myCar=${my.car}

# 2、环境变量属性,可以使用env.引用,可以使用mvn help:system查看所有的环境变量
java.env=${env.JAVA_HOME}

# 3、系统属性,user.home指向了用户目录,也可以使用mvn help:system查看所有的系统属性
user.home=${user.home}

# 4、setting属性
# 本地仓库
localRepository=${settings.localRepository}

# 5、maven内置属性,${basedir}表示根目录,${version}表示项目版本
project.basedir=${basedir}
project.version=${version}

# 6、pom属性
# 源码目录
sourceDirectory=${project.build.sourceDirectory}
# 测试类源码目录
testSourceDirectory=${project.build.testSourceDirectory}
# 构建目录
directory=${project.build.directory}
# 构建后class所在的目录
outputDirectory=${project.build.outputDirectory}
# 构建后测试类class所在的目录
testOutputDirectory=${project.build.testOutputDirectory}
# 项目组Id
groupId=${project.groupId}
# 构件Id
artifactId=${project.artifactId}
# 项目版本
version=${project.version}
# 构建后jar包或war包的名称,默认为${project.artifactId}-${project.version}
finalName=${project.build.finalName}
# project可以省略
resources目录下的文件database.properties
jdbc.url=${db.url}
jdbc.username=${db.username}
jdbc.password=${db.password}

myCar=${my.car}
java.env=${env.JAVA_HOME}
user.home=${user.home}
localRepository=${settings.localRepository}
project.basedir=${basedir}
project.version=${version}
sourceDirectory=${project.build.sourceDirectory}
testSourceDirectory=${project.build.testSourceDirectory}
directory=${project.build.directory}
outputDirectory=${project.build.outputDirectory}
testOutputDirectory=${project.build.testOutputDirectory}
groupId=${project.groupId}
artifactId=${project.artifactId}
version=${project.version}
finalName=${project.build.finalName}
resources目录下的文件test.txt
<html>
<body>
jdbc.url=${db.url}
jdbc.username=${db.username}
jdbc.password=${db.password}
</body>
</html>
wabapp目录下的文件index.jsp
1、pom文件中定义了一个profiles
2、pom文件中还开启了web资源过滤和一般资源过滤
3、打包:
    mvn clean install -Ptest
    mvn clean install -Pdev
4、查看target/classes目录下的database.properties、test.txt
   查看target/maven-profile-demo目录下的index.jsp

提示:
maven可以激活多个profile
mvn clean install -Pdev-x,dev-y
readme.txt
原文地址:https://www.cnblogs.com/Mike_Chang/p/10251060.html