maven(3)maven3.3.9使用入门

【0】README
1)maven 安装
step1)检查 jdk  是否安装且 环境变量 JAVA_HOME 是否设置;
step2)download maven: https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/
step3)解压,同样添加maven的环境变量M2_HOME(maven_home/bin 所在目录);
step4)mvn -n 测试 maven 安装是否正确;

2)相关概念补充(Supplement)
s1)构建:我们每天有相当一部分时间花在了编译,运行单元测试,生成文档,打包和部署等繁琐且不起眼的工作上,这叫做构建;
s2)maven 通过一个坐标系统准确地定位每一个构建(jar文件):也就是通过一组坐标 maven 能够找到任何一个 java 类库(如jar文件);(干货——引入了坐标+定位)

3)maven的中央仓库和本地仓库
3.1)中央仓库:maven 为全球的 java 开发者提供了一个免费的中央仓库(http://repo1.maven.org/maven2/),在其中集合可以找到任何的流行开源类库;(干货——引入了中央仓库,当然与之对应的还有本地仓库)
3.2)本地仓库:用户目录/.m2/repository/ (用户目录如 C:\Users\lenovo) ;
(t2)

4)配置用户范围 settings.xml
4.1)大多数用户需要将 M2_HOME/conf/settings.xml(dir1) copy 到 本地仓库的父目录下(dir2)(即它们是兄弟,如上图所示)
4.2)reasons:
reason1)dir1 是全局配置,该机子上的所有用户都要受到该配置的影响,而dir2 是用户范围的,只对当前用户(lenovo)产生影响;
reason2)配置用户范围 settting.xml 文件还便于maven升级。直接修改 conf/settings.xml 文件 会导致 maven 升级不便;
Attention)
A1)maven 能够帮助我们 清理,编译,测试,打包,部署,然后得到 final products;
A2)通过 其 衍生工具 Nexus,我们还能对开源类库(如jar)进行快速地搜索;

【1】编写POM
1)Hello的 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.maven.chapter3</groupId>
	<artifactId>service</artifactId>
	<version>1.0-SNAPSHOT</version>
	<name>service says hello maven.</name>	
</project>
对以上代码的分析(Analysis):
A1)project:是所有 pom.xml 的根元素,它声明了一些 POM 相关的命名空间和 xsd 元素;
A2)modelVersion: 指定了当前 pom 模型的版本,对于 maven 3来说,它只能是4.0.0
A3)groupId:定义了项目输入那个组;
A4)artifactId:定义了当前maven项目在组中唯一的id号码;
A5)version:指定了Hello 项目当前的版本;
A6)name:声明了一个对于用户更为友好的项目名称;
Attention)
A1)要知道, groupId + artifactId  + version 组成了一个坐标,当然你也看做是 三维坐标用来定位 所需 类库的位置;(干货——坐标==groupId + artifactId  + version,坐标的作用是定位 依赖类库的位置)
A2)看个荔枝:以junit为例,其jar list 如下所示;如果要用 Junit4.7的话,则其 坐标定义如下:
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
maven(3)maven3.3.9使用入门_第1张图片(t3)
(t4)

【2】编写主代码
1)Hello.java 代码如下:
package com.maven.chapter3.service;

public class Hello {
	
	public String sayHello() {
		return "hello maven.";
	}
	
	public static void main(String[] args) {
		System.out.println((new Hello()).sayHello());
	}
}
2)关于主代码有两点注意(Attention):
A1)在 大多数case下:应该把项目主代码放到 src/main/java 目录下;maven 会自动搜寻该目录找到项目主代码;
A2)该java类的包名是 com.maven.chapter3.service:这与之前在 pom 中定义的 groupId 和 artifactID 要吻合;
3)代码编写完毕后,使用maven进行编译(mvn clean compile)
maven(3)maven3.3.9使用入门_第2张图片
对以上console info的分析(Analysis):
A1)以上命令所进行的任务有: clean:clean, resources:resource, compiler:compile;
A2)clean 告诉 maven 清理输出目录 target/;compile 编译项目主代码;resources:resources 任务(未定义项目资源,略过);
maven(3)maven3.3.9使用入门_第3张图片

【3】编写测试代码(测试用例)
1)HelloTest.java 源码如下:
package com.maven.chapter3.service;

import static org.junit.Assert.*;

import org.junit.Test;

public class HelloTest {
	
	@Test
	public void testSayHello() {
		Hello hello = new Hello();
		
		String result = hello.sayHello();
		assertEquals("hello maven.", result);
	}
}
2)关于测试代码有两点注意(Attention):
A1)在 大多数case下:应该把测试代码放到 src/test/java 目录下;maven 会自动搜寻该目录找到项目测试代码;
A2)测试代码需要依赖 Junit,添加对JUnit的依赖,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.maven.chapter3</groupId>
	<artifactId>service</artifactId>
	<version>1.0-SNAPSHOT</version>
	<name>service says hello maven.</name>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>
对以上代码的分析(Analysis): 
A1)以上xml文件有 scope元素,scope表示依赖范围;
A2)依赖范围scope==test:则表明该依赖只对测试有效;换句话说,测试代码中 import JUnit 代码是没有问题的,但如果在主代码中 import JUnit,就会造成编译错误;
A3)依赖范围(default)scope==compile:如果不声明依赖访问,默认是compile,这样表示该依赖对主代码和测试代码都有效;

3)调用maven执行测试(mvn clean test)

【4】打包和运行
1)intro: 简单地执行命令 mvn clean package 进行打包;
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building service says hello maven. 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service --- // clean task.
[INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- // resources task.
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- // compile task.
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- // testResources task.
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- // testCompile task.
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service --- // test task.
[INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.maven.chapter3.service.HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---
[INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar  // jar task 负责打包.
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS // 构建成功.
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.599 s
[INFO] Finished at: 2016-06-16T22:18:46+08:00
[INFO] Final Memory: 17M/168M
[INFO] ------------------------------------------------------------------------

2)如何才能让其他项目直接引用这个 jar呢? 执行mvn clean install 安装 jar 到本地仓库;
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building service says hello maven. 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service ---
[INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---
[INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.maven.chapter3.service.HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---
[INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ service ---
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (3 KB at 1.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (2 KB at 2.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5 KB at 10.
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 KB at 13.7 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (226 KB at 189.1 KB/se
[INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to C:\Users\lenovo\.m2\
ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar
[INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to C:\Users\lenovo\.m2\repository\com\maven\cha
-SNAPSHOT\service-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.750 s
[INFO] Finished at: 2016-06-16T22:24:09+08:00
[INFO] Final Memory: 19M/165M
[INFO] ------------------------------------------------------------------------
3)对执行命令的分析:
A1)maven的命令列表有:mvn clean compile, mvn clean test, mvn clean install;
A2)命令执行顺序: 执行test之前要先执行 compile,执行package 之前要执行test,执行install 之前要执行packge;所以如果 键入 mvn clean install ,则执行顺序是 compile->test->package->install ;

4) 生成可执行的jar 文件,
4.1)默认情况下:打包生成的 jar 是不能够直接运行的,因为带有main 方法的类信息不回添加到 manifest中;
4.2)生成可执行的jar 文件: 需要借助maven-shade-plugin,配置该插件如下;
<?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.maven.chapter3</groupId>
	<artifactId>service</artifactId>
	<version>1.0-SNAPSHOT</version>
	<name>service says hello maven.</name>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.7</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
	    <plugins> <!-- maven-shade-plugin configuration -->
	  	  <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.maven.chapter3.service.Hello</mainClass>
	  	            </transformer>
	  	          </transformers>
	  	        </configuration>
	  	      </execution>
	  	    </executions>
	  	  </plugin>
	    </plugins>
  </build>
</project>
4.3)执行 mvn clean install 并查看目录树
D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install  // 执行 mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15
[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 service says hello maven. 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom (6 KB at 3.1 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom (12 KB at 27.3 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar (67 KB at 112.0 KB/sec
)
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service ---
[INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---
[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 D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---
[INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.maven.chapter3.service.HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 sec

Results :

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

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---
[INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service ---
Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom
Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom (436 B at 1.0 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom
Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom (425 B at 1.1 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom (3 KB at 6.5 KB/sec)
Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar
Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar
Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar
Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar
Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar (32 KB at 72.6 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar (22 KB at 17.1 KB/sec)
Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar (34 KB at 26.4 KB/sec
)
Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar (246 KB at 73.4 KB/sec)
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar with D:\classical_books\java_set\maven_in
_action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ service ---
[INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to C:\Users\lenovo\.m2\repository\com\ma
ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar
[INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to C:\Users\lenovo\.m2\repository\com\maven\chapter3\service\1.0
-SNAPSHOT\service-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.789 s
[INFO] Finished at: 2016-06-17T08:51:15+08:00
[INFO] Final Memory: 20M/172M
[INFO] ------------------------------------------------------------------------

D:\classical_books\java_set\maven_in_action\mycode\chapter3>tree /f  // 查看目录树.
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-7799
D:.
│  filelist.txt
│  pom.xml
│  pom.xml.bak
│
├─src
│  ├─main
│  │  └─java
│  │      └─com
│  │          └─maven
│  │              └─chapter3
│  │                  └─service
│  │                          Hello.java
│  │
│  └─test
│      └─java
│          └─com
│              └─maven
│                  └─chapter3
│                      └─service
│                              HelloTest.java
│
└─target
    │  original-service-1.0-SNAPSHOT.jar
    │  service-1.0-SNAPSHOT.jar
    │
    ├─classes
    │  └─com
    │      └─maven
    │          └─chapter3
    │              └─service
    │                      Hello.class
    │
    ├─maven-archiver
    │      pom.properties
    │
    ├─maven-status
    │  └─maven-compiler-plugin
    │      ├─compile
    │      │  └─default-compile
    │      │          createdFiles.lst
    │      │          inputFiles.lst
    │      │
    │      └─testCompile
    │          └─default-testCompile
    │                  createdFiles.lst
    │                  inputFiles.lst
    │
    ├─surefire-reports
    │      com.maven.chapter3.service.HelloTest.txt
    │      TEST-com.maven.chapter3.service.HelloTest.xml
    │
    └─test-classes
        └─com
            └─maven
                └─chapter3
                    └─service
                            HelloTest.class
对以上目录的分析(Analysis):
A1)打开target,可以看到 有两个jar 文件 original-service-1.0-SNAPSHOT.jar 和  service-1.0-SNAPSHOT.jar;前者是原始的jar, 后者是带有 Main-class 信息的可运行jar;
A2)打开可运行jar(service-1.0-SNAPSHOT.jar) 的 META-INF/MANIFEST.MF,可以看到如下信息:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: lenovo
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_60
Main-Class: com.maven.chapter3.service.Hello  // highlight line.
4.4)执行 service-1.0-SNAPSHOT.jar  文件
D:\classical_books\java_set\maven_in_action\mycode\chapter3>java -jar target\service-1.0-SNAPSHOT.jar
hello maven. // 执行结果. 

【5】使用 Archetype 生成项目骨架
0)intro to Archetype(骨架):我们把maven基本的目录结构和 pom.xml 文件内容(见上述目录树) 称为项目的骨架; (干货——引入骨架)
1)problem + solution
1.1)problem:后续的项目需要用到 Hello 项目,不可能每次都重复上述steps 来构建 Hello 项目;
1.2)solution:使用 maven archetype 来创建该项目的骨架,以便于后续项目引用;

2)生成项目骨架命令:mvn archetype:generate,该命令实际上是运行插件 maven-archetype-plugin;
3)看个荔枝: 为 Hello 项目生成骨架
step1)输入 mvn archetype:generate 生成骨架


step2)骨架生成后的文件树
D:\classical_books\java_set\maven_in_action\mycode\archetype_test>tree /f
卷 软件 的文件夹 PATH 列表
卷序列号为 0006-7799
D:.
└─service
    │  pom.xml
    │
    └─src
        ├─main
        │  └─java
        │      └─com
        │          └─maven
        │              └─chapter3
        │                  └─service
        │                          App.java
        │
        └─test
            └─java
                └─com
                    └─maven
                        └─chapter3
                            └─service
                                    AppTest.java
Attention)
A1)要将某项目生成骨架,这之前要安装该项目到本地仓库;
A2)退出当前项目文件,即建立一个空文件夹,然后再进入到该空文件夹 生成骨架(mvn archetype:generate)

你可能感兴趣的:(maven(3)maven3.3.9使用入门)