maven多模块工程的优化原则

来源:互联网 发布:淘宝网妈妈装棉袄 编辑:程序博客网 时间:2024/06/15 21:10
  1. 1、将各个工程中用到的重复jar包提取到parent的pom.xml中,通过properties属性标注版本信息,子模块只记录jar包,而不记录版本号  
  2.     父模块定义如下:  
  3.     <properties>  
  4.       
  5.         <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>  
  6.       
  7.         <hsqldb.version>1.8.0.7</hsqldb.version>  
  8.     
  9.     </properties>  
  10.     <dependencyManagement>  
  11.         <dependencies>  
  12.             <dependency>  
  13.                 <groupId>org.hibernate</groupId>  
  14.                 <artifactId>hibernate-annotations</artifactId>  
  15.                 <version>${hibernate.annotations.version}</version>  
  16.             </dependency>  
  17.             <dependency>  
  18.                 <groupId>org.hibernate</groupId>  
  19.                 <artifactId>hibernate-commons-annotations</artifactId>  
  20.                 <version>${hibernate.annotations.version}</version>  
  21.             </dependency>  
  22.         <dependencies>  
  23.     <dependencyManagement>  
  24.     子模块中定义如下:  
  25.     <dependencies>  
  26.         <dependency>  
  27.             <groupId>${project.groupId}</groupId>  
  28.             <artifactId>simple-model</artifactId>  
  29.             <version>${project.version}</version>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.hibernate</groupId>  
  33.             <artifactId>hibernate</artifactId>  
  34.         </dependency>  
  35.         <dependency>  
  36.             <groupId>org.hibernate</groupId>  
  37.             <artifactId>hibernate-annotations</artifactId>  
  38.         </dependency>  
  39.         <dependency>  
  40.             <groupId>org.hibernate</groupId>  
  41.             <artifactId>hibernate-commons-annotations</artifactId>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>org.apache.geronimo.specs</groupId>  
  45.             <artifactId>geronimo-jta_1.1_spec</artifactId>  
  46.             <version>1.1</version>  
  47.         </dependency>  
  48.         <dependency>  
  49.             <groupId>org.springframework</groupId>  
  50.             <artifactId>spring</artifactId>  
  51.         </dependency>  
  52.     </dependencies>  
  53. 2、子模块都应用父模块的grouId和version,  
  54.   
  55.     <parent>  
  56.         <groupId>org.sonatype.mavenbook</groupId>  
  57.         <artifactId>simple-parent</artifactId>  
  58.         <version>1.0</version>  
  59.         <!-- 如果没有该属性则该工程必须放到simpe-parent下-->  
  60.         <relativePath>../simple-parent/pom.xml</relativePath>  
  61.     </parent>  
  62.   
  63.     <artifactId>simple-persist</artifactId>  
  64.     <packaging>jar</packaging>  
  65.   
  66.     同时,该模块依赖其他模块时采用如下方式配置:  
  67.     <dependency>  
  68.         <groupId>${project.groupId}</groupId>  
  69.         <artifactId>simple-model</artifactId>  
  70.         <version>${project.version}</version>  
  71.     </dependency>  
  72. 3、采用mvn dependency:analyze将每个工程中的间接依赖添加到每个模块的工程中。