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>

(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)

对以上console info的分析(Analysis):

A1)以上命令所进行的任务有: clean:clean, resources:resource, compiler:compile;

A2)clean 告诉 maven 清理输出目录 target/;compile 编译项目主代码;resources:resources 任务(未定义项目资源,略过);

【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\.m2ven\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)

时间: 2024-10-13 22:53:41

maven(3)maven3.3.9使用入门的相关文章

Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例【附详细代码】

http://blog.csdn.net/xiefu5hh/article/details/51707529 Spark+ECLIPSE+JAVA+MAVEN windows开发环境搭建及入门实例[附详细代码] 标签: SparkECLIPSEJAVAMAVENwindows 2016-06-18 22:35 405人阅读 评论(0) 收藏 举报  分类: spark(5)  版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 前言 本文旨在记录初学Spark时,根据官网快速

Maven学习系列一5分钟入门教程

Maven是基于项目对象模型的一个Apache开源项目.Maven的主要配置文件pom.xml就是项目模型的意思(Project Oriented Model),它是Apache Ant的衍生扩展. 安装 Maven是一个Java工具,所以你必须首先安装Java所需运行环境. 1.解压apache-maven-3.2.1-bin.zip到你想要安装maven的目录,例如,我解压文件到D:\apache-maven-3.2.1. 2.添加M2_HOME环境变量,(WinKey+Pause)快捷键调

《Maven实战》笔记-1-Maven使用入门

<Maven实战>徐晓斌 2011 机械工业出版社 一.介绍 1.名词 artifact:插件 极限编程XP 2.构建脚本: maven--pom.xml(Project Object Model) ant--build.xml 其基本结构是目标(target).依赖(depends),以及实现目标的任务. 3.maven vs ant Ant是过程式的,开发者显示指定每一个目标,以及完成该目标所需要执行的任务. Maven是声明式的,项目构建过程和过程各个阶段所需的工作都由插件实现. 4.相

maven学习(上)- 基本入门用法

一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本文中是C:\Java\maven-3.1.1) 2.1 配置环境变量 系统环境变量里,添加MAVEN_HOME(或M2_HOME),其值为C:\Java\maven-3.1.1,然后PATH环境变量最后附加上";%MAVEN_HOME%\bin" 检测方法: a) 重新进入命令行(DOS窗

maven学习(上)- 基本入门

好文章默默收藏.如若侵权,请联系删除. 原文博主:菩提树下的杨过  原文地址:https://www.cnblogs.com/yjmyzz/p/3495762.html 一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本文中是C:\Java\maven-3.1.1) 2.1 配置环境变量 系统环境变量里,添加MAVEN_HOME(或M2_HOME)

使用idea maven开发spring boot 分布式开发入门

1:使用idea创建一个maven工程 bos2 2:删除bos2的src 文件夹,向pom.xml文件 中添加版本号 <packaging>pom</packaging> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.

Maven3路程(二)Eclipse集成Maven

我的环境: Eclipse:eclipse-jee-juno-SR2-win32 Maven:Maven3.0.5 1.Help->Eclipse Marketplace 2.选中要安装的插件,Next 3.选中I accept ...后,点Finish后,出现 Install Stoftware等待下载安装. 4.等待安装完成后,重启Eclipse后就可以看到安装完成 5.Eclipse中New->Other 弹出新建对话框后显示可以创建Maven项目 6.Eclipse中,Windows-

Maven快速入门使用

1. Maven 介绍 1.1 为什么使用 Maven 由于 Java 的生态非常丰富,无论你想实现什么功能,都能找到对应的工具类,这些工具类都是以 jar 包的形式出现的,例如 Spring,SpringMVC.MyBatis.数据库驱动,等等,都是以 jar 包的形式出现的,jar 包之间会有关联,在使用一个依赖之前,还需要确定这个依赖所依赖的其他依赖,所以,当项目比较大的时候,依赖管理会变得非常麻烦臃肿,这是 Maven 解决的第一个问题. Maven 还可以处理多模块项目.简单的项目,单

maven入门基础:远程仓库的配置和认证(二)

一. 远程仓库的配置 1. 配置方法一:在当前项目的POM.xml文件中配置(不推荐,当maven项目多时,比较麻烦) 路径:D:\program\IntelliJ IDEA 2019.1.3\lib\maven-model-builder-3.3.9\org\apache\maven\model\pom-4.0.0.xml <repositories> <repository> <id>central</id> #central:中央仓库唯一标识 <