GitHub倉庫:Fundebug/maven-tutorialjava
Maven是Java項目構建工具,能夠用於管理Java依賴,還能夠用於編譯、打包以及發佈Java項目,相似於JavaScript生態系統中的NPM。git
Maven的命令行工具爲mvn,其經常使用命令以下表所示:github
命令 | 說明 |
---|---|
mvn compile | 編譯Java源代碼 |
mvn package | 打包Java項目 |
mvn deploy | 將Java項目發佈到Maven倉庫 |
mvn clean | 刪除構建目錄 |
Maven的配置文件爲pom.xml,這個文件有個很嚇人的學術名字Project Object Model,可是怎麼看它都只是個普通的配置文件,與NPM中的package.json沒啥本質區別。spring
Maven的中央倉庫爲**Maven Repository**,這裏能夠找到各類Java依賴,例如咱們Fundebug的異常監控插件fundebug-java與fundebug-spring。apache
在MacBook上使用brew安裝很方便json
brew install maven
我安裝的是maven版本3.5.4緩存
mvn -version Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00) Maven home: /usr/local/Cellar/maven/3.5.4/libexec Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre Default locale: zh_CN, platform encoding: UTF-8 OS name: "mac os x", version: "10.14.2", arch: "x86_64", family: "mac"
本文示例代碼都在GitHub倉庫Fundebug/maven-tutorial。bash
使用tree命令能夠查看項目的目錄結構:app
tree -v . ├── pom.xml └── src └── main └── java └── com └── fundebug └── Hello.java
pom.xml爲Maven配置文件,位於項目的根目錄。maven
Hello.java爲Java源代碼,位於src/main/java/com/fundebug目錄中。根據Maven對目錄結構的要求,Java源代碼必須位於src/main/java目錄。
package com.fundebug; import org.json.JSONObject; public class Hello { public static void main(String[] args) { JSONObject tomJsonObj = new JSONObject(); tomJsonObj.put("name", "Fundebug"); tomJsonObj.put("url", "https://www.fundebug.com"); System.out.println(tomJsonObj.toString(4)); } }
Hello.java很是簡單,定義了一個JSON對象,而後把它打印出來了。
package定義的包名爲com.fundebug,須要與所在的目錄結構相吻合,所以Hello.java位於src/main/java/com/fundebug目錄中,而不是**src/main/java/**目錄中。
代碼依賴於第三方模塊json,所以須要在pom.xml配置dependency:
<dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency>
<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.fundebug</groupId> <artifactId>hello</artifactId> <version>1.0</version> <!-- 依賴 --> <dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> </dependencies> </project>
pom.xml中,<project></project>
爲最外層的標籤;<modelVersion>4.0.0</modelVersion>
定義了所使用的POM版本。這2個標籤基本上是不變的。
groupId、artifactId與version一塊兒則定義了模塊的座標(Coordinates),每一個公共模塊的座標應該是惟一的:
<dependencies></dependencies>
定義了當前項目所依賴的模塊:
<dependencies> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180813</version> </dependency> </dependencies>
Maven能夠根據<dependency></dependency>
中定義的座標,自動下載所依賴的模塊。在MacBook上,Maven將下載的模塊緩存在**$HOME/.m2/**目錄。
執行mvn package命令,便可將源碼打包爲.jar文件:
mvn package [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.fundebug:hello >------------------------- [INFO] Building hello 1.0 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/fundebug/Desktop/maven-tutorial/src/main/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ hello --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 1 source file to /Users/fundebug/Desktop/maven-tutorial/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /Users/fundebug/Desktop/maven-tutorial/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello --- [INFO] No tests to run. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello --- [INFO] Building jar: /Users/fundebug/Desktop/maven-tutorial/target/hello-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.789 s [INFO] Finished at: 2019-01-05T15:23:02+08:00 [INFO] ------------------------------------------------------------------------
mvn package執行以後,項目中會新增一個tartget目錄,編譯的字節碼文件位於target/classes目錄,而jar包位於target/hello-1.0.jar:
tree -v . ├── pom.xml ├── src │ └── main │ └── java │ └── com │ └── fundebug │ └── Hello.java └── target ├── classes │ └── com │ └── fundebug │ └── Hello.class ├── hello-1.0.jar
打包好的jar包,能夠直接使用java命令運行時,注意須要指定所依賴的jar包。對於所依賴的jar包,Maven則會自動下載依賴,放在本地倉庫中。在MacBook上,Maven本地倉庫位於**$HOME/.m2/**目錄。
java -cp target/hello-1.0.jar:$HOME/.m2/repository/org/json/json/20180813/json-20180813.jar com.fundebug.Hello { "name": "Fundebug", "url": "https://www.fundebug.com" }
也可使用mvn exec:java命令執行,不須要指定依賴的jar包,更加方便:
mvn exec:java -Dexec.mainClass="com.fundebug.Hello" [INFO] Scanning for projects... [INFO] [INFO] -------------------------< com.fundebug:hello >------------------------- [INFO] Building hello 1.0 [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ hello --- { "name": "Fundebug", "url": "https://www.fundebug.com" } [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.837 s [INFO] Finished at: 2019-01-05T15:33:57+08:00 [INFO] ------------------------------------------------------------------------