1-下載及安裝
1.1 - Maven - 項目管理利器
http://maven.apache.org/
Apache組織的開源項目。
Maven是一個基於POM(Project Object Model, 項目對象模型)的項目構建和管理工具。
可以通過一小段描述信息來管理項目的構建、報告和文檔,有助於開發者快速完成項目的配置,快速建立開發環境,從而提高開發效率。
常用項目管理和構建工具:
- Maven : http://maven.apache.org/
- Ant : http://ant.apache.org/
- Gradle :https://gradle.org/
1.2 - Maven的下載及安裝
- Downloading Apache Maven : http://maven.apache.org/download.cgi
- Installing Apache Maven : http://maven.apache.org/install.html
以windows系統為例:
- 確認已安裝JDK並配置系統變量JAVA_HOME
- 安裝Maven(解壓安裝包,例如:apache-maven-3.5.0-bin.zip)
- 配置系統變量M2_HOME,指向maven的安裝目錄(例如"D:\DownLoadFiles\apache-maven-3.5.0"), 並將maven的安裝目錄的bin目錄添加到系統path(例如"%M2_HOME%\bin",注意前后的分號)
- 運行"mvn -version"命令測試是否安裝成功,如果安裝成功則顯示Maven、Java、OS等相關版本信息:
$ mvn -version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00)
Maven home: D:\DownLoadFiles\apache-maven-3.5.0
Java version: 1.8.0_101, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_101\jre
Default locale: en_US, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
安裝后的目錄結構:
- bin/ : 包含mvn的運行腳本
- boot/ : 包含一個類加載器的框架
- conf/ : 配置文件目錄,例如經常用到的settings.xml文件
- lib/ : maven運行時用到的所有類庫(包括maven自身和第三方的類庫)
- LICENSE : 許可信息
- NOTICE :版權信息
- README.txt :使用說明及其他信息
1.3 - Maven項目的默認目錄結構
pom.xml Maven項目的核心配置文件,位於項目根目錄
src 源代碼目錄
- src/main/java/ 源文件(不包含測試)目錄,存放主代碼
- src/main/resources/ 資源文件目錄
- src/test/java/ 測試源文件目錄,存放測試代碼
- src/test/resources/ 測試資源文件目錄
target 構建過程中的默認生成的臨時目錄
- target/classes/ 存放src/main/java目錄下源文件編譯出來的字節碼文件(.class)
- target/maven-archiver/
- target/maven-status/
- target/surefire-reports/ 存放生成的測試報告
- target/test-classes/ 存放src/test/java目錄下源文件編譯出來的字節碼文件(.class)
- target/xxx-y.y.y-zzz.jar 生成的jar包
2-坐標和倉庫
2.1 - Maven坐標
在Maven中任何一個依賴、插件、項目構建的輸出都可以被稱為構件
所有構件通過坐標所為其唯一的標識
示例:一個基本坐標
<groupId>anliven.testmaven02</groupId>
<artifactId>testmaven02</artifactId>
<version>0.0.1-SNAPSHOT</version>
2.2 - Maven資源倉庫
Maven資源庫為依賴包提供來源。
如果本地倉庫中的沒有相應的依賴包,默認繼續在中央或遠程倉庫查找,下載后會放到本地倉庫。如果都沒有查找到,將提示報錯。
- 中央倉庫(central):Maven官方維護的倉庫,例如“http://central.maven.org/maven2/”。
- 遠程倉庫:在遠程服務器上建立的私有性質倉庫,本質類似於中央倉庫,可通過setting.xml文件設置。
- 本地倉庫:包含下載的所有依賴包,默認位於用戶目錄(例如windows系統:C:\Users<username>.m2\repository),可通過setting.xml文件設置。
官方資源倉庫
- http://central.maven.org/maven2/
- http://repo1.maven.org/maven2/
- http://repo2.maven.org/maven2/
- http://uk.maven.org/maven2/
阿里雲資源倉庫
Nexus : http://maven.aliyun.com/nexus/
Repositories: repositories http://maven.aliyun.com/nexus/#view-repositories
- 公共倉庫:http://maven.aliyun.com/nexus/content/groups/public/
- 中央倉庫:http://maven.aliyun.com/nexus/content/repositories/central/
查找jar包信息
其他資源倉庫
- https://oss.sonatype.org/content/groups/public/
- https://oss.sonatype.org/content/repositories/releases/
- https://maven.java.net/content/groups/public/
- https://maven.java.net/content/repositories/releases/
2.3 - 設置本地Maven倉庫
本地倉庫包含下載的所有依賴包,默認位於用戶目錄(例如windows系統:C:\Users<username>.m2\repository),可通過setting.xml文件設置。
示例:
<localRepository>D:\DownLoadFiles\apache-maven-repo</localRepository>
2.4 - 設置鏡像倉庫
通過設置多個遠程和中央倉庫的鏡像地址可以避免訪問緩慢或無法訪問的問題。
示例:
<mirrors>
<!-- 中央倉庫 -->
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>central</name>
<url>http://central.maven.org/maven2/</url>
</mirror>
<!-- 中央倉庫1 -->
<mirror>
<id>repo1</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo1.maven.org/maven2/</url>
</mirror>
<!-- 中央倉庫2 -->
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<!-- 阿里雲倉庫 -->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
</mirrors>
3-POM文件
3.1 - POM
POM文件是Maven項目中的核心配置和管理文件,也被稱為Maven描述文件。
- POM(Project Object Model,項目對象模型)是描述項目構建信息的XML格式文件,位於項目的根目錄。
- 統一管理項目構建的關鍵信息,包括:開發規范、開發工具、項目代碼、測試代碼、資源、依賴的包等。
- pom之間實際上存在三種關系:繼承、依賴、聚合。
官網參考信息:http://maven.apache.org/ref/3.5.0/maven-model/maven.html
3.2 - 解讀pom.xml
- project POM文件的根元素,包含約束信息
- modelVersion 指定當前Maven模型的版本號,對於Maven2和Maven3只能設置為4.0.0
- groupId 項目的全球唯一標識符(整個系統的名稱),一般是域名的反寫
- artifactId 構件標識符(子模塊名稱),和groupId一起唯一標識一個構件,可以使用"項目名-子模塊名"的命名方式
- version 項目當前版本,格式為:主版本.次版本.增量版本-限定版本號;限定版本號可以設置為SNAPSHOT(開發),Laest(最新),Alpha(內部測試),Release(穩定),Beta(公測),GA(正式發布)等
重要:groupId、artifactId、version三個元素構成基本坐標,可以唯一標識一個Maven項目。
- packaging 項目產生的構件類型(項目打包的類型),可以取值為jar、war、rar、ear、pom,也可以創建新類型;如果不設置,默認為jar
- name 項目的名稱, Maven產生的文檔用
- url 項目主頁的URL, Maven產生的文檔用
- description 項目的詳細描述, Maven 產生的文檔用
- developers 項目開發人員信息
- licenses 許可信息
- organization 組織信息
- dependencies 項目相關的所有依賴(dependency 包含一個依賴包的坐標信息)
- properties 定義配置屬性,例如設置project.build.sourceEncoding為UTF-8,防止中文亂碼
- build 定義構建項目需要的信息
- resources 描述項目相關或測試相關的所有資源路徑
3.3 - parent - 繼承
應用在子項目中。
<!-- 父項目的坐標。如果項目中沒有規定某個元素的值,那么父項目中的對應值即為項目的默認值。坐標包括group ID,artifact ID和 version。 -->
<parent>
<!-- 被繼承的父項目的構件標識符 -->
<artifactId>xxx</artifactId>
<!-- 被繼承的父項目的全球唯一標識符 -->
<groupId>xxx</groupId>
<!-- 被繼承的父項目的版本 -->
<version>xxx</version>
<!-- 父項目的pom.xml文件的相對路徑。默認值是../pom.xml。Maven首先在構建當前項目的地方尋找父項目的pom,其次在文件系統的這個位置(relativePath位置),然后在本地倉庫,最后在遠程倉庫尋找父項目的pom。 -->
<relativePath>xxx</relativePath>
</parent>
3.4 - dependencies - 依賴
官網信息:Dependency Scope
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope
- groupId 依賴項的groupId
- artifactId 依賴項的artifactId
- version 依賴項的版本
- exclusions 排除項目中的依賴沖突時使用。
- scope 依賴項的適用范圍:
- compile,缺省值,適用於所有階段,會隨着項目一起發布。
- provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。
- runtime,只在運行時使用,如JDBC驅動,適用運行和測試階段。
- test,只在測試時使用,用於編譯和運行測試代碼。不會隨項目發布。
- system,類似provided,與本機系統相關聯,可移植性差,需要顯式提供包含依賴的jar,Maven不會在Repository中查找它。
- import,導入的范圍,只使用在dependencyManagement中,表示從其它的pom中導入dependecy的配置
<!-- 該元素描述了項目相關的所有依賴。 這些依賴組成了項目構建過程中的一個個環節。它們自動從項目定義的倉庫中下載。 -->
<dependencies>
<dependency>
<!-- 依賴的group ID -->
<groupId> org.apache.maven </groupId>
<!-- 依賴的artifact ID -->
<artifactId> maven-artifact </artifactId>
<!-- 依賴的版本號。 在Maven 2里, 也可以配置成版本號的范圍。 -->
<version> 3.8.1 </version>
<!-- 依賴類型,默認類型是jar。它通常表示依賴的文件的擴展名,但也有例外。一個類型可以被映射成另外一個擴展名或分類器。類型經常和使用的打包方式對應,盡管這也有例外。一些類型的例子:jar,war,ejb-client和test-jar。-->
<type> jar </type>
<!-- 依賴的分類器。分類器可以區分屬於同一個POM,但不同構建方式的構件。分類器名被附加到文件名的版本號后面。例如,如果你想要構建兩個單獨的構件成JAR,一個使用Java 1.4編譯器,另一個使用Java 6編譯器,你就可以使用分類器來生成兩個單獨的JAR構件。 -->
<classifier></classifier>
<!-- 依賴范圍。在項目發布過程中,幫助決定哪些構件被包括進來。欲知詳情請參考依賴機制。
- compile :默認范圍,用於編譯
- provided:類似於編譯,但支持你期待jdk或者容器提供,類似於classpath
- runtime: 在執行時需要使用
- test: 用於test任務時使用
- system: 需要外在提供相應的元素。通過systemPath來取得
- systemPath: 僅用於范圍為system。提供相應的路徑
- optional: 當項目自身被依賴時,標注依賴是否傳遞。用於連續依賴時使用 -->
<scope> test </scope>
<!-- 僅供system范圍使用。注意,不鼓勵使用這個元素,並且在新的版本中該元素可能被覆蓋掉。該元素為依賴規定了文件
系統上的路徑。需要絕對路徑而不是相對路徑。推薦使用屬性匹配絕對路徑,例如${java.home}。 -->
<systemPath></systemPath>
<!-- 排除依賴列表,不依賴項目的依賴。此元素主要用於解決版本沖突問題 -->
<exclusions>
<exclusion>
<artifactId> spring-core </artifactId>
<groupId> org.springframework </groupId>
</exclusion>
</exclusions>
<!-- 可選依賴,如果在項目B中聲明C依賴為可選,那么需要在依賴於B的項目A中顯式的引用對C的依賴。可選依賴阻斷依賴的傳遞性。 -->
<optional> true </optional>
</dependency>
</dependencies>
dependencyManagement
依賴管理。其中定義的多個依賴,並不會實際引入。
應用在父模塊中,供子模塊所繼承使用。
<!-- 繼承自該項目的所有子項目的默認依賴信息。這部分的依賴信息不會被立即解析,而是當子項目聲明一個依賴(必須描述group ID和artifact ID信息),
如果group ID和artifact ID以外的一些信息沒有描述,則通過group ID和artifact ID匹配到這里的依賴,並使用這里的依賴信息。 -->
<dependencyManagement>
<dependencies>
<!-- 參見dependencies/dependency元素 -->
<dependency></dependency>
</dependencies>
</dependencyManagement>
3.5 - modules - 聚合
可以通過一個大的項目來整合各個小的模塊
<!-- 模塊(有時稱作子項目) 被構建成項目的一部分。列出的每個模塊元素是指向該模塊的目錄的相對路徑 -->
<modules>
<!--子項目相對路徑-->
<module></module>
</modules>
同時需要將packaging設置為pom
<packaging>pom</packaging>
3.6 - pluginManagement
Plugin的配置,應用在父項目。
<!-- 子項目可以引用的默認插件信息。該插件配置項直到被引用時才會被解析或綁定到生命周期。給定插件的任何本地配置都會覆蓋這里的配置 -->
<pluginManagement>
<!-- 使用的插件列表 。 -->
<plugins>
<!-- plugin元素包含描述插件所需要的信息。 -->
<plugin>
<!-- 插件在倉庫里的group ID -->
<groupId></groupId>
<!-- 插件在倉庫里的artifact ID -->
<artifactId></artifactId>
<!-- 被使用的插件的版本(或版本范圍) -->
<version></version>
<!-- 是否從該插件下載Maven擴展(例如打包和類型處理器),由於性能原因,只有在真需要下載時,該元素才被設置成enabled。 -->
<extensions>true/false</extensions>
<!-- 在構建生命周期中執行一組目標的配置。每個目標可能有不同的配置。 -->
<executions>
<!-- execution元素包含了插件執行需要的信息 -->
<execution>
<!-- 執行目標的標識符,用於標識構建過程中的目標,或者匹配繼承過程中需要合並的執行目標 -->
<id></id>
<!-- 綁定了目標的構建生命周期階段,如果省略,目標會被綁定到源數據里配置的默認階段 -->
<phase></phase>
<!-- 配置的執行目標 -->
<goals></goals>
<!-- 配置是否被傳播到子POM -->
<inherited>true/false</inherited>
<!-- 作為DOM對象的配置 -->
<configuration></configuration>
</execution>
</executions>
<!-- 項目引入插件所需要的額外依賴 -->
<dependencies>
<!-- 參見dependencies/dependency元素 -->
<dependency></dependency>
</dependencies>
<!-- 任何配置是否被傳播到子項目 -->
<inherited>true/false</inherited>
<!-- 作為DOM對象的配置 -->
<configuration></configuration>
</plugin>
</plugins>
</pluginManagement>
3.7 - resources
Build時需要的資源文件
<!-- 這個元素描述了項目相關的所有資源路徑列表,例如和項目相關的屬性文件,這些資源被包含在最終的打包文件里。 -->
<resources>
<!-- 這個元素描述了項目相關或測試相關的所有資源路徑 -->
<resource>
<!-- 描述了資源的相對路徑(相對target/classes目錄)。如果只是想把資源放到源碼目錄結構里,就不需要該配置。 -->
<targetPath></targetPath>
<!-- 是否使用參數值代替參數名。參數值取自properties元素或者文件里配置的屬性,文件在filters元素里列出。 -->
<filtering></filtering>
<!-- 描述存放資源的目錄,該路徑相對POM路徑 -->
<directory></directory>
<!-- 包含的模式列表,例如**/*.xml. -->
<includes>
<include></include>
</includes>
<!-- 排除的模式列表,例如**/*.xml -->
<excludes>
<exclude></exclude>
</excludes>
</resource>
</resources>
4-生命周期
4.1 - 生命周期
- clean 清理項目
- default 構建項目
- site 生成項目站點
4.2 - 完整的項目構建過程
清理、編譯、測試、打包、集成測試、驗證、部署
clean、compile、test、package、install
注意:執行某個階段時,其前面的階段會順序執行,但不會觸發其他生命周期的另外階段。
4.3 - clean的階段
- pre-clean 執行清理前的工作
- clean 清理上一次構建生成的所有文件
- post-clean 執行清理后的文件
4.4 - default構建項目(最核心)
compile、test、package、install
4.5 - site的階段
根據pom文件中信息自動生成站點
- pre-site 在生成項目站點前要完成的工作
- site 生成項目的站點文檔
- post-site 在生成項目站點后要完成的工作
- site-deploy 發布
5-常用命令
5.1 - 查看幫助信息
- mvn --help/-h
- mvn help:help 顯示help插件的幫助信息
- mvn help:help -Ddetail=true 顯示help插件的詳細幫助信息
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$ mvn help:help
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven02 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:2.2:help (default-cli) @ testmaven02 ---
[INFO] Maven Help Plugin 2.2
The Maven Help plugin provides goals aimed at helping to make sense out of the
build environment. It includes the ability to view the effective POM and
settings files, after inheritance and active profiles have been applied, as
well as a describe a particular plugin goal to give usage information.
This plugin has 9 goals:
help:active-profiles
Displays a list of the profiles which are currently active for this build.
help:all-profiles
Displays a list of available profiles under the current project.
Note: it will list all profiles for a project. If a profile comes up with a
status inactive then there might be a need to set profile activation
switches/property.
help:describe
Displays a list of the attributes for a Maven Plugin and/or goals (aka Mojo -
Maven plain Old Java Object).
help:effective-pom
Displays the effective POM as an XML for this build, with the active profiles
factored in.
help:effective-settings
Displays the calculated settings as XML for this project, given any profile
enhancement and the inheritance of the global settings into the user-level
settings.
help:evaluate
Evaluates Maven expressions given by the user in an interactive mode.
help:expressions
Displays the supported Plugin expressions used by Maven.
help:help
Display help information on maven-help-plugin.
Call mvn help:help -Ddetail=true -Dgoal=<goal-name> to display parameter
details.
help:system
Displays a list of the platform details like system properties and environment
variables.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.409 s
[INFO] Finished at: 2017-10-24T14:43:49+08:00
[INFO] Final Memory: 11M/309M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven02
$
5.2 - 查看插件的幫助信息:
mvn <plug-in>:help
- 示例:mvn dependency:help
5.3 - 使用Maven Help 插件
mvn help:describe命令可以顯示某個插件的信息
- -Dplugin=pluginName
- -Dgoal(或-Dmojo)=goalName
- -Ddetail=true
mvn help:describe -Dplugin=help # 使用help插件的describe目標來顯示Maven Help插件的信息
mvn help:describe -Dplugin=help -Dfull # 顯示Maven Help插件所有可用目標
mvn help:describe -Dplugin=exec -Dfull # 顯示Maven Exec插件所有可用目標
mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull # 獲取單個目標的信息,顯示Maven Compiler插件的compile目標信息
mvn help:effective-pom # 查看實際pom信息
5.4 - 常用命令
- mvn -version/-v 查看maven版本
- mvn compile 編譯源代碼
- mvn test 運行測試
- mvn test-compile 編譯測試代碼
- mvn test -skipping compile -skipping test-compile # 只測試,不編譯,也不測試編譯;
- mvn package 打包(生成target目錄,編譯、測試代碼,生成測試報告,生成jar/war文件)
- mvn package -Dmaven.test.skip=ture 打包時跳過測試
- mvn install 安裝jar到本地Repository中
- mvn install -X 打開 Maven 的調試標記查看完整的依賴蹤跡(包含被拒絕引入的構件)
- mvn clean 清除產生的項目(刪除target目錄及文件)
- mvn clean install 先清除,然后編譯安裝到本地倉庫
mvn -e # 顯示詳細錯誤信息
mvn validate # 驗證工程是否正確,所有需要的資源是否可用
mvn verify # 運行任何檢查,驗證包是否有效且達到質量標准
mvn archetype:create -DgroupId=packageName -DartifactId=projectName # 創建Maven的普通Java項目
mvn archetype:generate # 反向生成maven項目的骨架(建議使用,簡潔方便,根據提示操作即可)
mvn eclipse:eclipse # 生成eclipse項目結構(將項目轉化為Eclipse項目)
mvn eclipse:clean # 清除Eclipse項目結構
mvn idea:idea # 生成idea項目結構
mvn site # 生成站點目錄:
mvn site-deploy # 生成站點目錄並發布
mvn deploy # 上傳到私有服務器
mvn jar:jar # 只打jar包
mvn -Dtest package # 只打包不測試
mvn source:jar # 源碼打包
mvn source:jar-no-fork # 源碼打包
mvn dependency:list # 顯示當前項目已被解析的依賴
mvn dependency:resolve # 顯示已解決依賴的列表
mvn dependency:tree # 顯示整個依賴樹
mvn dependency:analyze # 分析項目的依賴信息,作用等同於mvn dependency:tree
5.5 - 一些示例
mvn compile 編譯
如果是第一次運行mvn compile等命令時,將會下載很多的第三方和maven所依賴的jar包。
在Maven項目根目錄下,默認生成target目錄:
target 構建過程中的默認生成的臨時目錄
target/classes/ 存放src/main/java目錄下源文件編譯出來的字節碼文件(.class)
target/maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[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 D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.064 s
[INFO] Finished at: 2017-10-20T10:56:12+08:00
[INFO] Final Memory: 15M/292M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 1
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 target/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ls -l target/classes/anliven/testmaven01/HelloMaven.class
-rw-r--r-- 1 guowli 1049089 406 Oct 20 10:56 target/classes/anliven/testmaven01/HelloMaven.class
mvn test 測試
生成如下目錄:
target/surefire-reports/ 存放生成的測試報告
target/test-classes/ 存放src/test/java目錄下源文件編譯出來的字節碼文件(.class)
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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 D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.064 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.286 s
[INFO] Finished at: 2017-10-20T11:01:06+08:00
[INFO] Final Memory: 16M/291M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/surefire-reports/
total 9
-rw-r--r-- 1 guowli 1049089 282 Oct 20 11:01 anliven.testmaven01.HelloMavenTest.txt
-rw-r--r-- 1 guowli 1049089 6398 Oct 20 11:01 TEST-anliven.testmaven01.HelloMavenTest.xml
mvn package 打包
生成如下目錄及文件:
target/maven-archiver/
target/xxx-y.y.y-zzz.jar 生成的jar包
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 0
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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 D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.065 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.437 s
[INFO] Finished at: 2017-10-20T11:13:48+08:00
[INFO] Final Memory: 16M/213M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:13 maven-archiver/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 10:56 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:01 test-classes/
-rw-r--r-- 1 guowli 1049089 2151 Oct 20 11:13 testmaven-0.0.1-SNAPSHOT.jar
mvn clean 刪除target目錄及文件
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ll
total 5
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 11:13 target/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom (0 B at 0 B/s)
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.jar (0 B at 0 B/s)
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testmaven ---
[INFO] Deleting D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.072 s
[INFO] Finished at: 2017-10-20T17:13:51+08:00
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 1
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$
mvn install 安裝jar包到本地倉庫中
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testmaven 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.pom (0 B at 0 B/s)
Downloading: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar
Downloaded: http://central.maven.org/maven2/org/apache/maven/plugins/maven-install-plugin/2.4/maven-install-plugin-2.4.jar (0 B at 0 B/s)
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testmaven ---
[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 D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testmaven ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\Anliven-Running\Zen\EclipseProjects\TestMaven\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testmaven ---
[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 D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ testmaven ---
[INFO] Surefire report directory: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running anliven.testmaven01.HelloMavenTest
Run test!
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ testmaven ---
[INFO] Building jar: D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ testmaven ---
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (2.5 kB at 4.7 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/3.1/plexus-3.1.pom (19 kB at 24 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.pom (1.1 kB at 1.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-components/1.1.7/plexus-components-1.1.7.pom (5.0 kB at 8.9 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus/1.0.8/plexus-1.0.8.pom (7.2 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-8/plexus-container-default-1.0-alpha-8.pom (7.3 kB at 13 kB/s)
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
Downloading: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-digest/1.0/plexus-digest-1.0.jar (12 kB at 9.8 kB/s)
Downloaded: http://central.maven.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (230 kB at 179 kB/s)
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\TestMaven\target\testmaven-0.0.1-SNAPSHOT.jar to D:\DownLoadFiles\apache-maven-repo\anliven\testmaven01\testmaven\0.0.1-SNAPSHOT\testmaven-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\Anliven-Running\Zen\EclipseProjects\TestMaven\pom.xml to D:\DownLoadFiles\apache-maven-repo\anliven\testmaven01\testmaven\0.0.1-SNAPSHOT\testmaven-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.602 s
[INFO] Finished at: 2017-10-20T17:14:19+08:00
[INFO] Final Memory: 18M/210M
[INFO] ------------------------------------------------------------------------
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l
total 5
-rw-r--r-- 1 guowli 1049089 561 Oct 19 17:44 pom.xml
drwxr-xr-x 1 guowli 1049089 0 Oct 19 13:21 src/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 target/
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$ ls -l target/
total 4
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 classes/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 maven-archiver/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 maven-status/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 surefire-reports/
drwxr-xr-x 1 guowli 1049089 0 Oct 20 17:14 test-classes/
-rw-r--r-- 1 guowli 1049089 2150 Oct 20 17:14 testmaven-0.0.1-SNAPSHOT.jar
guowli@5CG450158J MINGW64 /d/Anliven-Running/Zen/EclipseProjects/TestMaven
$