fqntxmqee

为名忙,为利忙,忙里偷闲,且喝两杯闲茶去!

勇往直前

Sonatype OSS Maven仓库使用手册2011-06-24

  1. 概述
  2. 注册用户
  3. 创建JIRA凭证
  4. 必要的准备工作
  5. pom 配置
  6. 发布snapshot和release

概述

Sonatype使用 Nexus 为开源项目提供 Maven仓库主机 服务,您可以通过她来发布项目的snapshot和release版本。

这篇文章将逐步引导您使用 Maven仓库主机 服务。

注册用户

首先您需要在 https://issues.sonatype.org/ 上,注册一个Sonatype JIRA帐号。

  1. 点击Sign Up链接注册
  2. 填写注册页面中的表单内容


记住您的用户名,创建JIRA凭证将会用到您的用户名。

创建JIRA凭证

  1. 在浏览器中打开 https://issues.sonatype.org/browse/OSSRH 页面。
  2. 通过下图的页面创建IRA凭证:


必要的准备工作

最好在linux系统下安装以下软件:

  • JDK 1.5+是否已安装;
  • 确保已安装版本控制(CVS、SVN、GIT…)系统客户端;
  • 使用Maven 2.2.1+;
  • 安装GPG(Ubuntu 已安装),生成 GPG签名 .

我在windows下生成GPG签名后,发布public key时失败,所以建议您中linux下使用;如果您找到在windows下的使用方法,请您给我留言,或发邮件,谢谢!

pom 配置

  • 以下pom元素在您项目的pom中必须要出现的:
	
    <modelVersion>
    <groupId>
    <artifactId>
    <version>
    <packaging>
    <name>
    <description>
    <url>
    <licenses>
    <scm><url>
    <scm><connection>
    <developers>
	

首先您需要配置您maven的settings.xml:

<settings>
  ...
  <servers>
    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>your-jira-id(注册帐号时的username)</username>
      <password>your-jira-pwd(注册帐号时的password)</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>
  ...
</settings>

如果您的项目packaging是jar,那么您必须要添加几个插件:

<build>
	...
	<plugins>
		...
		<plugin>
			<!-- 添加 artifactId-version-source.jar -->
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-source-plugin</artifactId>
			<version>2.1.2</version>
			<executions>
				<execution>
					<id>attach-sources</id>
					<goals>
						<goal>jar-no-fork</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<!-- 添加 artifactId-version-javadoc.jar -->
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-javadoc-plugin</artifactId>
			<version>2.7</version>
			<executions>
				<execution>
					<id>attach-javadocs</id>
					<goals>
						<goal>jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
		...
	</plugins>
	...
</build>

下面是我项目的一个完整pom配置:

<?xml version="1.0" encoding="UTF-8"?>
<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>net.mlw</groupId>
	<artifactId>vlh</artifactId>
	<version>0.1.14-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>valuelist</name>
	<description>Java Web Pages</description>
	<url>http://github.com/fqntxmqee/valuelist</url>

	<licenses>
		<license>
			<name>The Apache Software License, Version 2.0</name>
			<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
			<distribution>repo</distribution>
		</license>
	</licenses>

	<scm>
		<url>ssh://git@github.com/fqntxmqee/valuelist</url>
		<connection>scm:git:ssh://git@github.com/fqntxmqee/valuelist</connection>
		<developerConnection>scm:git:ssh://git@github.com/fqntxmqee/valuelist</developerConnection>
	</scm>

	<developers>
		<developer>
			<id>fqntxmqee</id>
			<name>Guoqing Huang</name>
			<email>guoqing.huang@foxmail.com</email>
		</developer>
	</developers>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

		<tomcat.version>6.0.16</tomcat.version>

		<plugin.findbugs.version>2.0.1</plugin.findbugs.version>
		<plugin.checkstyle.version>2.3</plugin.checkstyle.version>
		<plugin.javadoc.version>2.7</plugin.javadoc.version>
		<plugin.cobertura.version>2.4</plugin.cobertura.version>
	</properties>

	<dependencies>
		...
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate</artifactId>
			<version>3.2.6.ga</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
		</dependency>
		...
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<!-- findbugs插件 -->
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>findbugs-maven-plugin</artifactId>
					<version>${plugin.findbugs.version}</version>
				</plugin>

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

				<!-- 测试插件 -->
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.7.2</version>
					<executions>
						<execution>
							<id>run-integration-test</id>
							<phase>integration-test</phase>
							<goals>
								<goal>test</goal>
							</goals>
							<configuration>
								<includes>
									<include>**/*IT.java</include>
								</includes>
							</configuration>
						</execution>
					</executions>
					<configuration>
						<!-- 跳过测试 <skipTests>true</skipTests> -->
					</configuration>
				</plugin>

				<!-- 覆盖率测试插件 -->
				<plugin>
					<groupId>org.codehaus.mojo</groupId>
					<artifactId>cobertura-maven-plugin</artifactId>
					<version>${plugin.cobertura.version}</version>
					<configuration>
						<excludes>
							<exclude>**/*Test.class</exclude>
						</excludes>
					</configuration>
					<executions>
						<execution>
							<goals>
								<goal>clean</goal>
							</goals>
						</execution>
					</executions>
				</plugin>
			</plugins>
		</pluginManagement>

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>2.1.2</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-javadoc-plugin</artifactId>
				<version>2.7</version>
				<configuration>
					<show>private</show>
					<nohelp>true</nohelp>
				</configuration>
				<executions>
					<execution>
						<id>attach-javadocs</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			
			<!-- 资源插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.4.2</version>
			</plugin>

			<!-- 编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<version>2.3.1</version>
				<configuration>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
						</manifest>
					</archive>
				</configuration>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-scm-plugin</artifactId>
				<version>1.4</version>
				<configuration>
					<connectionType>developerConnection</connectionType>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-release-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<mavenExecutorId>forked-path</mavenExecutorId>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-gpg-plugin</artifactId>
				<version>1.3</version>
				<executions>
					<execution>
						<id>sign-artifacts</id>
						<phase>verify</phase>
						<goals>
							<goal>sign</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

	<!-- distribut -->
	<distributionManagement>
		<snapshotRepository>
			<id>sonatype-nexus-snapshots</id>
			<name>Sonatype Nexus Snapshots</name>
			<url>https://oss.sonatype.org/content/repositories/snapshots</url>
		</snapshotRepository>
		<repository>
			<id>sonatype-nexus-staging</id>
			<name>Nexus Release Repository</name>
			<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
		</repository>
	</distributionManagement>
</project>

如果您项目的版本控制不是Git,请更新pom中scm元素的配置。

SVN:

<project>
  ...
  <scm>
    <connection>scm:svn:http://foo.googlecode.com/svn/trunk/</connection>
    <developerConnection>scm:svn:https://foo.googlecode.com/svn/trunk/</developerConnection>
    <url>http://foo.googlecode.com/svn/trunk/</url>
  </scm>
  ...
</project>

发布snapshot和release

$ mvn clean deploy -Dgpg.passphrase=yourpassphrase 
/*yourpassphrase:gpg --gen-keys命令时设置的密码*/
$ mvn release:clean
$ mvn release:prepare -Dgpg.passphrase=yourpassphrase
$ mvn release:perform -Dgpg.passphrase=yourpassphrase

参考资源

Sonatype OSS Maven Repository Usage Guide
How To Generate PGP Signatures With Maven

blog comments powered by Disqus
Fork me on GitHub