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

来源:互联网 发布:sql求和语句查询 编辑:程序博客网 时间:2024/05/29 07:55

可以使用Maven来管理多个项目,首先是添加一个父pom.xml 文件,在所需管理项目的上一级,还是以hibernate tutorial 项目为例:

其中父pom.xml 的packaging 必须为 pom 类型

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  3.   
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd"  
  5.   
  6.     xmlns="http://maven.apache.org/POM/4.0.0"  
  7.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  8.   <modelVersion>4.0.0</modelVersion>  
  9.   <groupId>org.hibernate.tutorials</groupId>  
  10.   <artifactId>hibernate-tutorials</artifactId>  
  11.   <version>4.1.6.Final</version>  
  12.   <packaging>pom</packaging>  
  13.   <name>Hibernate Getting Started Guide Tutorials</name>  
  14.   <description0>  
  15.      Aggregator for the Hibernate tutorials presented in the Getting Started Guide  
  16.  </description>  
  17.   <modules>  
  18.     <module>basic</module>  
  19.     <module>annotations</module>  
  20.     <module>entitymanager</module>  
  21.     <module>envers</module>  
  22.   </modules>  
  23.   <properties>  
  24.     <maven.deploy.skip>true</maven.deploy.skip>  
  25.   </properties>  
  26.   <dependencies>  
  27.     <dependency>  
  28.       <groupId>org.hibernate</groupId>  
  29.       <artifactId>hibernate-core</artifactId>  
  30.       <version>4.1.6.Final</version>  
  31.       <scope>compile</scope>  
  32.     </dependency>  
  33.     <dependency>  
  34.       <groupId>org.slf4j</groupId>  
  35.       <artifactId>slf4j-simple</artifactId>  
  36.       <version>1.6.1</version>  
  37.       <scope>compile</scope>  
  38.     </dependency>  
  39.     <dependency>  
  40.       <groupId>junit</groupId>  
  41.       <artifactId>junit</artifactId>  
  42.       <version>4.10</version>  
  43.       <scope>compile</scope>  
  44.     </dependency>  
  45.     <dependency>  
  46.       <groupId>com.h2database</groupId>  
  47.       <artifactId>h2</artifactId>  
  48.       <version>1.2.145</version>  
  49.       <scope>compile</scope>  
  50.     </dependency>  
  51.   </dependencies>  
  52.   <repositories>  
  53.     <repository>  
  54.       <snapshots>  
  55.         <enabled>false</enabled>  
  56.       </snapshots>  
  57.       <id>central</id>  
  58.       <name>Maven Repository Switchboard</name>  
  59.       <url>http://repo1.maven.org/maven2</url>  
  60.     </repository>  
  61.   </repositories>  
  62.   <pluginRepositories>  
  63.     <pluginRepository>  
  64.       <releases>  
  65.         <updatePolicy>never</updatePolicy>  
  66.       </releases>  
  67.       <snapshots>  
  68.         <enabled>false</enabled>  
  69.       </snapshots>  
  70.       <id>central</id>  
  71.       <name>Maven Plugin Repository</name>  
  72.       <url>http://repo1.maven.org/maven2</url>  
  73.     </pluginRepository>  
  74.   </pluginRepositories>  
  75.   <build>  
  76.     <plugins>  
  77.        ....  
  78.     </plugins>  
  79.   </build>  
  80.   <reporting>  
  81.     ...  
  82.   </reporting>  
  83. </project>  

此时可以把需管理的项目的共用的dependencies ,plugin 移动到这个父pom.xml ,然后使用modules 添加到父pom.xml 中。

修改子项目的pom.xml ,添加一个parent 元素。比如 basic 项目的 pom.xml

[html] view plaincopyprint?
  1. project xmlns="http://maven.apache.org/POM/4.0.0"   
  2.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  4.   
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  6.   
  7.     <modelVersion>4.0.0</modelVersion>  
  8.   
  9.     <parent>  
  10.         <groupId>org.hibernate.tutorials</groupId>  
  11.         <artifactId>hibernate-tutorials</artifactId>  
  12.         <version>4.1.6.Final</version>  
  13.         <relativePath>../pom.xml</relativePath>  
  14.     </parent>  
  15.   
  16.     <artifactId>hibernate-tutorial-hbm</artifactId>  
  17.     <name>Hibernate hbm.xml Tutorial</name>  
  18.     <description>  
  19.        Hibernate tutorial illustrating the use of native APIs and hbm.xml for mapping metadata  
  20.     </description>  
  21.   
  22.     <properties>  
  23.         <!-- Skip artifact deployment -->  
  24.         <maven.deploy.skip>true</maven.deploy.skip>  
  25.     </properties>  
  26.   
  27. </project>  

如果使用eclipse IDE ,就更简单了,直接通过modules 来管理模块:

本文是Maven系列教程最后一篇。

0 0
原创粉丝点击