利用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文件大小。

相關文章
相關標籤/搜索