在 github 上托管 Maven 存储库 [英] Hosting a Maven repository on github

查看:30
本文介绍了在 github 上托管 Maven 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a fork of a small open sourced library that I'm working on github. I'd like to make it available to other developers via maven, but I don't want to run my own Nexus server, and because it's a fork I can't easily deploy it to oss.sonatype.org.

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

What I'd like to do is to deploy it to github so that others can access it using maven. What's the best way to do this?

推荐答案

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

The best solution I've been able to find consists of these steps:

  1. 创建一个名为 mvn-repo 的分支来托管您的 Maven 工件.
  2. 使用 github site-maven-plugin 将您的工件推送到 github.
  3. 配置 maven 以使用远程 mvn-repo 作为 maven 存储库.
  1. Create a branch called mvn-repo to host your maven artifacts.
  2. Use the github site-maven-plugin to push your artifacts to github.
  3. Configure maven to use your remote mvn-repo as a maven repository.

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

There are several benefits to using this approach:

  • Maven 工件与您的源代码分开保存在名为 mvn-repo 的单独分支中,就像 github 页面保存在名为 gh-pages 的单独分支中(如果你使用github页面)
  • 与其他一些提议的解决方案不同,如果您正在使用它们,它不会与您的 gh-pages 冲突.
  • 自然而然地与部署目标相关联,因此无需学习新的 Maven 命令.只需像往常一样使用 mvn deploy
  • Maven artifacts are kept separate from your source in a separate branch called mvn-repo, much like github pages are kept in a separate branch called gh-pages (if you use github pages)
  • Unlike some other proposed solutions, it doesn't conflict with your gh-pages if you're using them.
  • Ties in naturally with the deploy target so there are no new maven commands to learn. Just use mvn deploy as you normally would

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

The typical way you deploy artifacts to a remote maven repo is to use mvn deploy, so let's patch into that mechanism for this solution.

首先,告诉 maven 将工件部署到目标目录中的临时暂存位置.将此添加到您的 pom.xml:

First, tell maven to deploy artifacts to a temporary staging location inside your target directory. Add this to your 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.

Now try running mvn clean deploy. You'll see that it deployed your maven repository to target/mvn-repo. The next step is to get it to upload that directory to GitHub.

~/.m2/settings.xml中添加你的认证信息,以便github site-maven-plugin 可以推送到GitHub:

Add your authentication information to ~/.m2/settings.xml so that the github site-maven-plugin can push to 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 提示输入密码不要在配置文件中要求它,让我知道.)

(As noted, please make sure to chmod 700 settings.xml to ensure no one can read your password in the file. If someone knows how to make site-maven-plugin prompt for a password instead of requiring it in a config file, let me know.)

然后告诉 GitHub site-maven-plugin 关于你刚刚配置的新服务器,将以下内容添加到你的 pom 中:

Then tell the GitHub site-maven-plugin about the new server you just configured by adding the following to your pom:

<properties>
    <!-- github server corresponds to entry in ~/.m2/settings.xml -->
    <github.global.server>github</github.global.server>
</properties>

最后,配置 site-maven-plugin 以从您的临时临时存储库上传到您在 Github 上的 mvn-repo 分支:

Finally, configure the site-maven-plugin to upload from your temporary staging repo to your mvn-repo branch on Github:

<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 分支不需要存在,它会为你创建.

The mvn-repo branch does not need to exist, it will be created for you.

现在再次运行 mvn clean deploy.您应该看到 maven-deploy-plugin 将文件上传"到目标目录中的本地临时存储库,然后 site-maven-plugin 提交这些文件并将它们推送到服务器.

Now run mvn clean deploy again. You should see maven-deploy-plugin "upload" the files to your local staging repository in the target directory, then site-maven-plugin committing those files and pushing them to the server.

[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 分支,并验证您的所有二进制文件现在都在那里.

Visit github.com in your browser, select the mvn-repo branch, and verify that all your binaries are now there.

恭喜!

您现在只需运行 mvn clean deploy 即可将您的 Maven 工件部署到穷人的公共存储库.

You can now deploy your maven artifacts to a poor man's public repo simply by running mvn clean deploy.

您还需要执行一个步骤,即配置任何依赖于您的 pom 的 poms 以了解您的存储库在哪里.将以下代码段添加到依赖于您的项目的任何项目的 pom:

There's one more step you'll want to take, which is to configure any poms that depend on your pom to know where your repository is. Add the following snippet to any project's pom that depends on your project:

<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 存储库下载它们.

Now any project that requires your jar files will automatically download them from your github maven repository.

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

to avoid the problem mentioned in the comments ('Error creating commit: Invalid request. For 'properties/name', nil is not a string.'), make sure you state a name in your profile on github.

这篇关于在 github 上托管 Maven 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆