maven(3)maven3.3.9使用入门

maven(3)maven3.3.9使用入门

10274人阅读 评论(0) 收藏 举报
分类:
【0】README
1)maven 安装
step1)检查 jdk  是否安装且 环境变量 JAVA_HOME 是否设置;
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 文件如下:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  5.                             http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   
  7.     <modelVersion>4.0.0</modelVersion>  
  8.     <groupId>com.maven.chapter3</groupId>  
  9.     <artifactId>service</artifactId>  
  10.     <version>1.0-SNAPSHOT</version>  
  11.     <name>service says hello maven.</name>    
  12. </project>  
<?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的话,则其 坐标定义如下:
  1. <groupId>junit</groupId>  
  2. <artifactId>junit</artifactId>  
  3. <version>4.7</version>  
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
(t3)
(t4)

【2】编写主代码
1)Hello.java 代码如下:
  1. package com.maven.chapter3.service;  
  2.   
  3. public class Hello {  
  4.       
  5.     public String sayHello() {  
  6.         return "hello maven.";  
  7.     }  
  8.       
  9.     public static void main(String[] args) {  
  10.         System.out.println((new Hello()).sayHello());  
  11.     }  
  12. }  
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)
对以上console info的分析(Analysis):
A1)以上命令所进行的任务有: clean:clean, resources:resource, compiler:compile;
A2)clean 告诉 maven 清理输出目录 target/;compile 编译项目主代码;resources:resources 任务(未定义项目资源,略过);


【3】编写测试代码(测试用例)
1)HelloTest.java 源码如下:
  1. package com.maven.chapter3.service;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.junit.Test;  
  6.   
  7. public class HelloTest {  
  8.       
  9.     @Test  
  10.     public void testSayHello() {  
  11.         Hello hello = new Hello();  
  12.           
  13.         String result = hello.sayHello();  
  14.         assertEquals("hello maven.", result);  
  15.     }  
  16. }  
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文件如下:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  5.                             http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   
  7.     <modelVersion>4.0.0</modelVersion>  
  8.     <groupId>com.maven.chapter3</groupId>  
  9.     <artifactId>service</artifactId>  
  10.     <version>1.0-SNAPSHOT</version>  
  11.     <name>service says hello maven.</name>  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>junit</groupId>  
  15.             <artifactId>junit</artifactId>  
  16.             <version>4.7</version>  
  17.             <scope>test</scope>  
  18.         </dependency>  
  19.     </dependencies>  
  20. </project>  
<?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 进行打包;
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean package  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building service says hello maven. 1.0-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service --- // clean task.  
  9. [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target  
  10. [INFO]  
  11. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service --- // resources task.  
  12. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  13. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources  
  14. [INFO]  
  15. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service --- // compile task.  
  16. [INFO] Changes detected - recompiling the module!  
  17. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  18. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes  
  19. [INFO]  
  20. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service --- // testResources task.  
  21. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  22. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources  
  23. [INFO]  
  24. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service --- // testCompile task.  
  25. [INFO] Changes detected - recompiling the module!  
  26. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  27. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes  
  28. [INFO]  
  29. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service --- // test task.  
  30. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports  
  31.   
  32. -------------------------------------------------------  
  33.  T E S T S  
  34. -------------------------------------------------------  
  35. Running com.maven.chapter3.service.HelloTest  
  36. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec  
  37.   
  38. Results :  
  39.   
  40. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  41.   
  42. [INFO]  
  43. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---  
  44. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar  // jar task 负责打包.  
  45. [INFO] ------------------------------------------------------------------------   
  46. [INFO] BUILD SUCCESS // 构建成功.  
  47. [INFO] ------------------------------------------------------------------------  
  48. [INFO] Total time: 3.599 s  
  49. [INFO] Finished at: 2016-06-16T22:18:46+08:00  
  50. [INFO] Final Memory: 17M/168M  
  51. [INFO] ------------------------------------------------------------------------  
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 到本地仓库;
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building service says hello maven. 1.0-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service ---  
  9. [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target  
  10. [INFO]  
  11. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---  
  12. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  13. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources  
  14. [INFO]  
  15. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---  
  16. [INFO] Changes detected - recompiling the module!  
  17. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  18. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes  
  19. [INFO]  
  20. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---  
  21. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  22. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources  
  23. [INFO]  
  24. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---  
  25. [INFO] Changes detected - recompiling the module!  
  26. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  27. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes  
  28. [INFO]  
  29. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---  
  30. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports  
  31.   
  32. -------------------------------------------------------  
  33.  T E S T S  
  34. -------------------------------------------------------  
  35. Running com.maven.chapter3.service.HelloTest  
  36. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec  
  37.   
  38. Results :  
  39.   
  40. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  41.   
  42. [INFO]  
  43. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---  
  44. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar  
  45. [INFO]  
  46. [INFO] --- maven-install-plugin:2.4:install (default-install) @ service ---  
  47. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom  
  48. 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)  
  49. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom  
  50. 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)  
  51. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom  
  52. 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.  
  53. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar  
  54. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar  
  55. 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)  
  56. 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  
  57. [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar to C:\Users\lenovo\.m2\  
  58. ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar  
  59. [INFO] Installing D:\classical_books\java_set\maven_in_action\mycode\chapter3\pom.xml to C:\Users\lenovo\.m2\repository\com\maven\cha  
  60. -SNAPSHOT\service-1.0-SNAPSHOT.pom  
  61. [INFO] ------------------------------------------------------------------------  
  62. [INFO] BUILD SUCCESS  
  63. [INFO] ------------------------------------------------------------------------  
  64. [INFO] Total time: 7.750 s  
  65. [INFO] Finished at: 2016-06-16T22:24:09+08:00  
  66. [INFO] Final Memory: 19M/165M  
  67. [INFO] ------------------------------------------------------------------------  
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,配置该插件如下;
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  
  5.                             http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   
  7.     <modelVersion>4.0.0</modelVersion>  
  8.     <groupId>com.maven.chapter3</groupId>  
  9.     <artifactId>service</artifactId>  
  10.     <version>1.0-SNAPSHOT</version>  
  11.     <name>service says hello maven.</name>  
  12.     <dependencies>  
  13.         <dependency>  
  14.             <groupId>junit</groupId>  
  15.             <artifactId>junit</artifactId>  
  16.             <version>4.7</version>  
  17.             <scope>test</scope>  
  18.         </dependency>  
  19.     </dependencies>  
  20.     <build>  
  21.         <plugins> <!-- maven-shade-plugin configuration -->  
  22.           <plugin>  
  23.             <groupId>org.apache.maven.plugins</groupId>  
  24.             <artifactId>maven-compiler-plugin</artifactId>  
  25.             <configuration>  
  26.               <source>1.5</source>  
  27.               <target>1.5</target>  
  28.             </configuration>  
  29.           </plugin>  
  30.           <plugin>  
  31.             <groupId>org.apache.maven.plugins</groupId>  
  32.             <artifactId>maven-shade-plugin</artifactId>  
  33.             <version>1.2.1</version>  
  34.             <executions>  
  35.               <execution>  
  36.                 <phase>package</phase>  
  37.                 <goals>  
  38.                   <goal>shade</goal>  
  39.                 </goals>  
  40.                 <configuration>  
  41.                   <transformers>  
  42.                     <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  43.                       <mainClass>com.maven.chapter3.service.Hello</mainClass>  
  44.                     </transformer>  
  45.                   </transformers>  
  46.                 </configuration>  
  47.               </execution>  
  48.             </executions>  
  49.           </plugin>  
  50.         </plugins>  
  51.   </build>  
  52. </project>  
<?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 并查看目录树
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>mvn clean install  // 执行 mvn clean install  
  2. [INFO] Scanning for projects...  
  3. [WARNING]  
  4. [WARNING] Some problems were encountered while building the effective model for com.maven.chapter3:service:jar:1.0-SNAPSHOT  
  5. [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 22, column 15  
  6. [WARNING]  
  7. [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.  
  8. [WARNING]  
  9. [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.  
  10. [WARNING]  
  11. [INFO]  
  12. [INFO] ------------------------------------------------------------------------  
  13. [INFO] Building service says hello maven. 1.0-SNAPSHOT  
  14. [INFO] ------------------------------------------------------------------------  
  15. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.pom  
  16. 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)  
  17. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-plugins/13/maven-plugins-13.pom  
  18. 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)  
  19. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-shade-plugin/1.2.1/maven-shade-plugin-1.2.1.jar  
  20. 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  
  21. )  
  22. [INFO]  
  23. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ service ---  
  24. [INFO] Deleting D:\classical_books\java_set\maven_in_action\mycode\chapter3\target  
  25. [INFO]  
  26. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ service ---  
  27. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  28. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\main\resources  
  29. [INFO]  
  30. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ service ---  
  31. [INFO] Changes detected - recompiling the module!  
  32. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  33. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\classes  
  34. [INFO]  
  35. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ service ---  
  36. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!  
  37. [INFO] skip non existing resourceDirectory D:\classical_books\java_set\maven_in_action\mycode\chapter3\src\test\resources  
  38. [INFO]  
  39. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ service ---  
  40. [INFO] Changes detected - recompiling the module!  
  41. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!  
  42. [INFO] Compiling 1 source file to D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\test-classes  
  43. [INFO]  
  44. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ service ---  
  45. [INFO] Surefire report directory: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\surefire-reports  
  46.   
  47. -------------------------------------------------------  
  48.  T E S T S  
  49. -------------------------------------------------------  
  50. Running com.maven.chapter3.service.HelloTest  
  51. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.045 sec  
  52.   
  53. Results :  
  54.   
  55. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  56.   
  57. [INFO]  
  58. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ service ---  
  59. [INFO] Building jar: D:\classical_books\java_set\maven_in_action\mycode\chapter3\target\service-1.0-SNAPSHOT.jar  
  60. [INFO]  
  61. [INFO] --- maven-shade-plugin:1.2.1:shade (default) @ service ---  
  62. Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom  
  63. Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.pom (436 B at 1.0 KB/sec)  
  64. Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom  
  65. Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.pom (425 B at 1.1 KB/sec)  
  66. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.pom  
  67. 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)  
  68. Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/1.5.5/plexus-utils-1.5.5.jar  
  69. Downloading: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar  
  70. Downloading: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar  
  71. Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-dependency-tree/1.1/maven-dependency-tree-1.1.jar  
  72. Downloaded: https://repo.maven.apache.org/maven2/asm/asm-commons/3.1/asm-commons-3.1.jar (32 KB at 72.6 KB/sec)  
  73. Downloaded: https://repo.maven.apache.org/maven2/asm/asm-tree/3.1/asm-tree-3.1.jar (22 KB at 17.1 KB/sec)  
  74. 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  
  75. )  
  76. 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)  
  77. [INFO] Replacing original artifact with shaded artifact.  
  78. [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  
  79. _action\mycode\chapter3\target\service-1.0-SNAPSHOT-shaded.jar  
  80. [INFO]  
  81. [INFO] --- maven-install-plugin:2.4:install (default-install) @ service ---  
  82. [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  
  83. ven\chapter3\service\1.0-SNAPSHOT\service-1.0-SNAPSHOT.jar  
  84. [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  
  85. -SNAPSHOT\service-1.0-SNAPSHOT.pom  
  86. [INFO] ------------------------------------------------------------------------  
  87. [INFO] BUILD SUCCESS  
  88. [INFO] ------------------------------------------------------------------------  
  89. [INFO] Total time: 11.789 s  
  90. [INFO] Finished at: 2016-06-17T08:51:15+08:00  
  91. [INFO] Final Memory: 20M/172M  
  92. [INFO] ------------------------------------------------------------------------  
  93.   
  94. D:\classical_books\java_set\maven_in_action\mycode\chapter3>tree /f  // 查看目录树.  
  95. 卷 软件 的文件夹 PATH 列表  
  96. 卷序列号为 0006-7799  
  97. D:.  
  98. │  filelist.txt  
  99. │  pom.xml  
  100. │  pom.xml.bak  
  101. │  
  102. ├─src  
  103. │  ├─main  
  104. │  │  └─java  
  105. │  │      └─com  
  106. │  │          └─maven  
  107. │  │              └─chapter3  
  108. │  │                  └─service  
  109. │  │                          Hello.java  
  110. │  │  
  111. │  └─test  
  112. │      └─java  
  113. │          └─com  
  114. │              └─maven  
  115. │                  └─chapter3  
  116. │                      └─service  
  117. │                              HelloTest.java  
  118. │  
  119. └─target  
  120.     │  original-service-1.0-SNAPSHOT.jar  
  121.     │  service-1.0-SNAPSHOT.jar  
  122.     │  
  123.     ├─classes  
  124.     │  └─com  
  125.     │      └─maven  
  126.     │          └─chapter3  
  127.     │              └─service  
  128.     │                      Hello.class  
  129.     │  
  130.     ├─maven-archiver  
  131.     │      pom.properties  
  132.     │  
  133.     ├─maven-status  
  134.     │  └─maven-compiler-plugin  
  135.     │      ├─compile  
  136.     │      │  └─default-compile  
  137.     │      │          createdFiles.lst  
  138.     │      │          inputFiles.lst  
  139.     │      │  
  140.     │      └─testCompile  
  141.     │          └─default-testCompile  
  142.     │                  createdFiles.lst  
  143.     │                  inputFiles.lst  
  144.     │  
  145.     ├─surefire-reports  
  146.     │      com.maven.chapter3.service.HelloTest.txt  
  147.     │      TEST-com.maven.chapter3.service.HelloTest.xml  
  148.     │  
  149.     └─test-classes  
  150.         └─com  
  151.             └─maven  
  152.                 └─chapter3  
  153.                     └─service  
  154.                             HelloTest.class  
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,可以看到如下信息:
  1. Manifest-Version: 1.0  
  2. Archiver-Version: Plexus Archiver  
  3. Built-By: lenovo  
  4. Created-By: Apache Maven 3.3.9  
  5. Build-Jdk: 1.8.0_60  
  6. Main-Class: com.maven.chapter3.service.Hello  // highlight line.  
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  文件
  1. D:\classical_books\java_set\maven_in_action\mycode\chapter3>java -jar target\service-1.0-SNAPSHOT.jar  
  2. hello maven. // 执行结果.   
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)骨架生成后的文件树
  1. D:\classical_books\java_set\maven_in_action\mycode\archetype_test>tree /f  
  2. 卷 软件 的文件夹 PATH 列表  
  3. 卷序列号为 0006-7799  
  4. D:.  
  5. └─service  
  6.     │  pom.xml  
  7.     │  
  8.     └─src  
  9.         ├─main  
  10.         │  └─java  
  11.         │      └─com  
  12.         │          └─maven  
  13.         │              └─chapter3  
  14.         │                  └─service  
  15.         │                          App.java  
  16.         │  
  17.         └─test  
  18.             └─java  
  19.                 └─com  
  20.                     └─maven  
  21.                         └─chapter3  
  22.                             └─service  
  23.                                     AppTest.java  
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)

0
0
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值