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.0http://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 {@Testpublic 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.0http://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 secResults :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 secResults :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.0http://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 secResults :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└─serviceHelloTest.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└─serviceAppTest.java
Attention)
A1)要将某项目生成骨架,这之前要安装该项目到本地仓库;
A2)退出当前项目文件,即建立一个空文件夹,然后再进入到该空文件夹 生成骨架(mvn archetype:generate)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331100.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

TCP接入层的负载均衡、高可用、扩展性架构

转载自 TCP接入层的负载均衡、高可用、扩展性架构 一、web-server的负载均衡 互联网架构中&#xff0c;web-server接入一般使用nginx来做反向代理&#xff0c;实施负载均衡。整个架构分三层&#xff1a; 上游调用层&#xff0c;一般是browser或者APP 中间反向代理层&#xff…

使用poi统计工作职责

1 创建一个新的sheet工作页 Sheet job workbook.createSheet("工作职责统计"); 2 查询工作职责问题选项列表&#xff0c;并设置第一行倒出时间 List<Syslistconfig> listconfigs syslistconfigDao.listConfig(29); //工作职责问题选项列表job.createRow(0)…

漫画:什么是字典序算法

转载自 漫画&#xff1a;什么是字典序算法&#xff1f;算法题目&#xff1a; 给定一个正整数&#xff0c;实现一个方法来求出离该整数最近的大于自身的“换位数”。 什么是换位数呢&#xff1f;就是把一个整数各个数位的数字进行全排列&#xff0c;从而得到新的整数。例如53241…

mybatis_user_guide(2)mybatis3.4.0快速入门

【0】README0&#xff09;以下部分内容转自&#xff1a;“mybatis v.3.4.0 User Guide”&#xff1b;1&#xff09;本文旨在梳理 如何 构建 mybatis 环境&#xff0c;与 db 连接&#xff0c;且采用 JUnit 搭建其测试用例&#xff1b;2&#xff09;本文的环境配置都是基于纯 my…

jQuery中的几个案例:隔行变色、复选框全选和全不选

1 表格隔行变色 1 技术分析&#xff1a; 1 &#xff09;基本过滤选择器&#xff1a; odd: even: 2 &#xff09;jq添加和移除样式&#xff1a; addClass(); removeClass(); 2 代码实现 <script src"js/jquery1.11.3/jquery.min.js" type"text/javasc…

从 Linux 源码看 Socket 的阻塞和非阻塞

转载自 从 Linux 源码看 Socket 的阻塞和非阻塞笔者一直觉得如果能知道从应用到框架再到操作系统的每一处代码&#xff0c;是一件Exciting的事情。大部分高性能网络框架采用的是非阻塞模式。笔者这次就从linux源码的角度来阐述socket阻塞(block)和非阻塞(non_block)的区别。 本…

pojo和javabean的区别

【0】README 1&#xff09;本文转自&#xff1a; http://wenku.baidu.com/view/eba89bbcf121dd36a32d828a.html 【1】正文如下&#xff1a; POJO 和JavaBean是我们常见的两个关键字&#xff0c;一般容易混淆&#xff0c;POJO全称是Plain Ordinary Java Object / Pure Old Jav…

使用poi调整字体格式、添加单元格注释、自动调整列宽

1 创建新的工作铺 import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apa…

MySQL 的索引是什么?怎么优化?

转载自 MySQL 的索引是什么&#xff1f;怎么优化&#xff1f; 摘要: 索引对大数据的查询速度的提升是非常大的&#xff0c;Explain可以帮你分析SQL语句是否用到相关索引。 索引类似大学图书馆建书目索引&#xff0c;可以提高数据检索的效率&#xff0c;降低数据库的IO成本。My…

maven(5)坐标和依赖

【0】README1&#xff09;本文部分文字转自 “maven实战”&#xff0c;旨在 review “maven(5)坐标和依赖” 的相关知识&#xff1b;【2】坐标详解 1&#xff09;intro&#xff1a;坐标用于定位 类库&#xff0c;而一组maven 坐标通过一些元素来进行定义的&#xff1a;groupId…

poi中文api文档

POI中文API文档 一、 POI简介 Apache POI是Apache软件基金会的开放源码函式库&#xff0c;POI提供API给Java程序对Microsoft Office格式档案读和写的功能。二、 HSSF概况 HSSF 是Horrible SpreadSheet Format的缩写&#xff0c;通过HSSF&#xff0c;你可以用纯Java代码来读取、…

聊聊MyBatis缓存机制

转载自 聊聊MyBatis缓存机制前言MyBatis是常见的Java数据库访问层框架。在日常工作中&#xff0c;开发人员多数情况下是使用MyBatis的默认缓存配置&#xff0c;但是MyBatis缓存机制有一些不足之处&#xff0c;在使用中容易引起脏数据&#xff0c;形成一些潜在的隐患。个人在业务…

maven(6)仓库

【0】README1&#xff09;本文部分文字转自 “maven实战”&#xff0c;旨在 review “maven(6)仓库” 的相关知识&#xff1b; 【1】何为 Maven仓库1&#xff09;intro to 构件&#xff1a;在maven中&#xff0c;任何一个依赖&#xff0c;插件或者项目构建的输出&#xff0c;都…

防止用户重复提交表单数据,session方式,js方式

1. 使用session的方式创建Token令牌解决 创建一个生成令牌的工具类&#xff0c;在该类中有返回类的对象&#xff0c;生成token的方法public class TokenUtil {/**单例设计模式&#xff08;保证类的对象在内存中只有一个&#xff09;*1、把类的构造函数私有*2、自己创建一个类的…

mybatis_user_guide(3)XML配置

【-1】README1&#xff09;本文全文总结于 http://www.mybatis.org/mybatis-3/zh/configuration.html#environments【0】MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置&#xff08;settings&#xff09;和属性&#xff08;properties&#xff09;信息。文档的顶层结构如…

easyUI 运用窗口和form表单制作导出功能

这里运用到easyUI的窗口模式和form表单的提交制作一个有条件的导出excel数据统计的功能&#xff0c;主要是知道了怎么运用easyUI的窗口和表单 jsp中&#xff1a;<!-- 导出数据来源条件窗口 --><div id"exportSign" ><form id"condition" me…

分布式一致性算法:可能比你想象得更复杂

转载自 分布式一致性算法&#xff1a;可能比你想象得更复杂分布式系统的难题张大胖遇到了一个难题。他们公司的有个服务器&#xff0c;上面保存着宝贵的数据&#xff0c;领导Bill 为了防止它挂掉&#xff0c; 要求张大胖想想办法把数据做备份。张大胖发挥了抽象的能力&#xff…

mybatis_user_guide(4) Mapper XML 文件

【-1】README 1&#xff09;本文全文总结于 http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html【0】SQL 映射文件有很少的几个顶级元素&#xff08;按照它们应该被定义的顺序&#xff09;&#xff1a; cache – 给定命名空间的缓存配置。cache-ref – 其他命名空间缓存配置…

Hibernate关联关系配置(一对多、一对一和多对多)

第一种关联关系&#xff1a;一对多&#xff08;多对一&#xff09; “一对多”是最普遍的映射关系&#xff0c;简单来讲就如消费者与订单的关系。 一对多&#xff1a;从消费者角的度来说一个消费者可以有多个订单&#xff0c;即为一对多。 多对一&#xff1a;从订单的角度来…

3分钟了解“关联规则”推荐

转载自 3分钟了解“关联规则”推荐 “把啤酒放在尿布旁&#xff0c;有助于提升啤酒销售量”是关联规则推荐的经典案例&#xff0c;今天&#xff0c;和大家聊聊“关联规则推荐”&#xff0c;正文不含任何公式&#xff0c;保证PM弄懂。一、概念 什么是关联规则&#xff08;Associ…