现在的位置: 首页 > 综合 > 正文

Maven 使用指南(8): 使用Maven管理多个项目

2013年10月15日 ⁄ 综合 ⁄ 共 2826字 ⁄ 字号 评论关闭

可以使用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系列教程最后一篇。

抱歉!评论已关闭.