maven安装配置及pom.xml与setting.xml优先级 篇中粗略的提到了maven及其相关的配置文件,本文就结合《maven in action》一书中的示例了解下maven打包的用法。本篇中提到的示例中涉及到自动编译和自动测试两块,更简单的仅仅打包的示例可以查看原书代码。

一、目录结构

先看下目录结构:

 1[root@361way.com hello-world]# tree
 2.
 3├── pom.xml
 4└── src
 5    ├── main
 6    │   └── java
 7    │       └── com
 8    │           └── juvenxu
 9    │               └── mvnbook
10    │                   └── helloworld
11    │                       └── HelloWorld.java
12    └── test
13        └── java
14            └── com
15                └── juvenxu
16                    └── mvnbook
17                        └── helloworld
18                            └── HelloWorldTest.java

二、pom文件内容

上面列出的是hello world项目的代码示例。先看pom文件配置信息:

 1[root@361way.com hello-world]# cat pom.xml
 2<?xml version="1.0" encoding="UTF-8"?>
 3<project xmlns="http://maven.apache.org/POM/4.0.0"
 4         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 6http://maven.apache.org/maven-v4_0_0.xsd">
 7  <modelVersion>4.0.0</modelVersion>
 8  <groupId>com.juvenxu.mvnbook</groupId>
 9  <artifactId>hello-world</artifactId>         //包的名称
10  <version>1.0-SNAPSHOT</version>             //包的版本号,生成jar包为hello-wold-1.0-SNAPSHOT.jar
11  <name>Maven Hello World Project</name>
12  <dependencies>
13    <dependency>
14          <groupId>junit</groupId>   //依赖测试包相关信息
15          <artifactId>junit</artifactId>
16          <version>4.7</version>
17          <scope>test</scope>
18    </dependency>
19  </dependencies>
20  <build>   //build 编译的两个相关包信息
21    <plugins>
22          <plugin>
23            <groupId>org.apache.maven.plugins</groupId>
24            <artifactId>maven-compiler-plugin</artifactId>
25            <configuration>
26              <source>1.5</source>
27              <target>1.5</target>
28            </configuration>
29          </plugin>
30          <plugin>
31            <groupId>org.apache.maven.plugins</groupId>
32            <artifactId>maven-shade-plugin</artifactId>
33            <version>1.2.1</version>
34            <executions>
35              <execution>
36                <phase>package</phase>
37                <goals>
38                  <goal>shade</goal>
39                </goals>
40                <configuration>
41                  <transformers>
42                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
43                      <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>  //主函数信息
44                    </transformer>
45                  </transformers>
46                </configuration>
47              </execution>
48            </executions>
49          </plugin>
50    </plugins>
51  </build>
52</project>

按照上面的信息,我们执行mvn clean target 打包时,会在tree命令查看的同级目录下的target目录产生名为hello-wold-1.0-SNAPSHOT.jar的包---默认不指定包类型时默认编译的是jar包 。

三、main程序代码

主函数代码内容如下:

 1[root@361way.com hello-world]# cat src/main/java/com/juvenxu/mvnbook/helloworld/HelloWorld.java
 2package com.juvenxu.mvnbook.helloworld;
 3public class HelloWorld {
 4        public String sayHello()
 5        {
 6                return "Hello Maven";
 7        }
 8        public static void main(String[] args)
 9        {
10                System.out.print( new HelloWorld().sayHello() );
11        }
12}

四、test测试程序代码

内容如下:

 1[root@361way.com hello-world]# cat src/test/java/com/juvenxu/mvnbook/helloworld/HelloWorldTest.java
 2package com.juvenxu.mvnbook.helloworld;
 3import static org.junit.Assert.assertEquals;
 4import org.junit.Test;
 5import com.juvenxu.mvnbook.helloworld.HelloWorld;
 6public class HelloWorldTest
 7{
 8    @Test
 9    public void testSayHello()
10    {
11        HelloWorld helloWorld = new HelloWorld();
12        String result = helloWorld.sayHello();
13        assertEquals( "Hello Maven", result );
14    }

五、编辑执行

需要注意的是,执行完在target/目录,可以看到hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar两个jar包,前者是带有Main-Class信息的可执行jar,后者是原始的jar,打开hello-world-1.0-SNAPSHOT.jar的META-INF/MANIFEST.MF,可以看到它包含这样一行信息: Main-Class:com.juvenxu.mvnbook.helloworld.HelloWorld

 1[root@361way.com hello-world]# mvn clean package
 2[INFO] Scanning for projects...
 3[WARNING]
 4[WARNING] Some problems were encountered while building the effective model for com.juvenxu.mvnbook:hello-world:jar:1.0-SNAPSHOT
 5[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 21, column 14
 6[WARNING]
 7[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
 8[WARNING]
 9[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
10[WARNING]
11[INFO]
12[INFO] ------------------------------------------------------------------------
13[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
14[INFO] ------------------------------------------------------------------------
15[INFO]
16[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
17[INFO]
18[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
19[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
20[INFO] skip non existing resourceDirectory /tmp/ch-3/hello-world/src/main/resources
21[INFO]
22[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
23[INFO] Changes detected - recompiling the module!
24[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
25[INFO] Compiling 1 source file to /tmp/ch-3/hello-world/target/classes
26[INFO]
27[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
28[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
29[INFO] skip non existing resourceDirectory /tmp/ch-3/hello-world/src/test/resources
30[INFO]
31[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---
32[INFO] Changes detected - recompiling the module!
33[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
34[INFO] Compiling 1 source file to /tmp/ch-3/hello-world/target/test-classes
35[INFO]
36[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
37[INFO] Surefire report directory: /tmp/ch-3/hello-world/target/surefire-reports
38-------------------------------------------------------
39 T E S T S
40-------------------------------------------------------
41Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
42Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec
43Results :
44Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
45[INFO]
46[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
47[INFO] Building jar: /tmp/ch-3/hello-world/target/hello-world-1.0-SNAPSHOT.jar
48[INFO]
49[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world ---
50[INFO] Replacing original artifact with shaded artifact.
51[INFO] Replacing /tmp/ch-3/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /tmp/ch-3/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar
52[INFO] ------------------------------------------------------------------------
53[INFO] BUILD SUCCESS
54[INFO] ------------------------------------------------------------------------
55[INFO] Total time: 3.374 s
56[INFO] Finished at: 2014-03-16T21:47:16+08:00
57[INFO] Final Memory: 12M/30M
58[INFO] ------------------------------------------------------------------------

由于之前我已经执行过,所以依赖的包在本地仓库中都存在,所以这里直接取本地仓库中的三个依赖包,在进行测试的过程中也未发现错误,后最编译的jar包位置在示例中也有所体现。再看下新增加目录结构

 1[root@361way.com hello-world]# ll
 2total 12
 3-rw------- 1 root root 1683 Aug 26  2010 pom.xml
 4drwx------ 4 root root 4096 Oct 10  2009 src
 5drwxr-xr-x 7 root root 4096 Dec  6 21:47 target
 6[root@361way.com hello-world]# tree target/
 7target/
 8├── classes
 9│   └── com
10│       └── juvenxu
11│           └── mvnbook
12│               └── helloworld
13│                   └── HelloWorld.class
14├── hello-world-1.0-SNAPSHOT.jar
15├── maven-archiver
16│   └── pom.properties
17├── maven-status
18│   └── maven-compiler-plugin
19│       ├── compile
20│       │   └── default-compile
21│       │       ├── createdFiles.lst
22│       │       └── inputFiles.lst
23│       └── testCompile
24│           └── default-testCompile
25│               ├── createdFiles.lst
26│               └── inputFiles.lst
27├── original-hello-world-1.0-SNAPSHOT.jar
28├── surefire-reports
29│   ├── com.juvenxu.mvnbook.helloworld.HelloWorldTest.txt
30│   └── TEST-com.juvenxu.mvnbook.helloworld.HelloWorldTest.xml
31└── test-classes
32    └── com
33        └── juvenxu
34            └── mvnbook
35                └── helloworld
36                    └── HelloWorldTest.class
3718 directories, 11 files

六、打包相关的几个指令

maven打包过程中经常用到如下几个指令:mvn clean compile,mvn clean test,mvn clean package,mvn clean install 。也可以不加clean,这里以mvn clean package为例,如果当前源代码目录下之前打过包,存在target目录,增加clean 参数会先清理掉之前打包的文件,再重新进行打包操作。

mvn clean compile :该操作是将java源代码在target目录编译成.class文件;

mvn clean test:这个是调用pom文件中junit插件及本机的test目录的测试功能,作用很显然,就是执行上面打包过程中test的包程;

mvn clean package:除执行上面的两上步骤外,还会按pom文件中配置,打包为jar、war、zip格式,并输出相应的包名称;

mvn clean install :这个是将打好的包上传到本地仓库中。

最后再附下《maven in action》 和《maven权威指南》两本书的链接及其中的部分源码。

链接: http://pan.baidu.com/s/1dEeocxj 密码: fv9m