首先說明,本人并不是第一次學(xué)習(xí)maven,也不是第二次學(xué)習(xí)maven秤标,為什么還要來學(xué)習(xí)寫下這些東西?是因?yàn)橹耙恢倍际峭ㄟ^視頻或者百度進(jìn)行學(xué)習(xí)宙刘,知識(shí)零散且不全面苍姜,甚至更有版本的知識(shí)穿插問題,故決定以官方文檔為基礎(chǔ)進(jìn)行系統(tǒng)性學(xué)習(xí)悬包。
什么是POM衙猪?
pom代表“項(xiàng)目對(duì)象模型”。它是名為pom.xml的文件中保存的Maven項(xiàng)目的XML表示布近。當(dāng)有Maven的人在場時(shí)垫释,談?wù)撘粋€(gè)項(xiàng)目是在哲學(xué)意義上說的,而不僅僅是包含代碼的文件集合撑瞧。項(xiàng)目包含配置文件棵譬、所涉及的開發(fā)人員及其角色、缺陷跟蹤系統(tǒng)预伺、組織和許可證订咸、項(xiàng)目所在位置的URL、項(xiàng)目的依賴項(xiàng)酬诀,以及所有其他可以提供代碼生命的小部分脏嚷。這是一個(gè)一站式商店,所有與項(xiàng)目有關(guān)的事情瞒御。事實(shí)上父叙,在Maven世界中,一個(gè)項(xiàng)目根本不需要包含任何代碼,只需要pom.xml趾唱。
POM 是用來項(xiàng)目的構(gòu)成屿岂,就像介紹一個(gè)人是誰,在哪一樣鲸匿,不影響構(gòu)建的生命周期爷怀。
一個(gè)最基本的POM如下所示:
<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>org.codehaus.mojo</groupId>
? <artifactId>my-project</artifactId>
? <version>1.0</version>
<!-- 依賴父項(xiàng)目 -->
<parent>
? ? <groupId>org.codehaus.mojo</groupId>
? ? <artifactId>my-parent</artifactId>
? ? <version>2.0</version>
? ? <relativePath>../my-parent</relativePath>
? </parent>
<dependencies>
? ? <dependency>
? ? ? <groupId>junit</groupId>
? ? ? <artifactId>junit</artifactId>
? ? ? <version>4.12</version>
? ? ? <type>jar</type>
? ? ? <scope>test</scope>
? ? ? <optional>true</optional>
? ? </dependency>
</dependencies>
?<packaging>war</packaging>
</project>
配置文檔說明:
modelVersion 指定pom當(dāng)前版本。
groupId: 組織或團(tuán)隊(duì) eg: org.apache
artifactId: 項(xiàng)目或模塊名 eg: spring
version: 當(dāng)前軟件的版本 eg:1.0.0
packaging: 項(xiàng)目投入到生產(chǎn)環(huán)境當(dāng)中前带欢,都需要對(duì)項(xiàng)目進(jìn)行一系列的打包操作运授,所以需要指定打包成什么,eg:war乔煞、jar等等吁朦。默認(rèn)
是jar,主要包類型:pom,jar,maven-plugin,ejb,war,ear,rar.
dependencies: 項(xiàng)目的依賴列表渡贾,maven會(huì)自動(dòng)從maven倉庫當(dāng)中下載指定的jar包逗宜,groupId、artifactId空骚、version的含義和上方的一模一樣纺讲。
dependencies-type:指定文件的后綴擴(kuò)展名,默認(rèn)是jar
dependencies-scope:與處理任務(wù)時(shí)相關(guān)的類路徑囤屹,如何限制依賴的傳遞性熬甚。有compile: 僅在編譯期間提供;provided:編譯器提供,并且說明到運(yùn)行的時(shí)候希望jdk或者容器提供它肋坚。runtime:僅僅在執(zhí)行期間提供乡括,如jdbc的驅(qū)動(dòng)。test:測試的編譯和執(zhí)行階段提供智厌。system:和provided相似诲泌,總是是可用的,不需要從倉庫當(dāng)中去查找铣鹏。
dependencies-systemPath:僅僅在scope為system的時(shí)候用敷扫,否則構(gòu)建就會(huì)報(bào)錯(cuò)。路徑必須是絕對(duì)的吝沫,指定特定的系統(tǒng)路徑呻澜,eg: ${JAVA_HOME},maven就不會(huì)從maven倉庫當(dāng)中查找,而是去 systemPath指定的路徑查找惨险。
depenpendencies-optional:當(dāng)一個(gè)項(xiàng)目A依賴另一個(gè)項(xiàng)目B時(shí)羹幸,項(xiàng)目A可能很少一部分功能用到了項(xiàng)目B,此時(shí)就可以在A中配置對(duì)B的可選依賴辫愉。舉例來說栅受,一個(gè)類似hibernate的項(xiàng)目,它支持對(duì)mysql、oracle等各種數(shù)據(jù)庫的支持屏镊,但是在引用這個(gè)項(xiàng)目時(shí)依疼,我們可能只用到其對(duì)mysql的支持,此時(shí)就可以在這個(gè)項(xiàng)目中配置可選依賴而芥,也就是說律罢,所依賴的項(xiàng)目可能下載也可能下載。
? ? 配置可選依賴的原因:1棍丐、節(jié)約磁盤误辑、內(nèi)存等空間;2歌逢、避免license許可問題巾钉;3、避免類路徑問題秘案,等等砰苍。
depenpendencies-exclusion:排除依賴,明確不依賴阱高、不下載赚导。
項(xiàng)目依賴
當(dāng)項(xiàng)目的packaging設(shè)置為pom的時(shí)候,常常用于父工程或者聚合工程(多個(gè)項(xiàng)目模塊)讨惩。pom可以繼承的配置:groupId version description url inceptionYear
organization licenses developers contributors mailingLists scm issueManagement ciManagement properties dependencyManagement dependencies repositories pluginRepositories build plugin executions with matching ids plugin configuration etc. reporting profiles
不會(huì)繼承的元素:artifactId name prerequisites
要注意的是:relativePath 并不是必須的辟癌,指明先在給出的路徑中去查找,如果找不到才會(huì)去本地或者遠(yuǎn)程倉庫當(dāng)中查找荐捻。
超級(jí)pom(super pom)
所有的pom都會(huì)繼承此pom,應(yīng)當(dāng)詳細(xì)了解該pom寡夹,可以通過mvn help:effective-pom來查看最后打包的pom处面,因?yàn)檫@會(huì)影響構(gòu)建的pom,該pom內(nèi)容如下:
<project>
? <modelVersion>4.0.0</modelVersion>
? <repositories>
? ? <repository>
? ? ? <id>central</id>
? ? ? <name>Central Repository</name>
? ? ? <url>http://repo.maven.apache.org/maven2</url>
? ? ? <layout>default</layout>
? ? ? <snapshots>
? ? ? ? <enabled>false</enabled>
? ? ? </snapshots>
? ? </repository>
? </repositories>
? <pluginRepositories>
? ? <pluginRepository>
? ? ? <id>central</id>
? ? ? <name>Central Repository</name>
? ? ? <url>http://repo.maven.apache.org/maven2</url>
? ? ? <layout>default</layout>
? ? ? <snapshots>
? ? ? ? <enabled>false</enabled>
? ? ? </snapshots>
? ? ? <releases>
? ? ? ? <updatePolicy>never</updatePolicy>
? ? ? </releases>
? ? </pluginRepository>
? </pluginRepositories>
? <build>
? ? <directory>${project.basedir}/target</directory>
? ? <outputDirectory>${project.build.directory}/classes</outputDirectory>
? ? <finalName>${project.artifactId}-${project.version}</finalName>
? ? <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
? ? <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
? ? <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
? ? <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
? ? <resources>
? ? ? <resource>
? ? ? ? <directory>${project.basedir}/src/main/resources</directory>
? ? ? </resource>
? ? </resources>
? ? <testResources>
? ? ? <testResource>
? ? ? ? <directory>${project.basedir}/src/test/resources</directory>
? ? ? </testResource>
? ? </testResources>
? ? <pluginManagement>
? ? ? <!-- NOTE: These plugins will be removed from future versions of the super POM -->
? ? ? <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
? ? ? <plugins>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-antrun-plugin</artifactId>
? ? ? ? ? <version>1.3</version>
? ? ? ? </plugin>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-assembly-plugin</artifactId>
? ? ? ? ? <version>2.2-beta-5</version>
? ? ? ? </plugin>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-dependency-plugin</artifactId>
? ? ? ? ? <version>2.1</version>
? ? ? ? </plugin>
? ? ? ? <plugin>
? ? ? ? ? <artifactId>maven-release-plugin</artifactId>
? ? ? ? ? <version>2.0</version>
? ? ? ? </plugin>
? ? ? </plugins>
? ? </pluginManagement>
? </build>
? <reporting>
? ? <outputDirectory>${project.build.directory}/site</outputDirectory>
? </reporting>
? <profiles>
? ? <!-- NOTE: The release profile will be removed from future versions of the super POM -->
? ? <profile>
? ? ? <id>release-profile</id>
? ? ? <activation>
? ? ? ? <property>
? ? ? ? ? <name>performRelease</name>
? ? ? ? ? <value>true</value>
? ? ? ? </property>
? ? ? </activation>
? ? ? <build>
? ? ? ? <plugins>
? ? ? ? ? <plugin>
? ? ? ? ? ? <inherited>true</inherited>
? ? ? ? ? ? <artifactId>maven-source-plugin</artifactId>
? ? ? ? ? ? <executions>
? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? <id>attach-sources</id>
? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? <goal>jar</goal>
? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? </execution>
? ? ? ? ? ? </executions>
? ? ? ? ? </plugin>
? ? ? ? ? <plugin>
? ? ? ? ? ? <inherited>true</inherited>
? ? ? ? ? ? <artifactId>maven-javadoc-plugin</artifactId>
? ? ? ? ? ? <executions>
? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? <id>attach-javadocs</id>
? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? <goal>jar</goal>
? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? </execution>
? ? ? ? ? ? </executions>
? ? ? ? ? </plugin>
? ? ? ? ? <plugin>
? ? ? ? ? ? <inherited>true</inherited>
? ? ? ? ? ? <artifactId>maven-deploy-plugin</artifactId>
? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? <updateReleaseInfo>true</updateReleaseInfo>
? ? ? ? ? ? </configuration>
? ? ? ? ? </plugin>
? ? ? ? </plugins>
? ? ? </build>
? ? </profile>
? </profiles>
</project>
依賴管理(dependencyManagement)
資料參見:https://www.cnblogs.com/feibazhf/p/7886617.html
主要管理版本菩掏,對(duì)于子類繼承同一個(gè)父類是很有用的魂角,集中管理依賴版本不添加依賴關(guān)系,對(duì)于其中定義的版本智绸,子pom不一定要繼承父pom所定義的版本野揪。
dependencies即使在子項(xiàng)目中不寫該依賴項(xiàng),那么子項(xiàng)目仍然會(huì)從父項(xiàng)目中繼承該依賴項(xiàng)(全部繼承)
dependencyManagement里只是聲明依賴瞧栗,并不實(shí)現(xiàn)引入斯稳,因此子項(xiàng)目需要顯示的聲明需要用的依賴。如果不在子項(xiàng)目中聲明依賴迹恐,是不會(huì)從父項(xiàng)目中繼承下來的挣惰;只有在子項(xiàng)目中寫了該依賴項(xiàng),并且沒有指定具體版本,才會(huì)從父項(xiàng)目中繼承該項(xiàng)憎茂,并且version和scope都讀取自父pom;另外如果子項(xiàng)目中指定了版本號(hào)珍语,那么會(huì)使用子項(xiàng)目中指定的jar版本。
eg:
<dependencyManagement>
? ? ? ? <dependencies>?
? ? ? ? ? ? <dependency>?
? ? ? ? ? ? ? ? <groupId>org.eclipse.persistence</groupId>?
? ? ? ? ? ? ? ? <artifactId>org.eclipse.persistence.jpa</artifactId>?
? ? ? ? ? ? ? ? <version>${org.eclipse.persistence.jpa.version}</version>?
? ? ? ? ? ? ? ? <scope>provided</scope>?
? ? ? ? ? ? </dependency>
? ? ? ? ? ? <dependency>?
? ? ? ? ? ? ? ? <groupId>javax</groupId>?
? ? ? ? ? ? ? ? <artifactId>javaee-api</artifactId>?
? ? ? ? ? ? ? ? <version>${javaee-api.version}</version>?
? ? ? ? ? ? </dependency>?
? ? ? ? </dependencies>?
? ? </dependencyManagement>