Gradle:使第 3 方 jar 可用于本地 gradle 存储库

发布于 2024-08-27 18:55:41 字数 260 浏览 12 评论 0 原文

目前,我正在测试 Gradle 作为 Maven 的替代品。在我的项目中,有一些 3rd 方 jar,它们在任何 (Maven) 存储库中都不可用。我现在的问题是,我如何管理它以将这些 jar 安装到我的本地 .gradle 存储库中。 (如果可以的话,我不想使用本地Maven存储库,因为Gradle应该独立运行。)目前,我因为缺少jar而遇到很多异常。在 Maven 中,通过运行 install 命令非常简单。但是,我在 Google 上搜索类似于 Maven 安装命令的内容没有成功。有人有想法吗?

currently, I'm testing Gradle as an alternative to Maven. In my projects, there are some 3rd party jars, which aren't available in any (Maven) repositories. My problem is now, how could I manage it to install these jars into my local .gradle repository. (If it's possible, I don't want to use the local Maven repository, because Gradle should run independently.) At the moment, I get a lot of exceptions because of missing jars. In Maven, it's quite simple by running the install command. However, my Google search for something similar to the Maven install command wasn't successful. Has anybody an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倒带 2024-09-03 18:55:41

您可以包含文件系统 JAR 依赖项,如下所示:

dependencies {
    runtime files('libs/a.jar', 'libs/b.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
}

您可以更改编译/测试编译/等的运行时。

you can include your file system JAR dependencies as:

dependencies {
    runtime files('libs/a.jar', 'libs/b.jar')
    runtime fileTree(dir: 'libs', include: '*.jar')
}

you may change runtime for compile/testCompile/etc..

世态炎凉 2024-09-03 18:55:41

Adam Murdoch 在邮件列表中给出了更全面的答案 http://gradle.1045684.n5.nabble.com/Gradle-Make-a-3rd-party-jar-available-to-local-gradle- repository-td1431953.html

截至 2010 年 4 月,没有简单的方法可以将新的 jarfile 添加到 ~/.gradle 存储库。目前正在研究这种情况是否有所改变。

截至 2014 年 10 月,情况仍然如此——因为 gradle 对你的 jarfile 进行了 md5 校验,所以你不能简单地下载它并将其放入 .gradle/caches 下的目录中,而 gradle 不会,就目前而言我可以告诉你,有任何任务可以让你获取本地文件并将该文件推送到其缓存。

A more comprehensive answer was given on a mailing list by Adam Murdoch at http://gradle.1045684.n5.nabble.com/Gradle-Make-a-3rd-party-jar-available-to-local-gradle-repository-td1431953.html

As of April 2010 there was no simple way to add a new jarfile to your ~/.gradle repository. Currently researching whether this has changed.

As of October 2014, this is still the case--because gradle does an md5 checksum of your jarfile, you can't simply download it and put it into a directory under .gradle/caches, and gradle doesn't, as far as I can tell, have any tasks which let you take a local file and push that file to its cache.

初见终念 2024-09-03 18:55:41

使用 Adam Murdoch 帖子中的选项 (1)(上面已链接:http://gradle.1045684.n5.nabble.com/Gradle-Make-a-3rd-party-jar-available-to-local-gradle- repository-td1431953.html)与 gradle-1.3 一起使用,效果很好!

这是他的评论:

  1. 将 jar 复制到本地目录并使用 flatDir() 存储库来使用它们。例如,您可以将它们复制到
    $projectDir/lib 并在您的构建文件中执行以下操作:

存储库{
flatDir(dirs: 'lib') }

lib 目录中的文件必须遵循以下命名方案:
name-version-classifier.extension,其中版本和分类器是
选修的。因此,例如您可以将它们称为 groovy-1.7.0.jar 甚至
groovy.jar

然后,您只需像平常一样声明依赖项即可:

依赖项{
编译 'groovy:groovy:1.7.0' }

有一个 flatDir() 存储库的更多详细信息:
http://gradle.org/0.9-preview-1 /docs/userguide/dependency_management.html#sec:flat_dir_resolver

  • 与上面类似,但使用 ivy 解析器而不是 flatDir()。这与上面几乎相同,但允许
    就命名和位置而言,还有更多选择。
  • 有一些详细信息:
    http://gradle.org/0.9-preview-1 /docs/userguide/dependency_management.html#sub:more_about_ivy_resolvers

  • 不必费心声明依赖项。只需将 jar 复制到本地目录中的某个位置并添加文件依赖项即可。例如,
    如果 jar 位于 $projectDir/lib 中:
  • 依赖项{
    compile fileTree('lib') // 这包括编译类路径中'lib'下的所有文件 }

    更多详情请访问:
    http://gradle.org/0.9-preview-1/docs /userguide/dependency_management.html#N12EAD

  • 使用maven install将依赖项安装到本地maven缓存中,并将maven缓存用作存储库:
  • 存储库{
    mavenRepo(urls: new File(System.properties['user.home'], '.m2/repository').toURI().toURL()) }

    Used option (1) out of Adam Murdoch post (already linked above: http://gradle.1045684.n5.nabble.com/Gradle-Make-a-3rd-party-jar-available-to-local-gradle-repository-td1431953.html) with gradle-1.3 and it works just nicely!

    Here his comment:

    1. Copy the jars to a local directory and use a flatDir() repository to use them out of there. For example, you might copy them to
      $projectDir/lib and in your build file do:

    repositories {
    flatDir(dirs: 'lib') }

    The files in the lib directory must follow the naming scheme:
    name-version-classifier.extension, where version and classifier are
    optional. So, for example you might call them groovy-1.7.0.jar or even
    groovy.jar

    Then, you just declare the dependencies as normal:

    dependencies {
    compile 'groovy:groovy:1.7.0' }

    There's a little more detail one flatDir() repository at:
    http://gradle.org/0.9-preview-1/docs/userguide/dependency_management.html#sec:flat_dir_resolver

    1. Similar to the above, but using an ivy resolver instead of flatDir(). This is pretty much the same as the above, but allows a
      lot more options as far as naming and locations go.

    There's some detail at:
    http://gradle.org/0.9-preview-1/docs/userguide/dependency_management.html#sub:more_about_ivy_resolvers

    1. Don't bother with declaring the dependencies. Just copy the jars to a local directory somewhere and add a file dependency. For example,
      if the jars are in $projectDir/lib:

    dependencies {
    compile fileTree('lib') // this includes all the files under 'lib' in the compile classpath }

    More details at:
    http://gradle.org/0.9-preview-1/docs/userguide/dependency_management.html#N12EAD

    1. Use maven install to install the dependencies into your local maven cache, and the use the maven cache as a repository:

    repositories {
    mavenRepo(urls: new File(System.properties['user.home'], '.m2/repository').toURI().toURL()) }

    书间行客 2024-09-03 18:55:41

    也许我在阅读你的问题时遗漏了一些东西,假设你的 gradle 存储库是 flatDir 类型,你应该能够以 myjar-1.0.jar 的形式复制文件并将它们解析为版本 1.0 的 myjar 。

    不知道为什么 Gradle 需要运行 Maven 才能访问本地 Maven 存储库。您只需定义 Maven 存储库即可,它应该可以解决依赖关系。如果需要,您可以使用 gradle upload 将 jar 推送到本地或远程 Maven 存储库。在这种情况下,它将执行maven。

    Maybe I'm missing something from my reading of your question, assuming your gradle repo is of the flatDir type, you should be able to copy the files there in the form myjar-1.0.jar and resolve them as myjar of version 1.0.

    Not sure why should it be necessary for Gradle to run maven in order to access a local maven repository. You can just define the maven repos and it should resolve dependencies. You can use gradle upload to push the jars local or remote maven repos if you need to. In that case, it will execute maven.

    醉生梦死 2024-09-03 18:55:41

    简而言之:部署到存储库管理器。它可以是本地的,位于公司 LAN 上。

    思考此类问题的一种完全不同的方式,特别是如果它经常发生的话,就是使用存储库管理器。有一些很棒的开源选项,例如 Artifactory、Nexus 或 Archiva。

    假设您有一个来自某个可疑来源的 jar 文件,需要将其包含在您的构建中,直到您有机会重构它为止。存储库管理器允许您将文件上传到您自己的存储库,在本例中为 dubious-origin-UNKNOWN.jar

    然后您的 build.gradle 看起来像这样:

    repositories {
        mavenRepo urls: "http://your.own.repository/url";
    }
    
    dependencies {
        compile "dubious:origin:UNKNOWN";
    }
    

    使用它还有很多其他优点存储库管理器,例如缓存远程工件、从 scm 中删除工件、暂存版本、更细粒度的用户权限等等。

    不利的一面是,您将添加一台服务器,该服务器会带来一些维护开销以保持构建运行。

    我想这取决于你的项目的大小。

    In short: deploy to repository manager. It can local, on company LAN.

    An altogether different way of thinking about this type of problem, specially if it happens often, is to use a repository manager. There are some great open source options out there such as Artifactory, Nexus or Archiva.

    Lets assume you have a jar file from some dubious origin that needs to be included in your build until you have the opportunity of refactoring it out. A repository manager would allow you to upload the file to your own repository as, for the sake of this example, dubious-origin-UNKNOWN.jar

    Then your build.gradle would look something like this:

    repositories {
        mavenRepo urls: "http://your.own.repository/url";
    }
    
    dependencies {
        compile "dubious:origin:UNKNOWN";
    }
    

    There are a lot of other advantages to using a repository manager such as caching of remote artifacts, remove artifacts from scm, staging releases, more granular user permissions, and so forth.

    On the down side, you would be adding a server which carries some maintenance overhead to keep your builds running.

    Depends on the size if your project, I suppose.

    掌心的温暖 2024-09-03 18:55:41

    我认为这样的事情应该有效:

    dependencies {
      files('yourfile.jar')
    }
    

    它对你有用吗?

    I think something like this should work:

    dependencies {
      files('yourfile.jar')
    }
    

    Does it work for you?

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文