一、编写POM
Maven项目的核心是pom.xml;
POM(Project Object Model,项目对象模型)定义了项目的基本信息,用于描述项目如何构建,声明项目依赖;
POM文件:
<?xml version="1.0" encoding="UTF-8"?> //XML头,指定xml文档的版本和编码方式
<project xmlns="http://maven.apache.org/POM/4.0.0" //project根元素,命名空间,避免命名重复
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0">
<modleVersion>4.0.0</modleVersion> //modleVersion 当前POM模型版本
<groupId>com.juvenxu.mvnbook</groupId> //groupId 项目属于哪个组
<artifactId>hello-world</artifactId> //artifactId 项目在组中的唯一ID
<version>1.0-SNAPSHOT</version> //version 项目当前版本(SNAPSHOT正在开发、不稳定版本)
<name>Maven Hello World Project</name> //name 一个对用户更友好的项目名称
</project>
没有任何Java代码,就能够定义Maven项目的POM,让项目对象模型最大程度地和实际代码相互独立-解耦;
二、编写主代码
项目主代码和测试代码不同,项目主代码会被打包到最终的构建中(如jar),而测试代码只在运行测试时用到;
默认情况下,Maven假设项目主代码位于src/main/java目录中,然后在该目录下创建包名和源文件;
HelloWorld.java:
public class HelloWorld {
public String sayHello() {
return "Hello Maven";
}
public static void main(String[] args) {
System.out.print(new HelloWorld().sayHello());
}
}
在项目根目录下,运行命令mvn clean compile
C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world --- //执行clean:clean任务,删除target/目录
[INFO] Deleting C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world --- //执行resources:resources任务,未定义项目资源
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world --- //执行compile:compile任务,编译项目主代码至target/classes目录
[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 C:\Users\chengxiang.peng.QUNARSERVERS\MavenSou
rces\hello-world\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.466 s
[INFO] Finished at: 2015-05-01T11:54:18+08:00
[INFO] Final Memory: 12M/219M
[INFO] ------------------------------------------------------------------------
三、编写测试代码
Maven项目中默认的测试代码目录是src/test/java;
HelloWorldTest.java:
public class HelloWorldTest{
@Test
public void testSayHello(){
HelloWorld helloWorld = new HelloWorld();
String result = helloWorld.sayHello()
assertEquals( "Hello Maven", result );
}
}
POM文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
… …
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
… …
<name>Maven Hello World Project</name>
<dependencies> //dependencied元素,可以包含多个dependency元素以声明项目的依赖
<dependency>
<groupId>junit</groupId> //添加junit依赖,自动访问中央仓库下载需要的文件
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope> //scop依赖范围,标识该依赖只对测试有效(如果不声明依赖范围,那么默认值就是compile,标识该依
</dependency> //赖对主代码和测试代码都有效)
</dependencies>
</project>
执行mvn clean test
C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Hello World Project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello-world ---
[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 C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello-world --- //compiler:testCompile任务,编译测试代码
[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 C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world --- //surefire:test任务,负责执行测试的插件
[INFO] Surefire report directory: C:\Users\chengxiang.peng.QUNARSERVERS\MavenSou
rces\hello-world\target\surefire-reports
------------------------------------------------------- //输出测试报告
T E S T S
-------------------------------------------------------
Running com.juvenxu.mvnbook.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.063 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.605 s
[INFO] Finished at: 2015-05-01T16:23:59+08:00
[INFO] Final Memory: 13M/154M
[INFO] ------------------------------------------------------------------------
四、打包和运行
将项目进行编译、测试之后,下一个重要的步骤就是打包(package),执行命令mvn clean package进行打包;
C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world>mvn clean package
[INFO] Scanning for projects...
….. ……
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world --- //jar:jar任务,负责打包
[INFO] Building jar: C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target\hello-world-1.0-SNAPSHOT.jar
…… …..
如何才能让其他的项目直接饮用这个jar呢,执行mvn clean install命令
C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world>mvn clean install
[INFO] Scanning for projects...
…… ……
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world --- //执行安装任务install:install,将项目输出的jar包安装到Maven本地仓库中
[INFO] Installing C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\target\hello-world-1.0-SNAPSHOT.jar to C:\Users\chengxiang.peng.QUNARSERVERS\.m2\repository\com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.jar
[INFO] Installing C:\Users\chengxiang.peng.QUNARSERVERS\MavenSources\hello-world\pom.xml to C:\Users\chengxiang.peng.QUNARSERVERS\.m2\repository\com\juvenxu\mvnbook\hello-world\1.0-SNAPSHOT\hello-world-1.0-SNAPSHOT.pom
…… ……
默认打包生成的jar是不能够直接运行的,因为带有main方法的类信息不会添加到manifext中(打开jar文件中的META_INF?MANIFEAT.MF文件,将无法看到Main-Class一行);
为了生成可执行的jar文件,需要借助maven-shade-plugin,配置改插件如下:
<plugins>
<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.juvenxu.mvnbook.helloworld.HelloWorld</mainClass> //配置mainClass
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
构建完成之后打开target/目录,可以看到hello-world-1.0-SNAPHOST.jar和original-hello-world-SNAPHOST.jar,前者带有Main-Class信息可以运行jar,后者是原有的jar;
打开hello-world-1.0-SNAPHOST.jar的META_INF/MANIFEST.MF文件如下:
Manifest-Version: 1.0
Build-Jdk: 1.7.0_71
Built-By: chengxiang.peng
Created-By: Apache Maven 3.3.1
Main-Class: com.juvenxu.mvnbook.helloworld.HelloWorld
Archiver-Version: Plexus Archiver
在项目跟目录下执行java -jar target\hello-world-1.0-SNAPSHOT.jar
五、使用Archetype生成项目骨架
前面Hello World项目有一些Maven约定:
根目录放置pom.xml;
在src/main/java目录中放置项目的主代码;
在src/test/java中放置项目的测试代码;
Maven提供了Archetype可以帮我们快速勾勒出项目骨架,执行mvn archetype:generate,根据提示输入相关信息生成Maven基本结构项目;