Maven 使用指南 8 使用Maven管理多個專案
阿新 • • 發佈:2018-11-08
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
可以使用Maven來管理多個專案,首先是新增一個父pom.xml 檔案,在所需管理專案的上一級,還是以hibernate tutorial 專案為例:
其中父pom.xml 的packaging 必須為 pom 型別
<?xml version="1.0" encoding="UTF-8"?><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 > <groupId>org.hibernate.tutorials</groupId> <artifactId>hibernate-tutorials</artifactId> <version>4.1.6.Final</version> <packaging>pom</packaging> <name>Hibernate Getting Started Guide Tutorials</name> <description0 > Aggregator for the Hibernate tutorials presented in the Getting Started Guide </description> <modules> <module>basic</module> <module>annotations</module> <module>entitymanager</module> <module>envers</module> </modules> <properties> <maven.deploy.skip>true</maven.deploy.skip> </properties> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.1.6.Final</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.2.145</version> <scope>compile</scope> </dependency> </dependencies> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Maven Repository Switchboard</name> <url>http://repo1.maven.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Maven Plugin Repository</name> <url>http://repo1.maven.org/maven2</url> </pluginRepository> </pluginRepositories> <build> <plugins> .... </plugins> </build> <reporting> ... </reporting></project>
此時可以把需管理的專案的共用的dependencies ,plugin 移動到這個父pom.xml ,然後使用modules 新增到父pom.xml 中。
修改子專案的pom.xml ,新增一個parent 元素。比如 basic 專案的 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> <parent> <groupId>org.hibernate.tutorials</groupId> <artifactId>hibernate-tutorials</artifactId> <version>4.1.6.Final</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>hibernate-tutorial-hbm</artifactId> <name>Hibernate hbm.xml Tutorial</name> <description> Hibernate tutorial illustrating the use of native APIs and hbm.xml for mapping metadata </description> <properties> <!-- Skip artifact deployment --> <maven.deploy.skip>true</maven.deploy.skip> </properties></project>
如果使用eclipse IDE ,就更簡單了,直接通過modules 來管理模組:
本文是Maven系列教程最後一篇。