首页 > 代码库 > Maven继承
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.0http://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.0http://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>