Maven工程依賴,聚合,繼承學習筆記

學習一下maven,整理一下比較混淆的概念和使用方法,記錄筆記java

1:Maven工程依賴web

在Eclipse建立三個獨立的Maven工程:web工程、service工程、dao工程,pom.xml分別以下:apache

web工程:依賴serviceapi

<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.paic.maven.cms</groupId>
	<artifactId>web</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>web Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<dependencies>
		<dependency>
			<groupId>com.paic.maven.cms</groupId>
			<artifactId>service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>

	<build>
		<finalName>web</finalName>
	</build>
</project>

service工程:依賴daomybatis

<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>com.paic.maven.cms</groupId>
	<artifactId>service</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>com.paic.maven.cms</groupId>
			<artifactId>dao</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
	</dependencies>
</project>

dao工程app

<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>com.paic.maven.cms</groupId>
	<artifactId>dao</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
</project>

在classpath中體現出的結果就是:maven

service工程:學習

web工程:ui

能夠看到web工程將service工程引用到了classpath下,而且經過service工程的依賴傳遞同時依賴了dao工程。效果很直觀。url

下面對web工程打包:maven install

報錯信息以下:

[ERROR] Failed to execute goal on project web: Could not resolve dependencies for project com.paic.maven.cms:web:war:0.0.1-SNAPSHOT: Could not find artifact com.paic.maven.cms:service:jar:0.0.1-SNAPSHOT -> [Help 1]
[ERROR]

沒有找到service的artifact,也就是沒有找到service工程的jar

下面對service工程執行maven install,報錯信息相似,沒有找到dao的jar

沒辦法,依次對dao,service,web工程執行install操做

執行完畢以後

[INFO] Installing E:\maven_test_workspace\web\target\web.war to e:\repository\com\paic\maven\cms\web\0.0.1-SNAPSHOT\web-0.0.1-SNAPSHOT.war
[INFO] Installing E:\maven_test_workspace\web\pom.xml to e:\repository\com\paic\maven\cms\web\0.0.1-SNAPSHOT\web-0.0.1-SNAPSHOT.pom

能夠看到在工程下,生成了war包,而且拷貝到了maven的repository下

service和dao文件夾下生成的就是其pom.xml中定義的jar,而web工程則是生成war

打開web工程裏面生成的war包,lib以下:

依賴的jar包被自動打到lib下

這樣雖然實現了依賴,可是還須要一個一個獨立的按照依賴順序分別打包,比較麻煩。這是第一個問題;

另一個問題

修改dao工程的pom.xml,新增一個依賴

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
	</dependencies>

這裏依賴了junit,若是要使用該jar的類,則dao,service,web均可以使用,由於它們存在依賴順序。可是若是把junit的依賴放在service中,則dao就不能使用了,也是由於它們存在依賴順序。

因此,依賴放在最底層,則其上面的工程就無條件引入,若是jar屢次引用,會形成版本衝突,混淆;若是放在上層,則底層沒法享受引用,比較麻煩:jar包依賴和工程依賴混在一塊兒。。。

2:使用maven聚合

聚合的做用能夠解決第一個問題:打包問題

簡述一下就是擁有一個父maven工程,它包含了不少子模塊,當對父maven工程打包時,maven會自動的將其聚合的子模塊一塊兒打包,再也不須要一個一個install了

首先建立一個父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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.paic.maven.aggregate</groupId>
	<artifactId>aggregate-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<modules>
		<module>../aggregate-service</module>
		<module>../aggregate-web</module>
	</modules>

</project>

注意這裏的打包packing必須爲pom,且多了一個節點<modules>,其內容就表明了它要聚合的模塊。下面就看看兩個子模塊的pom.xml
aggregate-web

<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">
	
	<groupId>com.paic.maven.aggregate</groupId>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>aggregate-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>com.paic.maven.aggregate</groupId>
			<artifactId>aggregate-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
</project>

aggregate-service

<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">
	
	<groupId>com.paic.maven.aggregate</groupId>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>aggregate-web</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>com.paic.maven.aggregate</groupId>
			<artifactId>aggregate-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
</project>

這樣aggregate-web和aggregate-service都被聚合到了aggregate-parent下,而且aggregate-web依賴了aggregate-service,下面對aggregate-parent執行mvn install

執行成功,結果爲:

而且打開aggregate-web中的war包,查看lib

不但把servlet的jar打入,而且也把aggregate-service的jar打入了,消除了最開始時必須按順序對每一個工程執行install的問題

3:使用maven繼承

copy一些maven繼承的概念和優勢:

這比較符合的解決第二個問題,解決的問題簡述:擁有一個父maven工程,它提供統一的jar依賴,子工程無須再次引入,而且父maven提供的依賴子類能夠自主選擇,不會強制依賴全部

爲了簡單起見,直接在上面的聚合例子中修改

修改父類aggregate-parent的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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.paic.maven.aggregate</groupId>
	<artifactId>aggregate-parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<modules>
		<module>../aggregate-service</module>
		<module>../aggregate-web</module>
	</modules>

	<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
	<dependencies>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>
	</dependencies>

	<!-- dependencyManagement中的依賴不會被子項目依賴,其目的之一是提供各類依賴,供子項目按需自主選擇 -->
	<dependencyManagement>
		<dependencies>
			<!-- https://mvnrepository.com/artifact/junit/junit -->
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.11</version>
			</dependency>
		</dependencies>
	</dependencyManagement>

</project>

新增了一些節點

aggregate-web的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/xsd/maven-4.0.0.xsd">

	<parent>
		<groupId>com.paic.maven.aggregate</groupId>
		<artifactId>aggregate-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../aggregate-parent/pom.xml</relativePath>
	</parent>
	
	<modelVersion>4.0.0</modelVersion>
	<artifactId>aggregate-web</artifactId>
	<packaging>war</packaging>

	<dependencies>
		<dependency>
			<groupId>com.paic.maven.aggregate</groupId>
			<artifactId>aggregate-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
</project>

這裏主要是新增了<parent>節點,目的就是要找到父pom,而且因爲繼承關係,<groupId>和<version>直接繼承了父類,自己無須再次定義,只須要定義個性化的<artifactId>,還有<relativePath>

aggregate-service

<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">
	<parent>
		<groupId>com.paic.maven.aggregate</groupId>
		<artifactId>aggregate-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../aggregate-parent/pom.xml</relativePath>
	</parent>

	<modelVersion>4.0.0</modelVersion>
	<artifactId>aggregate-service</artifactId>

</project>

和上面相似

主要看一下aggregate-web的maven依賴:

能夠看到web中並無定義mybatis的依賴,可是它classpath中仍有其jar,這就是從parent中繼承而來,同時包括了service的依賴;另外一方面,web中還顯式的引用了父類junit4.11,故jar也存在

看看aggregate-service的依賴:

和web相似,引入了mybatis,卻沒有junit,實現了自主選擇依賴~!

打個包,三個均成功,還比較順利·!

相關文章
相關標籤/搜索