信息流zt_maven的pom.xml详解

一个项目里总会有很多配置文件。而且一般都会有多套环境。开发的、测试的、正式的。而在这些不同的环境这些配置的值都会不一样。比如mail的配置、服务的url配置这些都是很常见的。所以在打包的时候就要根据environment来选不同的值或者配置文件。比较常用的办法就是为不同的环境建立不同的配置文件目录。在打包的时候用对应的文件目录下的配置文件。
在Maven项目中,profile是根据不同的构件环境,对构建(build)过程进行动态配置的手段。可以通过pom.xml定义多个profile,也可以通过settings.xml文件定义多个profile。对于一个profile,如果同时在pom.xml和settings.xml中配置,则settings.xml中的配置优先。
Maven目录下的conf文件夹下的settings.xml为全局的,影响所有用户的配置。一般建议修改c:\Users\用户名\.m2\目录下的settings.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">
    <!--父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括group ID,artifact ID和 version。-->  
     <parent>  
     <!--被继承的父项目的构件标识符-->  
     <artifactId/>  
     <!--被继承的父项目的全球唯一标识符-->  
     <groupId/>  
     <!--被继承的父项目的版本-->  
     <version/>  
     <!-- 父项目的pom.xml文件的相对路径。相对路径允许你选择一个不同的路径。默认值是../pom.xml。Maven首先在构建当前项目的地方寻找父项 目的pom,其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。-->  
     <relativePath/>  
    </parent>
    <!--声明项目描述符遵循哪一个POM模型版本。模型本身的版本很少改变,虽然如此,但它仍然是必不可少的,这是为了当Maven引入了新的特性或者其他模型变更的时候,确保稳定性。-->     
    <modelVersion>4.0.0</modelVersion>
    <!--项目的全球唯一标识符,通常使用全限定的包名区分该项目和其他项目。并且构建时生成的路径也是由此生成, 如com.mycompany.app生成的相对路径为:/com/mycompany/app-->   
    <groupId>com.jd.adsbi</groupId>
    <!-- 构件的标识符,它和group ID一起唯一标识一个构件。换句话说,你不能有两个不同的项目拥有同样的artifact ID和groupID;在某个 特定的group ID下,artifact ID也必须是唯一的。构件是项目产生的或使用的一个东西,Maven为项目产生的构件包括:JARs,源 码,二进制发布和WARs等。-->   
    <artifactId>report-system</artifactId>
    <!--项目当前版本,格式为:主版本.次版本.增量版本-限定版本号-->
    <version>0.0.1-SNAPSHOT</version>
    <!--项目产生的构件类型,例如jar、war、ear、pom。插件可以创建他们自己的构件类型,所以前面列的不是全部构件类型-->
    <packaging>war</packaging>
    <!--项目的名称, Maven产生的文档用-->
    <name>jd-adsbi-report-system</name>
    <!--项目主页的URL, Maven产生的文档用-->
    <url>http://maven.apache.org</url>
    <!-- 项目的详细描述, Maven 产生的文档用。  当这个元素能够用HTML格式描述时(例如,CDATA中的文本会被解析器忽略,就可以包含HTML标 签), 不鼓励使用纯文本描述。如果你需要修改产生的web站点的索引页面,你应该修改你自己的索引页文件,而不是调整这里的文档。-->   
    <description>jd maven project</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <slf4j.version>1.7.5</slf4j.version>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
	<!--一定要设置成true这样才会用对应environment目录下的配置文件覆盖原来的。mvn package的时候就会用默认的dev目录下的配置文件。如果要发布正式环境的包就运行。mvn -Pprod package.-->
        <package.environment>dev</package.environment>
    </properties>
    <!--在列的项目构建profile,如果被激活,会修改构建处理-->  
    <profiles>
        <!--根据环境参数或命令行参数激活某个构建处理-->
        <profile>
	    <!--构建配置的唯一标识符。即用于命令行激活,也用于在继承时合并具有相同标识符的profile。--> 
            <id>dev</id>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
	    <!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。profile的力量来自于它  
   能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。activation元素并不是激活profile的唯一方式。-->
            <activation>
                <property>
                    <name>env</name>
                    <value>dev</value>
                </property>
		<!--profile默认是否激活的标志-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
            <activation>
                <property>
                    <name>env</name>
                    <value>test</value>
                </property>
            </activation>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <package.environment>pre</package.environment>
            </properties>
            <activation>
                <property>
                    <name>env</name>
                    <value>pre</value>
                </property>
            </activation>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <package.environment>prod</package.environment>
            </properties>
            <activation>
                <property>
                    <name>env</name>
                    <value>prod</value>
                </property>
            </activation>
        </profile>
    </profiles>
    <dependencies>
        <dependency>
    		<groupId>org.json</groupId>
    		<artifactId>json</artifactId>
    		<version>20090211</version>
		</dependency>
		<dependency>
    		<groupId>commons-io</groupId>
    		<artifactId>commons-io</artifactId>
    		<version>2.4</version>
  		</dependency>
  		<dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.4</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>
    </dependencies>
    
    <!--构建项目所需要的信息。参见build元素--> 
    <build>
        <finalName>jd-adsbi-report-system</finalName>
        <resources>
	    <!--这个元素描述了项目相关或测试相关的所有资源路径--> 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>dev/*</exclude>
                    <exclude>prod/*</exclude>
                    <exclude>test/*</exclude>
                    <exclude>pre/*</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources/${package.environment}</directory>
                <targetPath>./</targetPath>
                <filtering>true</filtering>
            </resource>
        </resources>
	<!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。--> 
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <plugins>
            <plugin>
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>3.1</version>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration>  
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <skip>false</skip>
                </configuration>
            </plugin>
            <plugin>
    			<!--插件在仓库里的group ID-->  
			<groupId>org.apache.maven.plugins</groupId>
			<!--插件在仓库里的artifact ID-->
    			<artifactId>maven-failsafe-plugin</artifactId>
			<!--被使用的插件的版本(或版本范围)-->
    			<version>2.17</version>
			<!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。--> 
    			<executions>
			        <!--execution元素包含了插件执行需要的信息--> 
        			<execution>
				<!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->
            			<id>integration-tests</id>
				<!--配置的执行目标-->
				<!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->  
                                <phase><phase/>
            			<goals>
                			<goal>integration-test</goal>
                			<goal>verify</goal>
            			</goals>
				<!--作为DOM对象的配置--> 
            			<configuration>
            				<excludes>
              					<exclude>none</exclude>
            				</excludes>
            				<includes>
              					<include>**/*IT.java</include>
            				</includes>
          				</configuration>
        			</execution>
    			</executions>
			</plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <warName>jd-adsbi-report-system</warName>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/${package.environment}</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <filtering>true</filtering>
                        </resource>
                        <resource>
                            <directory>src/main/resources</directory>
                            <targetPath>WEB-INF/classes/</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <finalName>jd-adsbi-report-system</finalName>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.jd.dsp.saf.TestInterface</mainClass>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>META-INF/spring.tooling</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

使用方法如下:

Build eclipse project: mvn eclipse:eclipse

## how to build

#### 1. build with different environment config
```
mvn package -Denv=dev  # mvn package  use dev as default
mvn package -Denv=test
mvn package -Denv=pre
mvn package -Denv=prod
```

Tips: git untracked directory: `src/main/resources/dev` , please put your local config file in this path.

#### 2. mvn package skip tests (package phase is behind integration-test, so [mvn package] will not run integration test)
```
mvn package -Denv=dev -DskipTests
```

#### 3. test dir tree
dir tree:
```
Unit Test Classes        : src/test/java/**/**Test.java
Integration Test Classes : src/test/java/**/**IT.java
TestCases dependent files: src/test/resources/testFiles/*
```

#### 4. mvn unit test
run unit test:
```shell
mvn clean -Denv=dev test  ## use src/main/resources/dev as testing runtime config
```

#### 5. mvn integration test
run integration test:
```shell
mvn -Denv=dev failsafe:integration-test
```

#### 6. mvn all test 
- NOTE: Only when integration web server has been started, can we do all test, otherwise integration tests will fail
- run all test:
```shell
mvn clean -Denv=dev integration-test
```

#### 7. mvn install without all tests
```shell
mvn clean install -Denv=dev -DskipTests
```

#### 8. mvn install only with unit tests
```shell
mvn clean install -Denv=dev -DskipITs
```

#### 9. mvn install with all tests
```shell
mvn clean install -Denv=dev
```

参考:

http://blog.youkuaiyun.com/cloud_ll/article/details/45290169

http://www.infoq.com/cn/news/2011/03/xxb-maven-5-integration-test/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值