我能找到的最佳解决方案包括以下步骤:
- 创建一个名为
mvn-repo 的分支来托管您的 Maven 工件。
- 使用 github site-maven-plugin 将您的工件推送到 github。
- 配置 maven 以使用您的远程
mvn-repo 作为 maven 存储库。
使用这种方法有几个好处:
- Maven 工件与您的源代码分开保存在一个名为
mvn-repo 的单独分支中,就像 github 页面保存在一个名为 gh-pages 的单独分支中一样(如果您使用 github 页面)
- 与其他一些提议的解决方案不同,如果您正在使用它们,它不会与您的
gh-pages 冲突。
- 与部署目标自然相关,因此无需学习新的 Maven 命令。像往常一样使用
mvn deploy
将工件部署到远程 maven 存储库的典型方式是使用 mvn deploy,因此让我们为该解决方案修补该机制。
首先,告诉 maven 将工件部署到目标目录内的临时暂存位置。将此添加到您的pom.xml:
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
</plugins>
现在尝试运行mvn clean deploy。您会看到它已将您的 maven 存储库部署到 target/mvn-repo。下一步是让它将该目录上传到 GitHub。
将你的认证信息添加到~/.m2/settings.xml,以便githubsite-maven-plugin可以推送到GitHub:
<!-- NOTE: MAKE SURE THAT settings.xml IS NOT WORLD READABLE! -->
<settings>
<servers>
<server>
<id>github</id>
<username>YOUR-USERNAME</username>
<password>YOUR-PASSWORD</password>
</server>
</servers>
</settings>
(如上所述,请确保chmod 700 settings.xml 以确保没有人可以读取文件中的密码。如果有人知道如何让 site-maven-plugin 提示输入密码而不是在配置文件中要求它,让我知道。)
然后通过将以下内容添加到您的 pom 中,告诉 GitHub site-maven-plugin 您刚刚配置的新服务器:
<properties>
<!-- github server corresponds to entry in ~/.m2/settings.xml -->
<github.global.server>github</github.global.server>
</properties>
最后,配置 site-maven-plugin 以从您的临时暂存存储库上传到您在 Github 上的 mvn-repo 分支:
<build>
<plugins>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.11</version>
<configuration>
<message>Maven artifacts for ${project.version}</message> <!-- git commit message -->
<noJekyll>true</noJekyll> <!-- disable webpage processing -->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
<branch>refs/heads/mvn-repo</branch> <!-- remote branch name -->
<includes><include>**/*</include></includes>
<repositoryName>YOUR-REPOSITORY-NAME</repositoryName> <!-- github repo name -->
<repositoryOwner>YOUR-GITHUB-USERNAME</repositoryOwner> <!-- github username -->
</configuration>
<executions>
<!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
mvn-repo 分支不需要存在,它会为你创建。
现在再次运行mvn clean deploy。您应该会看到 maven-deploy-plugin 将文件“上传”到目标目录中的本地暂存存储库,然后 site-maven-plugin 提交这些文件并将它们推送到服务器。
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building DaoCore 1.3-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] --- maven-deploy-plugin:2.5:deploy (default-deploy) @ greendao ---
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.jar (77 KB at 2936.9 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.pom (3 KB at 1402.3 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/maven-metadata.xml (768 B at 150.0 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/maven-metadata.xml (282 B at 91.8 KB/sec)
[INFO]
[INFO] --- site-maven-plugin:0.7:site (default) @ greendao ---
[INFO] Creating 24 blobs
[INFO] Creating tree with 25 blob entries
[INFO] Creating commit with SHA-1: 0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO] Updating reference refs/heads/mvn-repo from ab7afb9a228bf33d9e04db39d178f96a7a225593 to 0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.595s
[INFO] Finished at: Sun Dec 23 11:23:03 MST 2012
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------
在您的浏览器中访问 github.com,选择 mvn-repo 分支,并验证您的所有二进制文件现在都在那里。
恭喜!
您现在只需运行 mvn clean deploy 即可将您的 maven 工件部署到穷人的公共仓库。
您还需要采取一个步骤,即配置任何依赖于您的 pom 的 pom 以了解您的存储库在哪里。将以下 sn-p 添加到依赖于您的项目的任何项目的 pom 中:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/raw/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
现在任何需要您的 jar 文件的项目都会自动从您的 github maven 存储库下载它们。
编辑:为避免 cmets 中提到的问题(“创建提交时出错:请求无效。对于“属性/名称”,nil 不是字符串。”),请确保在 github 上的个人资料中声明名称。