像Make的Makefile迂曲、Ant的build.xml一樣稻励,Maven是基于pom.xml來管理配置的。POM(Project Object Model衙吩,項(xiàng)目對(duì)象模型)定義了項(xiàng)目的基本信息笔刹,描述了項(xiàng)目如何構(gòu)建芥备、聲明項(xiàng)目依賴等信息。
編寫POM
我們不需要手動(dòng)創(chuàng)建pom.xml文件徘熔,可以使用開發(fā)工具創(chuàng)建初始文件门躯。這里我使用的IntelliJ IDEA創(chuàng)建個(gè)maven工程。
目錄結(jié)構(gòu)為maven約定的目錄結(jié)構(gòu)
maven-p1/src/main主目錄
maven-p1/src/test測(cè)試目錄
maven-p1/pom.xml POM文件
同時(shí)我們會(huì)看到工程目錄下有pom.xml文件。文件內(nèi)容為:
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.buff.study</groupId>
<artifactId>maven-p1</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
其中g(shù)roupId山孔、artifactId懂讯、version是我們?cè)趧?chuàng)建工程時(shí)輸入的內(nèi)容。
編寫工程代碼
創(chuàng)建好pom.xml台颠,即創(chuàng)建好maven工程后褐望,需要編寫工程代碼。這里我們寫個(gè)包含main方法的測(cè)試類
package com.buff.study.mvnp1;
/**
* @Author: Buff
* @Description:測(cè)試類
* @Date: Created in 2018/6/8 12:46
*/
public class HelloWorld {
public String sayHello(){
return "hello maven!";
}
public static void main(String[] args) {
System.out.println(new HelloWorld().sayHello());
}
}
maven編譯
在工程目錄下串前,執(zhí)行mvn clean compile命令瘫里。我們會(huì)看到日志中三個(gè)maven插件以及對(duì)應(yīng)的三個(gè)操作,三個(gè)插件分別是maven-clean-plugin荡碾、maven-resources-plugin谨读、maven-compiler-plugin,如果是安裝完maven環(huán)境后第一次執(zhí)行該命令的話坛吁,maven會(huì)自動(dòng)下載這三個(gè)插件劳殖,同時(shí)會(huì)打印下載日志。這個(gè)三個(gè)插件順序?qū)?yīng)的三個(gè)操作分別是clean拨脉、resources哆姻、compile。
我們的命令mvn clean compile只指定了clean和compile玫膀,為啥會(huì)有resources呢矛缨,這個(gè)是由maven的生命周期過程決定的,后續(xù)會(huì)詳細(xì)講解匆骗。我們只需要感性的體會(huì)一下mavne的命令執(zhí)行即可劳景。
clean:清空輸出目錄target目錄
resources:由于工程沒有定義資源文件,所以這里沒執(zhí)行實(shí)際的動(dòng)作
compile:將項(xiàng)目源碼編譯至target/classes目錄下
C:\Users\vicya\data\study\maven-p1>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-p1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-p1 ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-p1 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-p1 ---
[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\vicya\data\study\maven-p1\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.279 s
[INFO] Finished at: 2018-06-08T12:52:10+08:00
[INFO] Final Memory: 13M/198M
[INFO] ------------------------------------------------------------------------
編寫工程測(cè)試代碼
由于我們使用Junit框架作為測(cè)試工具碉就,所以需要添加junit的jar包盟广。如果不使用maven的話,我們需要從網(wǎng)上下載junit的jar包瓮钥,并且通過IDE引入到工程中筋量。但是如果使用maven的話烹吵,我們只需要在pom.xml配置文件 中做以下配置,maven就可以幫我們自動(dòng)下載對(duì)應(yīng)的jar包桨武,并完成工程引入肋拔,非常方便。maven下載的jar文件是從倉(cāng)庫(kù)中獲取呀酸,關(guān)于倉(cāng)庫(kù)我們后續(xù)再詳細(xì)講解凉蜂。
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
在配置文件中添加以上內(nèi)容后,IDEA監(jiān)聽到pom.xml作了修改性誉,會(huì)提示我們進(jìn)行pom的重構(gòu)窿吩,點(diǎn)擊Import Changes即可。注意<scope>test</scope>配置错览,scope表示依賴范圍纫雁,設(shè)置為test就是說junit只會(huì)在測(cè)試目錄中使用,在主目錄中是不可見的倾哺,如果在主目錄代碼中使用junit會(huì)編譯不過的轧邪。這樣的話在工程打包的時(shí)候的也不會(huì)引入junit相關(guān)包。
添加完依賴包之后就可以編寫junit測(cè)試代碼羞海,代碼如下:
import static org.junit.Assert.assertEquals;
/**
* @Author: Buff
* @Description:
* @Date: Created in 2018/6/8 13:05
*/
public class HelloWorldTest {
@Test
public void testSayHello(){
HelloWorld helloWorld = new HelloWorld();
String result = helloWorld.sayHello();
assertEquals("hello maven!", result);
}
}
maven測(cè)試
在工程目錄下執(zhí)行mvn clean test忌愚,會(huì)看到對(duì)maven進(jìn)行了以下操作
1.clean輸出目錄
2.主目錄resources資源文件處理
3.主目錄源碼編譯
4.測(cè)試目錄resources資源文件處理
5.測(cè)試目錄源碼編譯
6.進(jìn)行測(cè)試代碼
7.輸出測(cè)試報(bào)告
C:\Users\vicya\data\study\maven-p1>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-p1 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-p1 ---
[INFO] Deleting C:\Users\vicya\data\study\maven-p1\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-p1 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-p1 ---
[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\vicya\data\study\maven-p1\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-p1 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\vicya\data\study\maven-p1\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-p1 ---
[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\vicya\data\study\maven-p1\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-p1 ---
[INFO] Surefire report directory: C:\Users\vicya\data\study\maven-p1\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.buff.study.mvnp1.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.484 s
[INFO] Finished at: 2018-06-08T13:27:20+08:00
[INFO] Final Memory: 15M/210M
[INFO] ------------------------------------------------------------------------
maven打包和運(yùn)行
以上內(nèi)容我們基本完成了代碼的編寫和測(cè)試工作,接下來我們需要進(jìn)行打包并運(yùn)行却邓。
maven在沒有指定打包文件格式時(shí)菜循,默認(rèn)的是打成jar包。在控制臺(tái)運(yùn)行mvn clean package命令后申尤,在target目錄下會(huì)生成maven-p1-1.0-SNAPSHOT.jar。以下是部分maven日志衙耕。
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-p1 ---
[INFO] Building jar: C:\Users\vicya\data\study\maven-p1\target\maven-p1-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.115 s
[INFO] Finished at: 2018-06-08T13:37:50+08:00
[INFO] Final Memory: 16M/211M
[INFO] ------------------------------------------------------------------------
接下來運(yùn)行jar包昧穿,結(jié)果運(yùn)行失敗。
C:\Users\vicya\data\study\maven-p1>java -jar ./target/maven-p1-1.0-SNAPSHOT.jar
./target/maven-p1-1.0-SNAPSHOT.jar中沒有主清單屬性
這是由于我們?cè)诖虬臅r(shí)候沒有指定main方法橙喘,所以這個(gè)jar包是無法正常啟動(dòng)運(yùn)行的时鸵。如果我們的工程本身沒有main方法,只是提供給其他工程做依賴的厅瞎,那這個(gè)打包是沒問題的饰潜。可如果我們要運(yùn)行這個(gè)jar包則class中必須要有main方法和簸,并且jar包的MANIFAST.MF必須指定該方法彭雾。所以這就需要我們做額外的工作來添加MANIFAST.MF。使用maven插件來幫我們完成這些工作锁保,在pom.xml中配置以下內(nèi)容薯酝,主要是告訴maven插件我們的main方法所在類即可半沽。
<build>
<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.buff.study.mvnp1.HelloWorld
</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置完成后,再次打包并運(yùn)行吴菠,可以看到正確結(jié)果
C:\Users\vicya\data\study\maven-p1>java -jar ./target/maven-p1-1.0-SNAPSHOT.jar
hello maven!