Maven继承

Posted wzjhoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven继承相关的知识,希望对你有一定的参考价值。

父pom配置:
<packaging>pom</packaging>
在父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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>cxxcode.maven</groupId>
 <artifactId>account-parent</artifactId>
 <packaging>pom</packaging>
 <version>0.0.1</version>
 <name>Account Parent</name>
 <url>http://maven.apache.org</url>

 <properties>
  <spring-version>2.5.6</spring-version>
  <junit-version>4.7</junit-version>
  <mail-version>1.4.1</mail-version>
  <greenmail-version>1.3.1b</greenmail-version>
  <dom4j-version>1.6.1</dom4j-version>
  <surefire-version>2.5</surefire-version>
 </properties>


 <!--
  依赖管理dependencyManagement
   假设不使用依赖管理。子pom会继承父pom中配置的全部依赖,子pom中仅仅需配置自己特需的依赖即可了,
   这样尽管简化了子pom的配置。可是强制性给子pom加上多余的依赖。
   
   使用依赖管理,子pom中要配置自己须要的依赖。假设这些依赖在父pom中配置了,
   子pom仅仅需指定这些依赖的groupId和artifactId
   
   
  依赖管理的导入
   将proj-A中的依赖管理导入此pom的依赖管理中
   
   <scope>import</scope>仅仅用在依赖管理配置中。且<type>pom</type>
  
   <dependencyManagement>
    <dependencies>
     <dependency>
      <groupId>cxxcode.mave</groupId>
      <artifactId>proj-A</artifactId>
      <version>0.0.1</version>
      <type>pom</type>
      <scope>import</scope>
     </dependency>
    </dependencies>
   </dependencyManagement>
 -->
 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring-version}</version>
   </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring-version}</version>
   </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-version}</version>
   </dependency>
   <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring-version}</version>
   </dependency>
   <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>${mail-version}</version>
   </dependency>

   <dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>${dom4j-version}</version>
   </dependency>

   <dependency>
    <groupId>com.icegreen</groupId>
    <artifactId>greenmail</artifactId>
    <version>${greenmail-version}</version>
    <scope>test</scope>
   </dependency>
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit-version}</version>
    <scope>test</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 
 <build>
  <!-- 插件管理pluginManagement -->
  <pluginManagement>
   <plugins>
    <!-- 编译插件 -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <configuration>
      <source>1.5</source>
      <target>1.5</target>
      <fork>true</fork><!-- true同意改动jdk编码版本号 -->
      <encoding>UTF-8</encoding>
      <!-- 跳过測试编译
      <skip>false</skip> -->
     </configuration>
    </plugin>
    
    <!-- 生成源代码jar -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-source-plugin</artifactId>
     <executions>
      <execution>
       <phase>verify</phase>
       <goals>
        <goal>jar-no-fork</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
    
    <!-- 复制主资源文件到主输出文件夹 -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <configuration>
      <encoding>UTF-8</encoding>
     </configuration>
    </plugin>
    
    <!-- 測试插件 -->
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>${surefire-version}</version>
     <configuration>
      <!-- 跳过測试
      <skip>true</skip> -->
     </configuration>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>
</project>
==================================

子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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <!-- 独自己定义
 <groupId>cxxcode.maven</groupId>
 <artifactId>account-email</artifactId>
 <packaging>jar</packaging>
 <version>0.0.1</version>
 <name>Account Email</name>
  -->
 
 <!--
  parent
  继承父pom
   子pom的groupId、version、依赖等信息继承父pom
   relativePath 父pom的位置。默认../pom.xml
   
  可继承的pom元素
   groupId   项目组Id
   version   项目版本号
   description  项目描写叙述
   organization 项目的组织信息
   inceptionYear 项目的创建年份
   url    项目的url
   developers  项目的开发人员
   contributors 项目的贡献者
   distributionManagement 项目的部署配置
   issueManagement 项目的缺陷跟踪系统信息
   ciManagement 项目的持续继承系统信息
   scm    项目的版本号控制系统信息
   mailingLists 项目的邮件列表信息
   properties  自己定义的maven属性
   dependencies 项目的依赖配置
   dependencyManagement 项目的依赖管理配置
   repositories 项目的仓库配置
   build   包含项目的源代码文件夹配置,输出文件夹配置、插件配置、插件管理配置等
   reporting  包含项目的报告输出文件夹配置、报告插件配置等
   
  -->
 <artifactId>account-email</artifactId>
 <packaging>jar</packaging>
 <name>Account Email</name>
 
 <parent>
  <groupId>cxxcode.maven</groupId>
  <artifactId>account-parent</artifactId>
  <version>0.0.1</version>
  <relativePath>../CXXMaven_AccountParent/pom.xml</relativePath>
 </parent>

 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.mail</groupId>
   <artifactId>mail</artifactId>
  </dependency>
  <dependency>
   <groupId>com.icegreen</groupId>
   <artifactId>greenmail</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
  </dependency>

 </dependencies>
 <build>
  <plugins>
   <!-- 编译插件 -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
   </plugin>

   <!-- 生成可执行的jar -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
     <execution>
      <phase>package</phase>
      <goals>
       <goal>shade</goal>
      </goals>
      <configuration>
       <transformers>
        <transformer
     implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
         <mainClass>
          com.cxx.maven.App
         </mainClass>
        </transformer>
       </transformers>
      </configuration>
     </execution>
    </executions>
   </plugin>

   <!-- 生成源代码jar -->
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>



































































































































































































































































以上是关于Maven继承的主要内容,如果未能解决你的问题,请参考以下文章

Maven -- 模块继承(子模块无需写依赖的version)

maven 聚合与继承

Maven中聚合与继承

maven如何继承父工程的依赖关系

Maven基础2

Maven 继承与聚合,会用吗?