maven中的聚合和继承

maven中的聚合,其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>

 <groupId>com.juvenxu.mvnbook</groupId>
 <artifactId>hello-world-m2e</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>pom</packaging>

 <name>hello-world-m2e</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
  
      <modules>
          <module>account-email</module>
          <module>account-persist</module>
      </modules>
 </project>


其中这里的第一个特殊的地方为packaging,其中值为POM。一般的默认值为jar

而modules为聚合的核心配置,用户可以通过在一个大包方式为pom的Maven项目中声明任意数量的module元素来实现模块的聚合。这里每个module值都是一个当前POM的相对目录。\


继承

<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>com.juvenxu.mvnbook.account</groupId>
   <artifactId>hello-world-m2e</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <relativePath>../account-parent/pom.xml</relativePath>
      </parent>

<project/>


上述POM使用parent元素声明父模块,parent下的子元素groupId、artifactId和version指定了父模块的坐标,这三个元素是必须的。元素relativePath表示父模块POM的相对路径。

依赖管理(dependencyManagement)


Maven提供的dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。在dependencyManagement元素下的依赖声明不会引入实际的依赖,不过它能够约束dependencies下的依赖使用。

   <properties>
    <springframework.version>2.5.6</springframework.version>
    <junit.version>4.7</junit.version>       
   </properties>

如果在父类中声明了dependncyManagement
<dependencyManagement>
    <dependencies>
        <dependency>
             <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
               <version>${springframework.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
 

那么子类如需要在声明version元素从而简化了配置。主要用于在父类中声明版本信息。


插件管理(pluginManagement)

  <build>
   <pluginManagement>
       <plugins>
           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-source-plugin</artifactId>
                  <version>2.1.1</version>
                   <executions>
                      <execution>
                         <id>attach-sources</id>
                          <phase>verify</phase>
                          <goals>
                             <goal>jar-no-fork</goal> 
                          </goals>
                      </execution>
                   </executions>
            </plugin>
        </plugins>
   </pluginManagement>
 </build>

当子模块需要生成源码包的时候,只需要如下简单配置:

<build>
  <plugins>
     <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-source-plugin</artifactId>
        </plugin>
     </plugins>
 </build>

猜你喜欢

转载自liuwuhen.iteye.com/blog/1678778
今日推荐