Maven 是一個項目管理和整合工具。Maven 爲開發者提供了一套完整的構建生命週期框架。開發團隊幾乎不用花多少時間就可以自動完成工程的基礎構建配置,由於 Maven 使用了一個標準的目錄結構和一個默認的構建生命週期。java
在有多個開發團隊環境的狀況下,Maven 可以在很短的時間內使得每項工做都按照標準進行。由於大部分的工程配置操做都很是簡單而且可複用,在建立報告、檢查、構建和測試自動配置時,Maven 可讓開發者的工做變得更簡單。apache
這裏介紹一下在Windows 系統上安裝Maven。注:Maven 3.3+ 須要JDK 1.7或更高版本才能執行windows
咱們將下載下來的zip文件,解壓到本地磁盤目錄,而後配置環境變量。安全
1.添加M2_HOME 和 MAVEN_HOME 環境變量。注:Maven 說只是添加 M2_HOME , 但一些項目仍引用 Maven 的文件夾 MAVEN_HOME, 所以,爲了安全也把它添加進去框架
2.更新Path變量的變量值。在最後追加maven
命令:mvn -v 或 mvn -version: 查看Maven版本信息,並肯定是否安裝成。ide
C:\Users\Administrator>mvn -v Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:0 5+08:00) Maven home: D:\apache-maven-3.5.3\bin\.. Java version: 1.8.0_161, vendor: Oracle Corporation Java home: D:\DevSoft\jdk1.8.0_161 Default locale: zh_CN, platform encoding: GBK OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
若是你看到相似消息,說明 Apache Maven 在 Windows 上已安裝成功。工具
能夠在項目目錄下執行以下命令:會在你的項目路徑下生成一個名稱爲hello的Maven項目。單元測試
mvn archetype:generate -DgroupId=com.cnblogs.hello -DartifactId=hello -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
hello目錄結構以下圖:測試
全部的源代碼放在文件夾 /src/main/java/, 全部的單元測試代碼放入 /src/test/java/。
pom.xml文件內容:該文件是在Maven項目中的核心配置文件。它是一個單獨的配置文件,其中包含以您構建項目所需的大部分信息。後面我會詳細介紹這個文件。
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.cnblogs.hello</groupId> 5 <artifactId>hello</artifactId> 6 <packaging>jar</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>hello</name> 9 <url>http://maven.apache.org</url> 10 <dependencies> 11 <dependency> 12 <groupId>junit</groupId> 13 <artifactId>junit</artifactId> 14 <version>3.8.1</version> 15 <scope>test</scope> 16 </dependency> 17 </dependencies> 18 </project>
接下來執行命令:mvn package 將項目進行打包處理。注:這個命令必須在hello項目木木下執行即:D:\MavenWork\hello目錄
D:\MavenWork\hello>mvn package [INFO] Scanning for projects... [INFO] [INFO] ----------------------< com.cnblogs.hello:hello >----------------------- [INFO] Building hello 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e . build is platform dependent! [INFO] skip non existing resourceDirectory D:\MavenWork\hello\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 GBK, i.e. buil d is platform dependent! [INFO] Compiling 1 source file to D:\MavenWork\hello\target\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he llo --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e . build is platform dependent! [INFO] skip non existing resourceDirectory D:\MavenWork\hello\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hello - -- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil d is platform dependent! [INFO] Compiling 1 source file to D:\MavenWork\hello\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello --- [INFO] Surefire report directory: D:\MavenWork\hello\target\surefire-reports Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.pom (1.7 k B at 740 B/s) Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o rg/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or g/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (2.3 kB at 1.0 kB/s) Downloading from alimaven: http://maven.aliyun.com/nexus/content/groups/public/o rg/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar Downloaded from alimaven: http://maven.aliyun.com/nexus/content/groups/public/or g/apache/maven/surefire/surefire-junit3/2.12.4/surefire-junit3-2.12.4.jar (26 kB at 10 kB/s) ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.cnblogs.hello.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.004 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello --- [INFO] Building jar: D:\MavenWork\hello\target\hello-1.0-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.482 s [INFO] Finished at: 2018-06-15T14:46:34+08:00 [INFO] ------------------------------------------------------------------------
最後執行java命令進行測試打包生成的jar
java -cp target/hello-1.0-SNAPSHOT.jar com.cnblogs.hello.App
結果打印以下:
Hello World!
最後生成站點,執行命令:mvn site
mvn site
執行命令會會根據項目的信息生成一個網站。您能夠查看target/site下生成的文檔。
經過快速入門我能夠簡單瞭解Maven的一些功能,這只是一個快速入門指南。後續幾章會有更全面的詳細介紹。