maven打包之hello world

2014年3月16日 发表评论 阅读评论

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

一、目录结构

先看下目录结构:

[root@361way.com hello-world]# tree
.
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── com
    │           └── juvenxu
    │               └── mvnbook
    │                   └── helloworld
    │                       └── HelloWorld.java
    └── test
        └── java
            └── com
                └── juvenxu
                    └── mvnbook
                        └── helloworld
                            └── HelloWorldTest.java

二、pom文件内容

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

[root@361way.com hello-world]# cat pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.juvenxu.mvnbook</groupId>
  <artifactId>hello-world</artifactId>         //包的名称
  <version>1.0-SNAPSHOT</version>             //包的版本号,生成jar包为hello-wold-1.0-SNAPSHOT.jar
  <name>Maven Hello World Project</name>
  <dependencies>
    <dependency>
          <groupId>junit</groupId>   //依赖测试包相关信息
          <artifactId>junit</artifactId>
          <version>4.7</version>
          <scope>test</scope>
    </dependency>
  </dependencies>
  <build>   //build 编译的两个相关包信息
    <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>1.5</source>
              <target>1.5</target>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>shade</goal>
                </goals>
                <configuration>
                  <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>  //主函数信息
                    </transformer>
                  </transformers>
                </configuration>
              </execution>
            </executions>
          </plugin>
    </plugins>
  </build>
</project> 

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

三、main程序代码

主函数代码内容如下:

[root@361way.com hello-world]# cat src/main/java/com/juvenxu/mvnbook/helloworld/HelloWorld.java
package com.juvenxu.mvnbook.helloworld;
public class HelloWorld {
        public String sayHello()
        {
                return "Hello Maven";
        }
        public static void main(String[] args)
        {
                System.out.print( new HelloWorld().sayHello() );
        }
}

四、test测试程序代码

内容如下:

[root@361way.com hello-world]# cat src/test/java/com/juvenxu/mvnbook/helloworld/HelloWorldTest.java
package com.juvenxu.mvnbook.helloworld;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.juvenxu.mvnbook.helloworld.HelloWorld;
public class HelloWorldTest
{
    @Test
    public void testSayHello()
    {
        HelloWorld helloWorld = new HelloWorld();
        String result = helloWorld.sayHello();
        assertEquals( "Hello Maven", result );
    }

五、编辑执行

需要注意的是,执行完在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

[root@361way.com hello-world]# mvn clean package
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.juvenxu.mvnbook:hello-world:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 21, column 14
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/ch-3/hello-world/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /tmp/ch-3/hello-world/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /tmp/ch-3/hello-world/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /tmp/ch-3/hello-world/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: /tmp/ch-3/hello-world/target/surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] Building jar: /tmp/ch-3/hello-world/target/hello-world-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ hello-world ---
[INFO] Replacing original artifact with shaded artifact.
[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
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.374 s
[INFO] Finished at: 2014-03-16T21:47:16+08:00
[INFO] Final Memory: 12M/30M
[INFO] ------------------------------------------------------------------------

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

[root@361way.com hello-world]# ll
total 12
-rw------- 1 root root 1683 Aug 26  2010 pom.xml
drwx------ 4 root root 4096 Oct 10  2009 src
drwxr-xr-x 7 root root 4096 Dec  6 21:47 target
[root@361way.com hello-world]# tree target/
target/
├── classes
│   └── com
│       └── juvenxu
│           └── mvnbook
│               └── helloworld
│                   └── HelloWorld.class
├── hello-world-1.0-SNAPSHOT.jar
├── maven-archiver
│   └── pom.properties
├── maven-status
│   └── maven-compiler-plugin
│       ├── compile
│       │   └── default-compile
│       │       ├── createdFiles.lst
│       │       └── inputFiles.lst
│       └── testCompile
│           └── default-testCompile
│               ├── createdFiles.lst
│               └── inputFiles.lst
├── original-hello-world-1.0-SNAPSHOT.jar
├── surefire-reports
│   ├── com.juvenxu.mvnbook.helloworld.HelloWorldTest.txt
│   └── TEST-com.juvenxu.mvnbook.helloworld.HelloWorldTest.xml
└── test-classes
    └── com
        └── juvenxu
            └── mvnbook
                └── helloworld
                    └── HelloWorldTest.class
18 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




本站的发展离不开您的资助,金额随意,欢迎来赏!

You can donate through PayPal.
My paypal id: itybku@139.com
Paypal page: https://www.paypal.me/361way

分类: 协同敏捷自动化 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.