利用Maven继承关系简化项目POM配置

昨天看到有人问依赖管理的问题,因此就想把本身在使用maven时的经验与你们进行分享。java

 

一、首先建一个root/pom.xml

 主要做用就是配置通常项目都须要的基本信息。web

如:编码,编译版本,生成eclipse项目时的编码,生成manifest描述,单元测试经常使用依赖,生成javadoc,生成source,eclipse:eclipse时自动关联source与javadoc,本地仓库配置等。spring

我定义的Final Name的格式是 ${project.artifactId}-${project.version}-r${svn.revision}-${maven.build.timestamp},其中包含了svn的rev号与构建时的时间(咱们用的是svn)apache

注意我用的maven-svn-revision-number-plugin 1.7目前不支持1.7格式的svn,官方插件目前已经升级到1.12了说是能够了,你们能够试试。api

这种格式方便咱们对产出的项目版本进行有效的跟踪(manifest中也有相关的信息)eclipse

文件名如:project1-core-1.0-SNAPSHOT-r82-20120503091343.jarmaven

<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>org.noahx</groupId>
	<artifactId>noahx-root</artifactId>
	<version>1.0</version>
	<packaging>pom</packaging>

	<properties>
		<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<maven.compile.source>1.5</maven.compile.source>
		<maven.compile.target>1.5</maven.compile.target>

		<maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version>
		<maven-eclipse-plugin.version>2.9</maven-eclipse-plugin.version>

		<maven-svn-revision-number-plugin.version>1.7</maven-svn-revision-number-plugin.version>
		<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
		<maven-war-plugin.version>2.2</maven-war-plugin.version>
		<maven-source-plugin.version>2.1.2</maven-source-plugin.version>
		<maven-javadoc-plugin.version>2.8.1</maven-javadoc-plugin.version>
		<maven-deploy-plugin.version>2.7</maven-deploy-plugin.version>

		<jmock.version>2.5.1</jmock.version>
		<junit.version>4.10</junit.version>
	</properties>


	<build>
		<plugins>
			<plugin>
				<groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
				<artifactId>maven-svn-revision-number-plugin</artifactId>
				<version>${maven-svn-revision-number-plugin.version}</version>
				<executions>
					<execution>
						<goals>
							<goal>revision</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<entries>
						<entry>
							<prefix>svn</prefix>
						</entry>
					</entries>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
				<configuration>
					<source>${maven.compile.source}</source>
					<target>${maven.compile.target}</target>
					<encoding>${project.build.sourceEncoding}</encoding>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<version>${maven-eclipse-plugin.version}</version>
				<configuration>
					<downloadSources>true</downloadSources>
					<downloadJavadocs>true</downloadJavadocs>
					<useProjectReferences>true</useProjectReferences>
					<wtpversion>2.0</wtpversion>
					<additionalConfig>
						<file>
							<!-- Text file encoding : UTF-8 -->
							<name>.settings/org.eclipse.core.resources.prefs</name>
							<content>  
                              <![CDATA[eclipse.preferences.version=1${line.separator}encoding/<project>=UTF-8${line.separator}]]>
							</content>
						</file>
						<file>
							<!-- New text file line delimiter : Unix -->
							<name>.settings/org.eclipse.core.runtime.prefs</name>
							<content>  
                              <![CDATA[eclipse.preferences.version=1${line.separator}line.separator=\n${line.separator}]]>
							</content>
						</file>
					</additionalConfig>
					<classpathContainers>
						<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6</classpathContainer>
					</classpathContainers>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>${maven-jar-plugin.version}</version>
				<configuration>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Build-Time>${maven.build.timestamp}</Build-Time>
							<SCM-Revision>${svn.revision}</SCM-Revision>
							<X-Compile-Source-JDK>${maven.compile.source}
							</X-Compile-Source-JDK>
							<X-Compile-Target-JDK>${maven.compile.target}
							</X-Compile-Target-JDK>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>${maven-war-plugin.version}</version>
				<configuration>
					<archive>
						<manifest>
							<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
						</manifest>
						<manifestEntries>
							<Build-Time>${maven.build.timestamp}</Build-Time>
							<SCM-Revision>${svn.revision}</SCM-Revision>
							<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
							<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
						</manifestEntries>
					</archive>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>${maven-source-plugin.version}</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar-no-fork</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>${maven-javadoc-plugin.version}</version>
				<configuration>
					<locale>en_US</locale>
				</configuration>
				<executions>
					<execution>
						<id>attach-javadocs</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-deploy-plugin</artifactId>
				<version>${maven-deploy-plugin.version}</version>
			</plugin>

		</plugins>

		<finalName>${project.artifactId}-${project.version}-r${svn.revision}-${maven.build.timestamp}</finalName>
	</build>

	<dependencies>

		<dependency>
			<groupId>org.jmock</groupId>
			<artifactId>jmock-junit4</artifactId>
			<version>${jmock.version}</version>
			<type>jar</type>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.jmock</groupId>
			<artifactId>jmock-legacy</artifactId>
			<version>${jmock.version}</version>
			<type>jar</type>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
			<scope>test</scope>
		</dependency>

	</dependencies>
<!--  本地artifactory配置
	<distributionManagement>
		<repository>
			<id>central</id>
			<name>My Artifactory-releases</name>
			<url>http://localhost/artifactory/libs-release-local</url>
		</repository>
		<snapshotRepository>
			<id>snapshots</id>
			<name>My Artifactory-snapshots</name>
			<url>http://localhost/artifactory/libs-snapshot-local</url>
		</snapshotRepository>
	</distributionManagement> -->
</project>

二、java项目(jar),project1/pom.xml

在parent中声明是上面的pom。这样你的项目自己的pom就瘦身了很多。ide

能够更加专一的关心你的依赖。svn

<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>org.noahx</groupId>
		<artifactId>noahx-root</artifactId>
		<version>1.0</version>
	</parent>

	<groupId>org.noahx.project1</groupId>
	<artifactId>project1-core</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<spring.version>3.0.4.RELEASE</spring.version>
	</properties>

	<dependencies>

		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-core</artifactId>
		    <version>${spring.version}</version>
		</dependency>

	</dependencies>

</project>

三、web项目(war),project2/pom.xml

同java同样进行瘦身。单元测试

注意packaging是war。wtpContextName这个只对eclipse wtp启做用,自动设置你的web上下文。对myeclipse无效须要手动修改。

<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>org.noahx</groupId>
		<artifactId>noahx-root</artifactId>
		<version>1.0</version>
	</parent>

	<groupId>org.noahx.project2</groupId>
	<artifactId>project2-web</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<servlet-api.version>2.4</servlet-api.version>
	</properties>

	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-eclipse-plugin</artifactId>
				<configuration>
					<wtpContextName>p2</wtpContextName>
				</configuration>
			</plugin>

		</plugins>
	</build>

	<dependencies>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet-api.version}</version>
			<scope>provided</scope>
		</dependency>

	</dependencies>
</project>

四、总结

你们固然还能够定义项目公共的pom继续提炼共性。

noahx-root<--project-root

project-root<--project-core

project-root<--project-ext

通常的开源东西的pom都是一级一级继承下来的。大大减少了pom文件大小。

相关文章
相关标签/搜索