Maven属性、profile和资源过滤

Maven的六类属性

  内置属性

    主要有两个常用内置属性:${basedir}项目的根目录(包含pom.xml文件的目录),${version}项目版本

  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.fianlName}:项目打包输出文件的名称。默认为${project.artifactId}-${project.version}

  自定义属性

    用户可以在POM的<properties>元素下自定义Maven属性

  Settings属性

    用户使用settings.开头的属性引用settings.xml文件中XML元素的值

  Java系统属性

    所有Java系统属性都可以使用Maven属性引用

  环境变量属性

    所有环境变量都可以使用以env.开头的Maven属性引用

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>part-a</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>part-b</artifactId>
        <version>${project-version</version>
    </dependency>
</dependencies>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <repositoryDirectory>${project.build.directory}/test-reports</repositoryDirectory>
    </configuration>
</plugin>
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3360/test</db.url>
            <db.username>username</db.username>
            <db.password>password></db.password>
        </properties>
    </profile>
</profiles>

 Maven属性默认只有在POM中才会被解析,因此需要让Maven解析资源文件中的Maven属性。Maven用maven-resources-plugin处理资源文件。它默认的行为只是将项目主资源文件复制到主代码编译输出目录中,将测试资源文件复制到测试代码编译输出目录中。Maven默认的主资源目录和测试资源目录的定义是在超级POM中,要为资源目录开启过滤,只要在此基础上添加一行filtering配置即可。Filtering是maven resource插件的功能,作用是用环境变量,pom文件里定义的属性和指定文件里的属性替换属性文件的占位符。

<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>

在src/main/resources目录下创建jdbc.properties文件:

database.jdbc.driverClass = ${db.driver}

database.jdbc.connectionURL = ${db.url}

database.jdbc.username = ${db.username}

database.jdbc.password = ${db.password}

mvn clean install  -Pdev //-P参数表示在命令行激活一个profile。编译后在target目录下jdbc.properties中,在maven配置的属性就会显示在jdbc.properties文件中。

Maven支持多种方式激活profile

  1.命令行激活

    用户可以使用mvn命令行参数-P加上profile的id来激活profile,多个id之间以逗号分割。

      mvn clean install -Pdev-x, dev-y

  2.settings文件显式激活

    用户希望某个profile默认一直处于激活的状态,可以配置settings.xml文件的activeProfiles元素

<settings>

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

</settings>

  3.系统属性激活

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

<profiles> 
    <profile>
        <activation>
            <property>
                <name>test</name> 
          <value>x</value> //当值为x的时候激活profile
</property> </activation> </profile> </profiles>

    mvn clean install -Dtest = x

  4.操作系统环境激活

    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>

  5.文件存在与否激活

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

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

  6.默认激活

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

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

查看当前激活的profile  mvn help:active-profiles

列出当前所有的profile      mvn help:all-profiles

profile的种类

  根据具体的需要,可以在以下位置声明profile

  pox.xml:只对当前项目有效

  用户settings.xml:用户目录下.m2/settings.xml中的profile对本机上该用户所有的Maven项目有效

  全局settings.xml:Mavem安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效

  profiles.xml:还可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,不过该特性已经在Maven3中被移除。建议用户将这类profile移到settings.xml中。

POM中profile可使用的元素

<project>

  <repositories></repositories>  //修改或添加仓库

  <pluginRepositories></pluginRepositories>  //修改或添加插件仓库

  <distributionManagement></distributionManagement>  //修改或添加仓库部署地址

  <dependencies></dependencies>  //修改或添加项目依赖

  <dependencyManagement></dependencyMangement>  //修改或添加项目依赖

  <modules></modules>  //修改聚合项目的聚合配置

  <properties></properties>  //自由添加或修改Maven属性

  <reporting></reporting>  //添加或修改项目报告配置

  <build>

    <plugins><plugins>  

    <defaultGoal></defaultGoal>

    <resources></resources>

    <testResources></testResources>

    <finalName></finalName>

  </build>

</project>

Web资源过滤

  在Web项目中,资源文件位于src/main/resources/目录下,他们经处理后会位于WAR包的WEB-INF/classes目录下,即这类资源文件在打包过后位于应用程序的classpath中。Web项目中位于src/main/webapp目录,经打包后位于WAR包的根目录。这一类资源文件称作web资源文件,他们在打包过后不位于应用程序的classpath中。web资源默认不会被过滤,因此开启一般资源文件的过滤也不会影响到web资源文件。

<profiles>
    <profile>
        <id>client-a</id>
        <properties>
            <client.logo>a.jpg</client.logo>
            <client.theme>red</client.theme>
        </properties>
    </profile>
    <profile>
        <id>client-b</id>
        <properties>
            <client.logo>b.jpg</client.logo>
            <client.theme>blue</client.theme>
        </properties>
    </profile>
</profiles>

需要配置maven-war-plugin对src/main/webapp这一web资源目录开启过滤

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1-beta-1</version>
    <configuration>
        <webResources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/webapp</directory>
                <includes>
                    <include>**/*.css</include>
                    <include>**/*.js</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>
原文地址:https://www.cnblogs.com/forerver-elf/p/6257395.html