Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | Email

2.3. maven

http://maven.apache.org/

2.3.1. 安装

2.3.1.1. Ubuntu

$ sudo apt-get install maven2

$ mvn -version
Apache Maven 2.2.1 (rdebian-4)
Java version: 1.6.0_22
Java home: /usr/lib/jvm/java-6-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "2.6.38-8-generic" arch: "amd64" Family: "unix"

			
JAVA_HOME="/usr/lib/jvm/java-6-openjdk/jre"
MAVEN_HOME="/usr/share/maven2/"
			

2.3.1.2. 源码安装

curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/java/maven/maven.sh | bash
			

2.3.2. Maven 仓库

http://search.maven.org/

http://mvnrepository.com/

本地下载目录

$ ls ~/.m2
		

2.3.3. pom.xml

2.3.3.1. properties

定义 properties

			
<properties>
    <spring.version>4.0.1.RELEASE</spring.version>
</properties>
			
			

引用 properties

			
  <!-- Spring dependencies -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>
			
			

例 2.1. Maven properties

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/maven-v4_0_0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javahash.web</groupId>
 <artifactId>Spring4MVCHelloWorld</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>Spring4MVCHelloWorld Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <properties>
    <spring.version>4.0.1.RELEASE</spring.version>
 </properties>

 <dependencies>

    <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
    </dependency>

   <!-- Spring dependencies -->
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>

 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${spring.version}</version>
 </dependency>

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
 </dependency>
</dependencies>

 <build>
 <finalName>Spring4MVCHelloWorld</finalName>
 </build>
</project>
				
				

2.3.3.2. dependencies

			
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.apache.struts</groupId>
    	<artifactId>struts2-core</artifactId>
    	<version>2.3.24.1</version>
    </dependency>
  </dependencies>
			
			

2.3.3.3. build

2.3.3.3.1. sourceDirectory

编译源码目录

				
<sourceDirectory>src</sourceDirectory>				
				
				
2.3.3.3.2. resources

将资源文件编译后复制到 WEB-INF/classes 目录中

				
		<resources>
			<resource>
				<directory>src/resources</directory>
			</resource>
		</resources>			
				
				

include / exclude

				
<resources>  
    <!-- Filter jdbc.properties & mail.properties. NOTE: We don't filter applicationContext-infrastructure.xml,   
        let it go with spring's resource process mechanism. -->  
    <resource>  
        <directory>src/main/resources</directory>  
        <filtering>true</filtering>  
        <includes>  
            <include>jdbc.properties</include>  
            <include>mail.properties</include>  
        </includes>  
    </resource>  
    <!-- Include other files as resources files. -->  
    <resource>  
        <directory>src/main/resources</directory>  
        <filtering>false</filtering>  
        <excludes>  
            <exclude>jdbc.properties</exclude>  
            <exclude>mail.properties</exclude>  
        </excludes>  
    </resource>  
</resources>
				
				

2.3.3.4. plugins

2.3.3.4.1. 跳过Unit test
			
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skip>true</skip>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>			
			
			
2.3.3.4.2. maven-shade-plugin
				
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>1.4</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>cn.netkiller.Oracle</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>				
				
				

2.3.4. Maven 命令

2.3.4.1. clean

清楚

$ mvn clean		
			

2.3.4.2. compile

编译项目的源代码

$ mvn compile
			

2.3.4.3. test

编译测试的源代码

mvn test-compile			
			

运行测试

$ mvn test
			

打包但不测试

mvn -D maven.test.skip=true package
			

2.3.4.4. package

将编译后的代码打包

$ mvn package
			
$ ls target/*.war
target/www.netkiller.cn-0.0.1-SNAPSHOT.war
			

2.3.4.5. install

将项目打包后安装到本地仓库,可以作为其它项目的本地依赖。

$ mvn install		
			

跳过测试

mvn install -Dmaven.test.skip=true			
			

2.3.4.6. war

mvn compile war:war			
			
mvn compile war:exploded			
			
mvn compile war:inplace
			

2.3.4.7. exec

$ mvn exec:java -Dexec.mainClass="cn.netkiller.Test"			
			

2.3.5. dependency

2.3.5.1. build-classpath

$ mvn dependency:build-classpath
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building api.netkiller.cn 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:build-classpath (default-cli) @ api.cf88.com ---
[INFO] Dependencies classpath:
/www/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.3.0.RELEASE/spring-boot-starter-web-1.3.0.RELEASE.jar:......:/www/.m2/repository/junit/junit/4.12/junit-4.12.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.692 s
[INFO] Finished at: 2016-08-10T17:32:49+08:00
[INFO] Final Memory: 25M/162M
[INFO] ------------------------------------------------------------------------
			
			

2.3.5.2. dependency:tree 显示包依赖树

[www@iZ62yln3rjjZ repository]$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building api.example.com 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ api.cf88.com ---
[INFO] cf88.com:api.cf88.com:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.3.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.0.RELEASE:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.3:compile
[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.1.3:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.13:compile
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.13:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.0.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.28:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.28:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.28:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.28:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.0.RELEASE:compile
[INFO] |  |  \- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[INFO] |  |     +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |     \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.3:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.3:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.6.3:compile
[INFO] |  +- org.springframework:spring-web:jar:4.2.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-aop:jar:4.2.3.RELEASE:compile
[INFO] |  |     \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.3.0.RELEASE:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.7:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.9.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-orm:jar:4.2.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.apache.tomcat:tomcat-jdbc:jar:8.0.28:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-juli:jar:8.0.28:compile
[INFO] |  \- org.springframework:spring-jdbc:jar:4.2.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-redis:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:1.6.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context-support:jar:4.2.3.RELEASE:compile
[INFO] |  \- redis.clients:jedis:jar:2.7.3:compile
[INFO] |     \- org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:1.3.0.RELEASE:compile
[INFO] |  \- org.mongodb:mongo-java-driver:jar:2.13.3:compile
[INFO] +- org.springframework.boot:spring-boot-starter-amqp:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-messaging:jar:4.2.3.RELEASE:compile
[INFO] |  \- org.springframework.amqp:spring-rabbit:jar:1.5.2.RELEASE:compile
[INFO] |     +- org.springframework.retry:spring-retry:jar:1.1.2.RELEASE:compile
[INFO] |     +- org.springframework.amqp:spring-amqp:jar:1.5.2.RELEASE:compile
[INFO] |     +- com.rabbitmq:http-client:jar:1.0.0.RELEASE:compile
[INFO] |     |  \- org.apache.httpcomponents:httpclient:jar:4.5.1:compile
[INFO] |     |     +- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] |     |     \- commons-codec:commons-codec:jar:1.9:compile
[INFO] |     \- com.rabbitmq:amqp-client:jar:3.5.6:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.3.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.3.0.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.3.0.RELEASE:test
[INFO] |  +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] |  |  \- org.objenesis:objenesis:jar:2.1:test
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  +- org.springframework:spring-core:jar:4.2.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-test:jar:4.2.3.RELEASE:test
[INFO] +- org.springframework.data:spring-data-mongodb:jar:1.8.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.2.3.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-commons:jar:1.11.1.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.13:compile
[INFO] |  \- org.slf4j:jcl-over-slf4j:jar:1.7.13:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.37:compile
[INFO] +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] \- junit:junit:jar:4.12:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.400 s
[INFO] Finished at: 2016-08-09T10:25:07+08:00
[INFO] Final Memory: 23M/163M
[INFO] ------------------------------------------------------------------------		
			

2.3.5.3. copy-dependencies 导出依赖包

$ mvn dependency:copy-dependencies
			
$ ls target/dependency/
antlr-2.7.7.jar                                javassist-3.18.1-GA.jar                    snakeyaml-1.16.jar                                spring-boot-starter-web-1.3.0.RELEASE.jar        spring-webmvc-4.2.3.RELEASE.jar
aopalliance-1.0.jar                            javax.transaction-api-1.2.jar              spring-aop-4.2.3.RELEASE.jar                      spring-boot-starter-websocket-1.3.0.RELEASE.jar  spring-websocket-4.2.3.RELEASE.jar
aspectjweaver-1.8.7.jar                        jboss-logging-3.3.0.Final.jar              spring-aspects-4.2.3.RELEASE.jar                  spring-context-4.2.3.RELEASE.jar                 thymeleaf-2.1.4.RELEASE.jar
classmate-1.1.0.jar                            jboss-logging-annotations-1.2.0.Beta1.jar  spring-beans-4.2.3.RELEASE.jar                    spring-core-4.2.3.RELEASE.jar                    thymeleaf-layout-dialect-1.3.1.jar
dom4j-1.6.1.jar                                jcl-over-slf4j-1.7.13.jar                  spring-boot-1.3.0.RELEASE.jar                     spring-data-commons-1.11.1.RELEASE.jar           thymeleaf-spring4-2.1.4.RELEASE.jar
groovy-2.4.4.jar                               jstl-1.2.jar                               spring-boot-autoconfigure-1.3.0.RELEASE.jar       spring-data-jpa-1.9.1.RELEASE.jar                tomcat-embed-core-8.0.28.jar
hibernate-commons-annotations-4.0.5.Final.jar  jul-to-slf4j-1.7.13.jar                    spring-boot-starter-1.3.0.RELEASE.jar             spring-expression-4.2.3.RELEASE.jar              tomcat-embed-el-8.0.28.jar
hibernate-core-4.3.11.Final.jar                log4j-over-slf4j-1.7.13.jar                spring-boot-starter-aop-1.3.0.RELEASE.jar         spring-jdbc-4.2.3.RELEASE.jar                    tomcat-embed-logging-juli-8.0.28.jar
hibernate-entitymanager-4.3.11.Final.jar       logback-classic-1.1.3.jar                  spring-boot-starter-data-jpa-1.3.0.RELEASE.jar    spring-messaging-4.2.3.RELEASE.jar               tomcat-embed-websocket-8.0.28.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar          logback-core-1.1.3.jar                     spring-boot-starter-jdbc-1.3.0.RELEASE.jar        spring-orm-4.2.3.RELEASE.jar                     tomcat-jdbc-8.0.28.jar
hibernate-validator-5.2.2.Final.jar            mybatis-3.3.0.jar                          spring-boot-starter-logging-1.3.0.RELEASE.jar     spring-security-config-4.0.3.RELEASE.jar         tomcat-juli-8.0.28.jar
jackson-annotations-2.6.3.jar                  mybatis-spring-1.2.3.jar                   spring-boot-starter-security-1.3.0.RELEASE.jar    spring-security-core-4.0.3.RELEASE.jar           unbescape-1.1.0.RELEASE.jar
jackson-core-2.6.3.jar                         mysql-connector-java-5.1.37.jar            spring-boot-starter-thymeleaf-1.3.0.RELEASE.jar   spring-security-web-4.0.3.RELEASE.jar            validation-api-1.1.0.Final.jar
jackson-databind-2.6.3.jar                     ognl-3.0.8.jar                             spring-boot-starter-tomcat-1.3.0.RELEASE.jar      spring-tx-4.2.3.RELEASE.jar                      xml-apis-1.0.b2.jar
jandex-1.1.0.Final.jar                         slf4j-api-1.7.13.jar                       spring-boot-starter-validation-1.3.0.RELEASE.jar  spring-web-4.2.3.RELEASE.jar		
			

2.3.5.4. analyze 查看未被使用的包

$ mvn dependency:analyze			
			

2.3.5.5. sources 下载源码

mvn dependency:sources			
			

2.3.6. help

查看可用的pom定义

mvn help:effective-pom			
		

2.3.7. resources 文件处理

		
		
		<resources>
			<resource>
				<directory>src/resources</directory>
				<excludes>
					<exclude>development.properties</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/resources</directory>
				<includes>
					<include>development.properties</include>
				</includes>
				<targetPath>resources</targetPath>
			</resource>
		</resources>
				
		
		

2.3.8. plugins

2.3.8.1. maven-antrun-plugin

查看可用的pom定义

			
or alternatively, we can use an external build.xml.

<project>
  <modelVersion>4.0.0</modelVersion>
  <artifactId>my-test-app</artifactId>
  <groupId>my-test-group</groupId>
  <version>1.0-SNAPSHOT</version>

  <build>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>compile</id>
            <phase>compile</phase>
            <configuration>
              <target>
                <property name="compile_classpath" refid="maven.compile.classpath"/>
                <property name="runtime_classpath" refid="maven.runtime.classpath"/>
                <property name="test_classpath" refid="maven.test.classpath"/>
                <property name="plugin_classpath" refid="maven.plugin.classpath"/>

                <echo message="compile classpath: ${compile_classpath}"/>
                <echo message="runtime classpath: ${runtime_classpath}"/>
                <echo message="test classpath:    ${test_classpath}"/>
                <echo message="plugin classpath:  ${plugin_classpath}"/>

                <ant antfile="${basedir}/build.xml">
                  <target name="test"/>
                </ant>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

The build.xml:

<?xml version="1.0"?>
<project name="test6">

    <target name="test">

      <echo message="compile classpath: ${compile_classpath}"/>
      <echo message="runtime classpath: ${runtime_classpath}"/>
      <echo message="test classpath:    ${test_classpath}"/>
      <echo message="plugin classpath:  ${plugin_classpath}"/>

    </target>

</project>