工作笔记-Maven继承:创建一个Parent工程

继承是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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值