在github上托管一个Maven仓库 [英] Hosting a Maven repository on github

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

问题描述

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



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

解决方案

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


  1. 创建一个名为 mvn-repo 的分支来承载您的maven工件。

  2. 使用github site-maven-plugin 推送您的工件

  3. 配置maven以使用您的远程 mvn-repo 作为maven仓库。

使用此方法有以下好处:


  • Maven工件保持独立从一个名为 mvn-repo 的独立分支的源代码中,很像github页面保存在一个名为 gh-pages (如果您使用github页面)

  • 与其他建议的解决方案不同,它不会与您的 gh-pages 冲突if您正在使用它们。

  • 与th自然结合e部署目标,因此没有新的maven命令可供学习。像通常那样使用 mvn deploy



部署工件的典型方式到一个远程maven仓库是使用 mvn deploy ,所以让我们来解释一下这个解决方案的机制。



第一个,告诉maven将工件部署到目标目录中的临时登台位置。把它添加到你的 pom.xml

 < distributionManagement> 
< repository>
< id> internal.repo< / id>
<名称>临时分段存储库< /名称>
< url> file:// $ {project.build.directory} / mvn-repo< / url>
< / repository>
< / distributionManagement>

< plugins>
< plugin>
< artifactId> maven-deploy-plugin< / artifactId>
< version> 2.8.1< / version>
<配置>
< 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

code>以便github site-maven-plugin 可以推送到GitHub:

 <! - 注意:请确认settings.xml不是世界可读的! - > 
< settings>
<服务器>
< server>
< id> github< / id>
< username> YOUR-USERNAME< / username>
<密码>您的密码< /密码>
< / server>
< /服务器>
< / settings>

(如上所述,请确保 chmod 700 settings.xml code>确保没有人能够读取你的密码,如果有人知道如何让site-maven-plugin提示输入密码而不是在配置文件中要求它,请告诉我。)



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

< properties>
<! - github服务器对应于〜/ .m2 / settings.xml中的条目 - >
< github.global.server> github< /github.global.server>
< / properties>

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


$ b

 <建立> 
< plugins>
< plugin>
< groupId> com.github.github< / groupId>
< artifactId> site-maven-plugin< / artifactId>
< version> 0.11< / version>
<配置>
< message> $ {project.version}的Maven工件< / message> <! - git commit message - >
< noJekyll> true< / noJekyll> <! - 禁用网页处理 - >
< outputDirectory> $ {project.build.directory} / mvn-repo< / outputDirectory> <! - 与上述配送管理储存库网址匹配 - >
< branch> refs / heads / mvn-repo< / branch> <! - 远程分支名称 - >
< includes>< include> ** / *< / include>< / includes>
< repositoryName> YOUR-REPOSITORY-NAME< / repositoryName> <! - github回购名称 - >
< repositoryOwner> YOUR-GITHUB-USERNAME< / repositoryOwner> <! - github用户名 - >
< / configuration>
<执行次数>
<! - 运行site-maven-plugin的'站点'目标作为构建的正常'部署'阶段的一部分 - >
<执行>
<目标>
< goal>网站< / goal>
< /目标>
<阶段>部署< /阶段>
< /执行>
< /执行次数>
< / plugin>
< / plugins>
< / build>

mvn-repo 分支不需要将会为您创建。



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

  [INFO]扫描项目... 
[信息]
[信息] -------------- -------------------------------------------------- --------
[INFO]构建DaoCore 1.3-SNAPSHOT
[INFO] ----------------------- -------------------------------------------------
...
[INFO] --- maven-deploy-plugin:2.5:deploy(default-deploy)@ greendao ---
已上传:file:/// Users / mike /项目/ 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)
已上传: 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 /秒)
已上传: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 )
已上传:file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/maven-metadata.xml(282 B at 91.8 KB /秒)
[信息]
[信息] ---网站-maven-plugin:0.7:网站(默认)@ greendao ---
[信息]创建24个斑点
[INFO]使用25个blob条目创建树
[INFO]使用SHA-1创建提交:0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO]将ab7afb9a228bf33d9e04db39d178f96a7a225593的引用refs / heads / mvn-repo更新为0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO ] ------------------------------------------------- -----------------------
[信息]建立成功
[信息] ----------- -------------------------------------------------- -----------
[信息]总时间:8.595s
[信息]完成时间:Sun Dec 23 11:23:03 MST 2012
[信息]最终记忆:9M / 8 1M
[INFO] ------------------------------------------ ------------------------------

在您的浏览器中访问github.com,选择 mvn-repo 分支,并确认您的所有二进制文件都已经存在。





恭喜! 您现在可以将您的maven工件部署到穷人的公共repo中,只需运行 mvn clean deploy



您还需要执行一个步骤,即配置任何取决于您的poms POM知道你的仓库在哪里。将以下代码片段添加到任何取决于您的项目的项目中:

 < repositories> 
< repository>
< id> YOUR-PROJECT-NAME-mvn-repo< / id>
< url> https://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/< / url>
<快照>
< enabled> true< / enabled>
< updatePolicy>始终< / updatePolicy>
< / snapshots>
< / repository>
< / repositories>

现在任何需要您的jar文件的项目都会自动从您的github maven仓库下载它们。



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


I have a fork of a small open sourced library that I'm working on 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.

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. 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 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

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.

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>

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.

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>

(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.)

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>

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>

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

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] ------------------------------------------------------------------------

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

Congratulations!

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

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://raw.github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/mvn-repo/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>

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

Edit: 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天全站免登陆