您的位置:首页 > 其它

Maven构建项目的灵活性

2016-06-27 00:07 399 查看

概述

典型的项目都会有开发环境、测试环境。比如有多个数据库环境的项目、Web项目资源的过滤等等,Maven为了支持构建的灵活性,内置了三大特性,即属性、Profile和资源过滤。

Maven属性

Maven属性主要有六类

内置属性

主要有两个常用的内置属性,
${basedir}
表示项目的根目录,即包含pom.xml文件的目录;
${version}
表示项目版本。

POM属性

用户可以使用该类属性引用POM文件中对应元素的值,如
${project.artifactId}
就对应了元素的值,常用的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.artifactId}


自定义属性

可以在POM的元素下自定义的属性

<project>
...
<properties>
<xxx.xxx.xxx>XXXXXX</xxx.xxx.xxx>
</properties>
...
</project>


Setting属性

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

Java系统属性

所有Java系统属性都可以使用Maven属性引用,如
${user.home}
指向用户目录。用户可以使用 mvn help:system查看所有的Java系统属性。

环境变量属性

所有环境变量都可以使用以env.开头的Maven属性引用,如
${env.JAVA_HOME}
指代了JAVA_HOME环境变量的值,用户可以使用 mvn help:system查看所有的Java系统属性。

资源过滤

示例如下

1、原本的数据库配置文件,在resources目录下的config.properties

dataSource.url=jdbc:mysql://localhost:3306/huodai
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.username=root
dataSource.password=


2、改为引用属性值

dataSource.url=${dev.dataSource}
dataSource.driverClassName=${dev.className}
dataSource.username=${dev.username}
dataSource.password=${dev.password}


3、修改项目pom.xml文件

<profiles>
<profile>
<id>dev</id>
<properties>
<dev.dataSource>jdbc:mysql://localhost:3306/huodai</dev.dataSource>
<dev.className>com.mysql.jdbc.Driver</dev.className>
<dev.username>root</dev.username>
<dev.password>huawei</dev.password>
</properties>
</profile>
</profiles>


4、此时还不行,Maven属性默认只有在POM中才会被解析,也就是数据库配置文件中的$并不能获取到属性值,因此需要maven解析资源文件中的属性,使用插件maven-resources-plugin,开启资源过滤

<!-- 为主资源目录开启过滤 -->
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!-- 配置多个资源目录 -->
<resource>
<directory>${project.basedir}/src/main/sql</directory>
<filtering>false</filtering>
</resource>
</resources>

<!-- 为测试资源目录开启过滤 -->
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>


5、激活profile,使用命令$mvn clean install -Pdev

-P表示在命令行激活一个profile,这里激活id为dev的profile。

6、完整的pom.xml文件如下

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fengyun.webpro</groupId>
<artifactId>webpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<profiles>
<profile>
<id>dev</id>
<properties>
<dev.dataSource>jdbc:mysql://localhost:3306/huodai</dev.dataSource>
<dev.className>com.mysql.jdbc.Driver</dev.className>
<dev.username>root</dev.username>
<dev.password>huawei</dev.password>
</properties>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.0-beta-2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>

<build>
<defaultGoal>clean compile</defaultGoal>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<home>D:\devware\apache-tomcat-6.0.44</home>
</container>
<configuration>
<type>existing</type>
<home>D:\devware\apache-tomcat-6.0.44</home>
<properties>
<cargo.servlet.port>8080</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<!-- type默认值为installed -->
<type>remote</type>
</container>
<configuration>
<type>runtime</type>
<!-- 不同的Web容器,有不同的属性配置,需要查询相关具体的容器配置 -->
<properties>
<cargo.remote.username>admin</cargo.remote.username>
<cargo.remote.password>root</cargo.remote.password>
<cargo.tomcat.manager.url>http://ip:prot/manager</cargo.tomcat.manager.url>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>

<!-- 为主资源目录开启过滤 --> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> <filtering>true</filtering> </resource> <!-- 配置多个资源目录 --> <resource> <directory>${project.basedir}/src/main/sql</directory> <filtering>false</filtering> </resource> </resources> <!-- 为测试资源目录开启过滤 --> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> </build>
</project>


Profile

不同环境的的profile

<profiles>
<profile>
<id>dev</id>
<properties>
<dev.dataSource>jdbc:mysql://localhost:3306/huodai</dev.dataSource>
<dev.className>com.mysql.jdbc.Driver</dev.className>
<dev.username>root</dev.username>
<dev.password>huawei</dev.password>
</properties>
</profile>
<profile>
<id>teset</id>
<properties>
<dev.dataSource>jdbc:mysql://localhost:3306/huodai</dev.dataSource>
<dev.className>com.mysql.jdbc.Driver</dev.className>
<dev.username>root</dev.username>
<dev.password>test</dev.password>
</properties>
</profile>
</profiles>


激活profile的方式

命令行激活:$mvn clean install -Pdev-one,-Pdev-two ,该命令激活了dev-one和dev-two两个profile

settings文件显式激活:

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


系统属性激活

可以配置当某个系统属性test存在的时候,自动激活profile

<activation>
<property>
<name>test</name>
</property>
</activation>


可以配置当某个系统属性test存在的时候且value值登录AAA,自动激活profile

<activation>
<property>
<name>test</name>
<value>AAA</value>
</property>
</activation>


操作系统环境激活

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


文件是否存在激活

<activation>
<file>
<missing>A.properties</missing>
<exists>B.properties</exists>
</file>
</activation>


默认激活

<activation>
<activeByDefault>true</activeByDefault>
</activation>


maven-help-plugin插件的作用

$mvn help:avtive-profiles
了解当前激活的profile

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

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  maven