Java 从 GitHub 加载 Maven 依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20161602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:35:43  来源:igfitidea点击:

Loading Maven dependencies from GitHub

javamavengithubdependencies

提问by Arielle

How do I add a Java library from its GitHub repo (the library uses Maven as a build system) as a dependency to my Maven project? Can I do that without downloading and compiling the library?

如何从其 GitHub 存储库(该库使用 Maven 作为构建系统)添加 Java 库作为我的 Maven 项目的依赖项?我可以在不下载和编译库的情况下做到这一点吗?

回答by Andrey Chaschev

At the moment there is no way you can do this unless the maintainer of the library provided a way to do this.

目前没有办法做到这一点,除非图书馆的维护者提供了一种方法来做到这一点。

So on the title page of the library the should be an instruction containing the repository address like:

所以在图书馆的标题页上应该是一个包含存储库地址的指令,如:

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

And a dependency name:

和一个依赖名称:

<dependency>
     <groupId>...</groupId>
     <artifactId>...</artifactId>
     <version>...</version>
</dependency>

This means that all artifact of your project including your dependency will be searched in this repo.

这意味着将在此 repo 中搜索您项目的所有工件,包括您的依赖项。

You could also have a glance at pom.xmlto check if there was an effort made to deploy artifacts to a remote repo. Typically the keywords are oss.sonatype.orgor raw.github.comlike in this case.

您还可以pom.xml查看一下是否已努力将工件部署到远程存储库。在这种情况下,通常关键字是oss.sonatype.orgraw.github.com

FYI, here is a way to provide a repo for your gihub artifact: Hosting a Maven repository on github.

仅供参考,这里有一种为您的 gihub 工件提供存储库的方法:在 github 上托管 Maven 存储库

回答by Andrejs

Now you can import a Java library from a GitHub repo using JitPack. In your pom.xml:

现在,您可以使用JitPack从 GitHub 存储库导入 Java 库。在你的 pom.xml 中:

  1. Add repository:
  1. 添加存储库:
<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>
  1. Add dependency
  1. 添加依赖
<dependency>
    <groupId>com.github.User</groupId>
    <artifactId>Repo name</artifactId>
    <version>Release tag</version>
</dependency>

It works because JitPack will check out the code and build it. So you'll end up downloading the jar.
If the project doesn't have a GitHub release then its possible to use a commit id as the version.

它有效是因为 JitPack 会检查代码并构建它。所以你最终会下载jar。
如果项目没有 GitHub 版本,则可以使用提交 ID 作为版本。

回答by Memin

Another very nice thing about Jitpackis, it has a lookup button on the main page. And if you type the URL of your GitHub repository, it displays different commits of the source code, and you can select which commit/tag you want. The Jitpack creates pom dependencies for you.

Jitpack另一个非常好的地方是,它在主页上有一个查找按钮。如果您输入 GitHub 存储库的 URL,它会显示源代码的不同提交,您可以选择所需的提交/标记。Jitpack 为您创建 pom 依赖项。

It became dead simple.

它变得非常简单。

回答by david

Github now supports packages https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages

Github 现在支持包https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages

You can follow the steps above to deploy Jar files to github properly.

您可以按照上述步骤将 Jar 文件正确部署到 github。