一、Maven是什么
1. 优秀的构建工具
我们能快速的从源码库构件出最终产品,只需一写配置,能让Maven帮你清理、编译、测试、打包、部署,然后得到最终产品。
2. 不重复发明轮子
Maven已经有庞大的插件,全世界通用,自己不用谢代码
3. 项目管理工具
项目开发过程中需要调用多种jar包,不同的项目可能用相同的jar包,为统一管理项目jar包,实现多个项目中共享JARs。
二、Maven下载和环境变量配置
1. 官网下载
[apache-maven-3.5.2-bin.zip]
(http://mirror.bit.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.zip)
2. 解压到指定目录
指定目录:C:AppDateapache-maven-3.5.2
3. 配置本机环境变量
环境变量路径:
新建M2_HOME,值:C:AppDateapache-maven-3.5.2
在Path中添加 C:AppDateapache-maven-3.5.2in
2. 解压到指定目录
指定目录:C:AppDateapache-maven-3.5.2
3. 配置本机环境变量
环境变量路径:
新建M2_HOME,值:C:AppDateapache-maven-3.5.2
在Path中添加 C:AppDateapache-maven-3.5.2in
4. CMD验证
打开cmd命令窗口
命令:mvn -v
安装Maven前要配置Java环境
C:Windowssystem32>mvn -v
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00)
Maven home: C:AppDateapache-maven-3.5.2in..
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: C:Program FilesJavajdk1.8.0_151jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
三、Maven使用入门
(一)、Maven约定目录结构及内容
- **约定优与配置 **
Maven约定的Java项目结构,以hello-word为例:
CMD 命令:tree /f
E:Class16_MavenDemohello-word> tree /f
│ pom.xml
│
└─src
├─main
│ └─java
│ HelloWord.java
│
└─test
└─java
HelloWordTest.java
1.pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xusan</groupId>
<artifactId>hello-word</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-word</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.主代码
package com.xusan.helloword;
public class HelloWord {
public String sayHello() {
return "Hello Maven";
}
public static void main(String[] args) {
System.out.println(new HelloWord().sayHello());
}
}
3.测试代码
package com.xusan.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class HelloWordTest {
@Test
public void testSayHello() {
HelloWord helloword = new HelloWord();
String result = helloword.sayHello();
assertEquals("Hello Maven",result);
}
}
(二) Maven常用指令
- mvn -v 或者 mvn -version:验证环境变量
- mvn help:system :打印所有系统属性和环境变量
- mvn compile:编译主源码,但不编译test目录,产生target 文件
- mvn test:运行单元测试
- mvn test-compile:编译测试代码,生成targer在test-classes下
- mvn clean: 删除target 文件夹
- mvn package :打包
- mvn install:安装
1、测试mvn compile
E:Class16_MavenDemohello-word> mvn compile
[INFO] Scanning for projects...
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-word ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:Class16_MavenDemohello-word argetclasses
[INFO] -----------------------------------------------------------------------
[INFO] BUILD SUCCESS
2、测试mvn compile
E:Class16_MavenDemohello-word> mvn test-compile
[INFO] Scanning for projects...
(省略...)
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:Class16_MavenDemohello-word arget est-classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
3、查看最新目录target
E:Class16_MavenDemohello-word> tree /f
E:.
│ pom.xml
│
├─src
│ ├─main
│ │ └─java
│ │ HelloWord.java
│ │
│ └─test
│ └─java
│ HelloWordTest.java
│
└─target
├─classes
│ └─com
│ └─xusan
│ └─helloword
│ HelloWord.class
│
├─maven-status
│ └─maven-compiler-plugin
│ ├─compile
│ │ └─default-compile
│ │ createdFiles.lst
│ │ inputFiles.lst
│ │
│ └─testCompile
│ └─default-testCompile
│ createdFiles.lst
│ inputFiles.lst
│
└─test-classes
└─com
└─xusan
└─helloword
HelloWordTest.class
4、打包和运行
E:Class16_MavenDemohello-word> mvn clean package
[INFO] Scanning for projects...
(省略...)
[INFO] Building jar: E:Class16_MavenDemohello-word argethello-word-1.0-SNAPSHOT.jar
[INFO]
[INFO] BUILD SUCCESS
target下产生一个jar:targethello-word-1.0-SNAPSHOT.jar
5、安装到本地仓库
E:Class16_MavenDemohello-word> mvn install
[INFO] Scanning for projects...
---省略...---
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] Building jar: E:Class16_MavenDemohello-word argethello-word-1.0-SNAPSHOT.jar
[INFO] Installing E:Class16_MavenDemohello-word argethello-word-1.0-SNAPSHOT.jar to C:AppDatemaven
epositorycomxusanhello-word1.0-SNAPSHOThello-word-1.0-SNAPSHOT.jar
---我的本地仓库是:C:AppDatemaven
epository,后面会讲修改本地仓库---
[INFO] Installing E:Class16_MavenDemohello-wordpom.xml to C:AppDatemaven
epositorycomxusanhello-word1.0-SNAPSHOThello-word-1.0-SNAPSHOT.pom
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
6、执行jar
命令:java -jar
E:Class16_MavenDemohello-word> cd . arget
PS E:Class16_MavenDemohello-word arget> java -jar .hello-word-1.0-SNAPSHOT.jar
.hello-word-1.0-SNAPSHOT.jar中没有主清单属性
- 原因:
HelloWord类有main方法,默认打包成jar无法直接运行,带有main方法类信息无法添加到manifest中运行(打开jar文件中的META-INF/MANIFEST.MF文件,无法看到Main-Class一行) - 解决办法:
Maven插件————”Apache Maven Shade Plugin“中的“ManifestResourceTransformer”可以解决问题, 参考——四、POM插件配置
7、mvn install
E:Class16_MavenDemohello-word> mvn install
E:Class16_MavenDemohello-word> cd . arget
E:Class16_MavenDemohello-word arget> java -jar .hello-word-1.0-SNAPSHOT.jar
Hello Maven
输出Hello Maven 代表成功编译。
(三)Archetype生成项目骨架
1. mvn archetype:generate
2000多个骨架供选择,直接回车,默认
2043: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
2044: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
2045: remote -> xyz.luan.generator:xyz-generator (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1108:
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: archetype:generatearchetype:generate
Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
Define value for property 'groupId': com.xusan
Define value for property 'artifactId': helloword
[INFO] Using property: version = 1.0-SNAPSHOT
Define value for property 'package' com.xusan: : com.xusan
Confirm properties configuration:
groupId: com.xusan
artifactId: helloword
version: 1.0-SNAPSHOT
package: com.xusan
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: E:Class16_MavenDemoArchetype
[INFO] Parameter: package, Value: com.xusan
[INFO] Parameter: groupId, Value: com.xusan
[INFO] Parameter: artifactId, Value: helloword
[INFO] Parameter: packageName, Value: com.xusan
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: E:Class16_MavenDemoArchetypehelloword
生成一个新的helloword骨架
E:Class16_MavenDemoArchetype> tree /f
E:.
└─helloword
│ pom.xml
│
└─src
├─main
│ └─java
│ └─com
│ └─xusan
│ App.java
│
└─test
└─java
└─com
└─xusan
AppTest.java
1.生成的main/./App
package com.xusan;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
2.生成的test/./AAppTest
package com.xusan;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}
3.生成的pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xusan</groupId>
<artifactId>helloword</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloword</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.直接编辑命令包含3要素
命令:mvn archetype:generate "-DgroupId=com.xusan.maven01" "-DartifactId=demo-maven01" "-Dversion=1.0-SNAPSHOT" "-Dpackage=com.xusan.maven01"
四、Maven项目核心POM .xml
pom定义了项目的基本信息,描述了项目的构建、依赖
1、Maven项目的基本坐标:
<!-- 项目包名: 公司地址名称反写+项目名称-->
<groupId>com.xusan</groupId>
<!--项目模块名称:一般为 项目名-模块名 -->
<artifactId>hello-word</artifactId>
<!-- 指定当前pom 的版本-->
<version>1.0-SNAPSHOT</version>
2.POM插件配置
(1).查看官方配置文档
官网ManifestResourceTransformer配置
(2).配置pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xusan</groupId>
<artifactId>hello-word</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello-word</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.xusan.helloword.HelloWord</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
只要修改包名即可
<manifestEntries>
<!--改成包名-->
<Main-Class>com.xusan.helloword.HelloWord</Main-Class>
</manifestEntries>