Maven学习之路(一):POM文件

基本概念

POM(Project Object Model,项目对象模型)文件是Maven项目的核心,定义项目基本信息,描述如何构建,声明依赖

第一个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.shannonAJ.mavenStudy</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Maven study project</name>
</project>




  • XML头指定了xml文档的版本和编码方式
  • project元素是根元素,声明了一些POM相关的命名空间和XSD元素
  • 根元素下是Modelversion指定了POM的版本,目前都是4.0.0
  • groupID、artifactId、version是最重要的三行,他们定义了一个基本坐标,Maven中所有Jar、war和POM都是以这个基本坐标来区分
  • groupID定义了项目属于哪个组织,如建立一个maven-study的项目,groupId应该就是com.shannonAJ.maven-study
  • artifactId定义了当前Maven项目在组中的唯一ID,如maven-study
  • version指定了项目的当前版本:1.0-SNAPSHOT,SNAPSHOT代表快照,说明不稳定,处于开发中,RELEASE代表稳定发布版本
  • name元素声明了对于用户更友好的项目名称,推荐声明

编写代码

  • 在工作区新建一个文件夹:maven-study,maven默认Java代码在/src/main/java下,创建该目录,编写文件hello.java

package com.shannonAJ.maven-study.hello-maven;

public class HelloMaven
{
    public String sayHello()
    {
        return "Hello Maven";
    }

    public static void main(String[] args) {
        System.out.println(new HelloMaven().sayHello());
    }

}

  • 在项目根目录下打包
E:\Development\workspace\java-project\hello-maven>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven study project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting E:\Development\workspace\java-project\hello-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.156 s
[INFO] Finished at: 2018-07-21T21:22:34+08:00
[INFO] Final Memory: 13M/165M
[INFO] ------------------------------------------------------------------------

  • Maven首先执行clean:clean,告诉Maven清理输出目录target,默认maven的输出都在target目录下。然后执行resource:resource,接着执行compile:compile,把编译好的文件放到target/classes下
  • clean:clean,resource:resource,compile:compile对应了一些插件的目标

编写测试代码

  • 测试代码位于src/test/java下
  • POM中添加JUnit测试依赖
<?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.shannonAJ.mavenStudy</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Maven study project</name>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
</project>



  • 代码中添加Junit依赖,Maven能自动去中央仓库中下载junit-4.7.jar
  • <scope></scope>标签为依赖范围,依赖范围为test表明该依赖只对测试有效。如果在测试中引入Junit的package没有问题,但在主代码中引入就会报错
  • <scope>不设置的话默认值为compile,表示该依赖对主代码和测试代码都有效
  • 编写测试类

package com.shannonAJ.studyMaven.helloMaven;

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class HelloMavenTest
{
    @Test
    public void testSayHello() {
        HelloMaven hm = new HelloMaven();
        String res = hm.sayHello();
        assertEquals(""Hello Maven", res);
    }
    
}


  • 运行mvn clean test
E:\Development\workspace\java-project\hello-maven>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven study project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting E:\Development\workspace\java-project\hello-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: E:\Development\workspace\java-project\hello-maven\target\surefire-reports
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (2.4 kB at 7.6 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (2.3 kB at 9.8 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 kB at 145 kB/s)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.shannonAJ.studyMaven.helloMaven.HelloMavenTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.891 s
[INFO] Finished at: 2018-07-22T10:37:31+08:00
[INFO] Final Memory: 20M/209M
[INFO] ------------------------------------------------------------------------




  • 可以看到总共经历了clean:clean、resource:resource、compile:compile、resource:testResource、compile:testCompile、surefire:test阶段
  • surefire是Maven中负责执行测试的插件,它运行测试用例,并输出测试报告,显示运行了多少测试、失败了多少、出错了多少、跳过了多少
  • 测试代码通过编译之后在/target/test-classes下生成了二进制文件

打包和运行

  • POM中没有指定打包类型,默认类型为jar,执行mvn clean package进行打包
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven study project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting E:\Development\workspace\java-project\hello-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: E:\Development\workspace\java-project\hello-maven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.shannonAJ.studyMaven.helloMaven.HelloMavenTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.042 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-maven ---
[INFO] Building jar: E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.476 s
[INFO] Finished at: 2018-07-22T18:11:18+08:00
[INFO] Final Memory: 17M/165M
[INFO] ------------------------------------------------------------------------



  • Maven会在打包之前执行compile、resource等操作
  • jar:jar任务负责打包,实际上就是jar插件的jar目标将项目主代码打包成一个名为hello-maven-1.0-SNAPSHOT.jar的文件,该文件也位于/target/输出目录中,它根据artifact-version.jar规则进行命名,还可以使用finalName来自定义该文件的名称
  • 让其他maven项目直接引用这个jar还需要安装步骤
  • 安装mvn clean install
E:\Development\workspace\java-project\hello-maven>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven study project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting E:\Development\workspace\java-project\hello-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: E:\Development\workspace\java-project\hello-maven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.shannonAJ.studyMaven.helloMaven.HelloMavenTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-maven ---
[INFO] Building jar: E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-maven ---
[INFO] Installing E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar to E:\Development\m2\localRepository\com\shannonAJ\mavenStudy\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.jar
[INFO] Installing E:\Development\workspace\java-project\hello-maven\pom.xml to E:\Development\m2\localRepository\com\shannonAJ\mavenStudy\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.707 s
[INFO] Finished at: 2018-07-22T18:19:01+08:00
[INFO] Final Memory: 17M/170M
[INFO] ------------------------------------------------------------------------



  • 打包之后又执行了安装任务install:install,该任务将项目输出的jar安装到了maven本地仓库中,其他的Maven项目才能使用它

生成可执行的jar文件

  • 默认打包生成的jar不能直接运行,因为带有main方法的类信息不会添加到manifest中,打开jar文件中的META-INF/MANIFEST.MF文件,无法看到Main-Class一行
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: andy-
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_131



  • 借助maven-shade-plugin,配置该插件
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.shannonAJ.mavenStudy.helloMaven.HelloMaven</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



E:\Development\workspace\java-project\hello-maven>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven study project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-shade-plugin/2.4.3/maven-shade-plugin-2.4.3.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-shade-plugin/2.4.3/maven-shade-plugin-2.4.3.pom (8.5 kB at 23 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-shade-plugin/2.4.3/maven-shade-plugin-2.4.3.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/plugins/maven-shade-plugin/2.4.3/maven-shade-plugin-2.4.3.jar (104 kB at 640 kB/s)
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-maven ---
[INFO] Deleting E:\Development\workspace\java-project\hello-maven\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-maven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\Development\workspace\java-project\hello-maven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Development\workspace\java-project\hello-maven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-maven ---
[INFO] Surefire report directory: E:\Development\workspace\java-project\hello-maven\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.shannonAJ.studyMaven.helloMaven.HelloMavenTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-maven ---
[INFO] Building jar: E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.4.3:shade (default) @ hello-maven ---
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-compat/3.0/maven-compat-3.0.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-compat/3.0/maven-compat-3.0.pom (4.0 kB at 14 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/1.0-beta-6/wagon-provider-api-1.0-beta-6.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/1.0-beta-6/wagon-provider-api-1.0-beta-6.pom (1.8 kB at 13 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon/1.0-beta-6/wagon-1.0-beta-6.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon/1.0-beta-6/wagon-1.0-beta-6.pom (12 kB at 31 kB/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/jdom/jdom/1.1/jdom-1.1.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/jdom/jdom/1.1/jdom-1.1.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-dependency-tree/2.2/maven-dependency-tree-2.2.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-dependency-tree/2.2/maven-dependency-tree-2.2.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-shared-components/20/maven-shared-components-20.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-shared-components/20/maven-shared-components-20.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/vafer/jdependency/1.1/jdependency-1.1.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/vafer/jdependency/1.1/jdependency-1.1.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm/5.0.4/asm-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm/5.0.4/asm-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-parent/5.0.4/asm-parent-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-parent/5.0.4/asm-parent-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-analysis/5.0.4/asm-analysis-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-analysis/5.0.4/asm-analysis-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-tree/5.0.4/asm-tree-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-tree/5.0.4/asm-tree-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-commons/5.0.4/asm-commons-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-commons/5.0.4/asm-commons-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-util/5.0.4/asm-util-5.0.4.pom
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-util/5.0.4/asm-util-5.0.4.pom (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-compat/3.0/maven-compat-3.0.jar
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/1.0-beta-6/wagon-provider-api-1.0-beta-6.jar
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-dependency-tree/2.2/maven-dependency-tree-2.2.jar
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/jdom/jdom/1.1/jdom-1.1.jar
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/jdom/jdom/1.1/jdom-1.1.jar (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/vafer/jdependency/1.1/jdependency-1.1.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/shared/maven-dependency-tree/2.2/maven-dependency-tree-2.2.jar (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-analysis/5.0.4/asm-analysis-5.0.4.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/vafer/jdependency/1.1/jdependency-1.1.jar (0 B at 0 B/s)
Downloading: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-util/5.0.4/asm-util-5.0.4.jar
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-analysis/5.0.4/asm-analysis-5.0.4.jar (0 B at 0 B/s)
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/wagon/wagon-provider-api/1.0-beta-6/wagon-provider-api-1.0-beta-6.jar (53 kB at 145 kB/s)
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/codehaus/plexus/plexus-utils/3.0.22/plexus-utils-3.0.22.jar (245 kB at 580 kB/s)
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/ow2/asm/asm-util/5.0.4/asm-util-5.0.4.jar (0 B at 0 B/s)
Downloaded: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-compat/3.0/maven-compat-3.0.jar (285 kB at 650 kB/s)
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar with E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-maven ---
[INFO] Installing E:\Development\workspace\java-project\hello-maven\target\hello-maven-1.0-SNAPSHOT.jar to E:\Development\m2\localRepository\com\shannonAJ\mavenStudy\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.jar
[INFO] Installing E:\Development\workspace\java-project\hello-maven\pom.xml to E:\Development\m2\localRepository\com\shannonAJ\mavenStudy\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.700 s
[INFO] Finished at: 2018-07-22T18:54:54+08:00
[INFO] Final Memory: 21M/173M
[INFO] ------------------------------------------------------------------------


  • 再执行mvn clean install,target下会构建生成两个jar,前者是带有mainclass信息的jar,后者是原始jar


 E:\Development\workspace\java-project\hello-maven\target 的目录

2018/07/22  18:54    <DIR>          .
2018/07/22  18:54    <DIR>          ..
2018/07/22  18:54    <DIR>          classes
2018/07/22  18:54             3,160 hello-maven-1.0-SNAPSHOT.jar
2018/07/22  18:54    <DIR>          maven-archiver
2018/07/22  18:54    <DIR>          maven-status
2018/07/22  18:54             2,906 original-hello-maven-1.0-SNAPSHOT.jar
2018/07/22  18:54    <DIR>          surefire-reports
2018/07/22  18:54    <DIR>          test-classes
               2 个文件          6,066 字节
               7 个目录 231,469,121,536 可用字节

  • 打开hello-maven-1.0-SNAPSHOT.jar的MANIFEST.MF,可以看到包含了MainClass信息
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: andy-
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_131
Main-Class: com.shannonAJ.mavenStudy.helloMaven.HelloMaven

  • 项目根目录执行jar文件,输出main方法结果
E:\Development\workspace\java-project\hello-maven>java -jar target\hello-maven-1.0-SNAPSHOT.jar
Hello Maven

小结

  • 介绍了Maven的主要命令:mvn clean compile\mvn clean test\mvn clean package\mvn clean install,执行顺序compile->test->package->install

参考资料

《Maven实战》
作者:许晓斌
京东链接: https://item.jd.com/10476794.html

    原文作者:ShannonAJ
    原文地址: https://www.jianshu.com/p/0e539050ebe8
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞