Maven实战(1)

来源:互联网 发布:手机淘宝查看交易快照 编辑:程序博客网 时间:2024/04/30 04:12

Maven实战(1)


本文的目的:

  • 了解Maven构建工具的基本思想
  • maven的基本命令
  • maven-shade-plugin的作用
  • 如何在Eclipse中使用
  1. 安装maven,新建helloworld作为示例;
  2. 注意groupId,artifactId代表的含义;
  3. clean, compile, test, package, install 命令。这样运行mvn package命令后会跑测试用例,然后打包工程文件到jar,但是不能运行,因为Manifest中没有mainclass;
  4. 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文件。
这里写图片描述
这里写图片描述
这里写图片描述

Uber jar
maven-shade插件的作用

0 0
原创粉丝点击