部落格已轉至:[url]http://www.muxuanli.com/lmx/[/url]
【聚合】類似的多個子產品的項目,我們在建構時,會希望一次建構完成,而不是每個子產品都執行mvn指令。Maven的聚合特性可是實作此需求。
【繼承】在parent項目中定義一些依賴,可以避免在其它子項目中重複定義,其<packaging>的值也是 pom
以admin項目為例 包括version配置等資訊
admin-common 主要是POJO entity vo 等
admin-biz 主要進行一些業務操作(依賴biz)
admin-web web項目 依賴common/biz
admin-parent 主要是定義項目中依賴的jar包的版本等,用于繼承
[b]在以上四個目錄之上,可以建構一個新的項目admin,該項目隻有一個pom檔案,其特殊之處在于:[/b]
1. <packaging>的值 是 pom , 而不是預設的jar
2. 使用了modules元素,實作多子產品的聚合
admin
--- admin-common
--- admin-biz
--- admin-web
--- admin-parent
--- pom.xml
[i][b]step1 建立總的聚合檔案夾 admin,裡面隻有一個 pom.xml 檔案[/b][/i]
[i][b]step2 在admin目錄下建立parent項目,裡面也隻有一個pom.xml 檔案[/b][/i]
<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.lmx.test</groupId>
<artifactId>admin-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Admin Parent</name>
</project>
[i][b]step3 分别建立common,biz,web項目[/b][/i]
以common項目為例:在admin目錄下執行mvn指令:
mvn archetype:generate -DgroupId=com.lmx.test -DartifactId=admin-common -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
建立admin-web項目:
mvn archetype:generate -DgroupId=com.lmx.test -DartifactId=admin-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractivMode=false
檢視admin-common的pom檔案,已經繼承了parent:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lmx.test</groupId>
<artifactId>admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.lmx.test</groupId>
<artifactId>admin-common</artifactId>
<version>1.0-SNAPSHOT</version>
<name>admin-common</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
其中 groupId version等是可以繼承的,是以可以删除掉!
檢視admin下的pom檔案:modules元素下已自動加入了3個子子產品
<?xml version="1.0" encoding="UTF-8"?>
<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.lmx.test</groupId>
<artifactId>admin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Admin Project</name>
<modules>
<module>admin-common</module>
<module>admin-biz</module>
<module>admin-web</module>
</modules>
</project>
[i][b]step4 eclipse引入[/b][/i]