maven - 在 github 上托管 Maven 存储库

标签 maven github github-pages mvn-repo

我有一个小型开源库的分支,我正在 github 上工作。我想通过 Maven 将其提供给其他开发人员,但我不想运行自己的 Nexus 服务器,而且因为它是一个分支,所以我无法轻松地将其部署到 oss.sonatype.org。

我想做的是将它部署到github上,以便其他人可以使用maven访问它。最好的方法是什么?

最佳答案

我找到的最佳解决方案包括以下步骤:

  1. 创建一个名为 mvn-repo 的分支来托管您的 Maven Artifact 。
  2. 使用 github site-maven-plugin将您的 Artifact 推送到 github。
  3. 配置 Maven 以使用远程 mvn-repo 作为 Maven 存储库。

使用这种方法有几个好处:

  • Maven Artifact 与源代码分开保存在名为 mvn-repo 的单独分支中,就像 github 页面保存在名为 gh-pages 的单独分支中一样(如果您使用 github 页面)
  • 与其他建议的解决方案不同,如果您使用它们,它不会与您的 gh-pages 冲突。
  • 与部署目标自然结合,因此无需学习新的 Maven 命令。只需像平常一样使用 mvn deploy

将 Artifact 部署到远程 Maven 存储库的典型方法是使用 mvn deploy,因此让我们为该解决方案修补该机制。

首先,告诉 maven 将 Artifact 部署到目标目录内的临时暂存位置。将其添加到您的 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 中,以便 github site-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 分支,并验证所有二进制文件现在都在那里。

enter image description here

恭喜!

现在,您只需运行 mvn clean deploy 即可将 Maven Artifact 部署到穷人的公共(public)存储库。

您还需要执行一个步骤,即配置依赖于您的 pom 的所有 pom,以了解您的存储库的位置。将以下代码片段添加到取决于您的项目的任何项目的 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 存储库下载它们。

编辑:为了避免评论中提到的问题(“创建提交时出错:无效请求。对于“属性/名称”,nil 不是字符串。”),请确保在 github 上的个人资料中注明名称。

关于maven - 在 github 上托管 Maven 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14013644/

相关文章:

powershell - 批处理脚本期间重启环境和脚本

ruby - github-pages gem 失败,即使我有 Ruby 版本 2.1.2

maven - Jmeter 插件,不采用属性中提供的 keystore

javascript - 如何将 Lighthouse CI 设置为仅测试可访问性?

git - 如何将新分支添加到现有 GitHub 存储库作为主要分支?

git - 将构建工件发布到 github 页面

travis-ci - GH-token 无效,GitHub Pages Deployment on Travis

eclipse - 防止 Eclipse 从 Maven 目标文件夹定位/链接文件

maven - 如何使用 Maven 解压缩文件?

java - Maven 项目在 prod 中寻找不存在的路径