继承是Maven三大特点之一,创建一个Parent工程方便集中管理依赖关系和配置构建。
可以通过几个标签来配置父工程。
1. dependencyManagement
在<dependencyManagement>中配置的依赖和版本,在所有子工程中都有效。
<dependencyManagement>
<dependencies>
<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.42.Final</version>
</dependency>
<!-- netty end -->
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
子工程中再配置<dependency>及<version>, 可以覆盖父类的配置。
2. <pluginManagement>
类似<dependencyManagement>这个标签的作用是配置plugin并在所有子工程中生效。同样在子类中的详细配置可以覆盖父类中的配置。
<build>
<pluginManagement>
<plugins>
<!--打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--默认plugin: maven-resources-plugin, maven-compiler-plugin, maven-war-plugin -->
</plugins>
</pluginManagement>
</build>
3. <repositories>
远程代码仓不需要在每一个模块中都配置一遍,在父类中配置好就可以了。
<repositories>
<repository>
<id>aliyunmaven</id>
<name>阿里云中央仓库</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
4. <distributionManagement>
同理。
一个完整的Maven Parent配置示例
<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>
<name>Netbar-parent</name>
<description>Nerbar Parent Project</description>
<groupId>cn.jim</groupId>
<artifactId>Netbar-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<!-- netty end -->
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring mvc end -->
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<!--打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!--默认plugin: maven-resources-plugin, maven-compiler-plugin, maven-war-plugin -->
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>aliyunmaven</id>
<name>阿里云中央仓库</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<distributionManagement>
<repository></repository>
<snapshotRepository></snapshotRepository>
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netty.version>4.0.42.Final</netty.version>
<spring.version>3.2.4.RELEASE</spring.version>
<cxf.version>2.7.0</cxf.version>
<quartz.version>2.2.3</quartz.version>
<slf4j-version>1.7.12</slf4j-version>
</properties>
</project>
总结
创建一个parent工程,将比较统一的配置放在parent中,方便管理,推荐的标签:<dependencyManagement><pluginManagement><repositories><distributionManagement>