上传利用GitHub管理的软件到Maven Central Repository之一

笔者最近自己开发了一个开源项目PortletTester,代码托管在GitHub,使用Maven作为构建工具。为了使大家使用起来更加方便,决定尝试将项目发布到Maven的Central Repository上面,这样其他同样使用Maven的人要用的话只需要将PortletTester加入到pom.xml的dependency里就可以了,不用再GitHub网站上下载jar包,还要下载项目依赖的包。

这个过程可以说相当曲折,经过几个晚上的研究,终于把成功地发布了项目的第一个版本。现在把步骤和遇到的问题记录下来,方便有需要的人或者自己以后查阅。本文使用Sonatype Nexus作为代理仓库。也就是说先要把软件发布到这里,然后他们会帮同步到Maven的Central Repository上,这貌似是目前最简单有效的办法。

首先,修改pom.xml文件,使其满足下面这些要求:
  • 下列标签填写正确
  •     <modelVersion>
        <groupId>
        <artifactId>
        <version>
        <packaging>
        <name>
        <description>
        <url>
        <licenses>
        <scm><url>
        <scm><connection>
        <developers>
  • 文件中不能包含<repositories>和<pluginRepositories>标签
  • 假如项目代码托管在GitHub等开源项目网站,<groupId>必须是com.github.<用户名>或者自己拥有的域名之一
  • <verion>的标签必须是带SNAPSHOT的,例如1.0-SNAPSHOT,否则在release:prepare的时候会报错说没有可用的SNAPSHOT版本


确保信息正确以后,在pom.xml中加入
<profiles>
  	<profile>
		<id>release-sign-artifacts</id>
		<activation>
		  <property>
			<name>performRelease</name>
			<value>true</value>
		  </property>
		</activation>
		<build>
		  <plugins>
			<plugin>
			  <groupId>org.apache.maven.plugins</groupId>
			  <artifactId>maven-gpg-plugin</artifactId>
			  <version>1.1</version>
			  <executions>
				<execution>
				  <id>sign-artifacts</id>
				  <phase>verify</phase>
				  <goals>
					<goal>sign</goal>
				  </goals>
				</execution>
			  </executions>
			</plugin>
		  </plugins>
		</build>
	</profile>
  </profiles>


这个profile在运行mvn release:perform的时候生效,主要作用是为发布的软件做数字签名以防别人下载到被修改过的软件。

在pom.xml文件中继续添加:
<build>
  	<plugins>
  		<plugin>
		  <groupId>org.apache.maven.plugins</groupId>
		  <artifactId>maven-source-plugin</artifactId>
		  <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>
		  <executions>
			<execution>
			  <id>attach-javadocs</id>
			  <goals>
				<goal>jar</goal>
			  </goals>
			</execution>
		  </executions>
		</plugin>
  	</plugins>
  </build>


由于提交到Maven Central Repository的开源软件需要有对应的sources.jar和javadoc.jar文件,因此添加这两个插件方便自动生成这两个必须的文件。

继续修改pom.xml文件,添加:

<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Repository</name>
            <url>http://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>


这个是发布软件时的目标仓库,如果你发布的不是Sonatype Nexus,那么URL要作相应修改。

到这里pom.xml文件就修改完了。接下来进行其他步骤。请点击 http://drug.iteye.com/blog/1979777查看第二篇。

猜你喜欢

转载自drug.iteye.com/blog/1979756