Maven知識點速查
阿新 • • 發佈:2019-02-02
Maven常用命令
- compile 編譯src/main/java檔案
- test 執行src/test/java下的帶@Test的測試檔案
- package 打包src/main/java到target目錄下生成jar包
- install 打包並把jar包拷貝到本地倉庫.m2下
- deploy 打包並把jar包拷貝釋出到遠端倉庫
Maven命令列建立目錄骨架
- mvn archetype:generate
Maven中的座標和倉庫
- 構件通過座標作為ID
- 座標包含
- groupId
- artifactId
- Version
- 倉庫:
- 本地倉庫
- 遠端倉庫
Maven生命週期和外掛
生命週期:是一種工程的抽象
- clean 清理
- pre-clean
- clean
- post-clean
- default 構建(最核心)
- compile
- test
- package
- install
- site 生成站點
- pre-site
- post-site
- site-deploy 釋出站點
外掛:是對生命週期的實現
使用外掛
<build>
<plugins>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<executions>
<phase >package</phase> <!-- 生命週期那個階段 -->
<goals>
<goal>jar-no-fork</goal><!--做什麼事-->
</goals>
</executions>
</plugins>
</build>
Maven的pom.xml結構的解析
<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>反寫的公司網址+專案名</groupId> <!-- 座標 -->
<artifactId>專案名+模組名(專案中的一個模組)</artifactId>
<packaging>jar</packaging> <!-- 打包,jar、war、zip、pom -->
<version>1.0-SNAPSHOT</version>
<!-- 0.x.x 表示大版本
x.0.x 分支版本
x.x.0 小版本
0.0.1snapshot 快照
alpha 內測
beta 公測
release 穩定
GA 正式釋出 -->
<name></name> <!--文件用-->
<url></url> <!-- 專案地址 -->
<description></description>
<developers></developers>
<lincences></lincences>
<organization></organization>
<denpendencies>
<denpendency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<type></type>
<scope>test</scope>
<optional></optional> <!-- 設定依賴是否可選 -->
<exclusions><!-- 排除依賴,比如A->B->C,而A不想依賴C -->
<exclusion>
</exclusion>
</exclusions>
</denpendency>
</denpendencies>
<!-- 依賴管理,用在父模組中供繼承用, -->
<dependencyManagement>
<denpendencies>
<dependency></dependency>
</denpendencies>
</dependencyManagement>
<build>
<!-- 外掛列表 -->
<plugins>
<plugin>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</plugin>
</plugins>
</build>
<!-- 繼承 -->
<parent></parent>
<!-- 模組 -->
<modules>
<module>
</module>
</modules>
Maven依賴範圍
<scope>test</scope>
- compile 預設編譯和執行都有效
- test 測試
- runtime 執行和測試時有效
- provided 編譯和測試有效
- import 只用在dependencyManagement中,表示從其他pom匯入dependency的配置
Maven依賴傳遞
A->B->C,則A間接依賴C,A中會引入C的jar包,可以排除依賴用<exclusion></exclusion>,則C不見了
Maven依賴衝突,則看規則
- 短路優先
- A->B->C-X(jar) 不選這條
- A->D->X(jar) ✅
- 先宣告先優先:如果路徑長度相同,則誰先宣告,先解析誰
Maven的聚合和繼承
聚合:<modules></modules>
繼承:<parent></parent>
Maven的構建web專案
在<plugin>裡使用座標標記的jetty或tomcat外掛