Maven實戰(1)
本文的目的:
- 瞭解Maven構建工具的基本思想
- maven的基本命令
- maven-shade-plugin的作用
- 如何在Eclipse中使用
- 安裝maven,新建helloworld作爲示例;
- 注意groupId,artifactId代表的含義;
- clean, compile, test, package, install 命令。這樣運行mvn package命令後會跑測試用例,然後打包工程文件到jar,但是不能運行,因爲Manifest中沒有mainclass;
- Maven-shade-plugin的作用是爲了生成“uber jar”包, 爲了測試打包後的效果,這裏使用了Commons-codec庫作爲演示。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>
<build>
<plugins>
<!-- Maven Shade Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.vonzhou.helloworld.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<groupId>com.vonzhou</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 使用Codec庫 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
</dependencies>
</project>
5.運行mvn package之後會得到兩個jar:helloworld-0.0.1-SNAPSHOT,original-helloworld-0.0.1-SNAPSHOT,前者會包含所有依賴的class,並且根據配置會定製manifest(有Main-Class,如下圖);後者僅僅是本工程的class文件。