在 Gradle 中,您如何处理网络托管的 .jar 文件作为依赖项?

How do you handle web hosted .jar-files as dependencies in Gradle?

我是 JVM 世界的新手,我发现 Maven 和 Gradle 是处理构建和依赖项的非常出色的工具。

我需要在我的解决方案中加入两个 jar 文件。它们不托管在任何 Maven 存储库中。我必须使用 libs 文件夹并在开发人员之间或在存储库中共享文件吗?

jar 文件不受我控制,我不想经历在 Maven Central 或类似的东西上发布东西的喧嚣。我相信 jar 文件的 url 非常持久。

第一个解决方案
不要离开Gradle。相反,请尝试使用文件集合。它应该工作!但不适合我,第二个解决方案

 dependencies {
        def webHostedJarFiles = ["http://url.to.jar", "http://url.to.second.jar"]
                .collect{fileName->new File(fileName)}
    
        compile([
                files{webHostedJarFiles}, 
                'commons-validator:commons-validator:1.4.1'
                 /* and all the other Maven dependencies...*/])
    }

将 URLs 直接放在文件方法中会给您一个 无法将 URL“http://url.to.jar”转换为文件异常

出于某种原因,这对我不起作用。下载了依赖,在IntelliJ的gradle插件中出现了,但是compilpiler编译的时候好像找不到。

第二种解法
不要离开Gradle。而是将文件下载到 libs 文件夹中。

复制任务:

task downloadJarsToLibs(){
    def f = new File('libs/myFile.jar')
    if (!f.exists()) {
        new URL('http://path.to/myFile.jar').withInputStream{ i -> f.withOutputStream{ it << i }}
    }    
}

依赖关系:

dependencies {
            compile([
                    fileTree(dir: 'libs', include: ['*.jar']), 
                    'commons-validator:commons-validator:1.4.1'
                     /* and all the other Maven dependencies...*/])
        }

第三个解决方案(@RaGe 的 Cortesey)
示例文件:

http://exampe.com/uda/virtuoso/7.2/rdfproviders/jena/210/virt_jena2.jar
http://exampe.com/uda/virtuoso/7.2/jdbc/virtjdbc4.jar

build.gradle:

repositories {
    ivy {
        url 'http://example.com/'
        layout 'pattern', {
            artifact '/uda/[organisation]/7.2/[module]/[revision].[ext]'
        }
        // This is required in Gradle 6.0+ as metadata file (ivy.xml) 
        // is mandatory. Docs linked below this code section
        metadataSources { artifact() } 
    }
    mavenCentral()
}

dependencies {
    compile 'virtuoso:rdfproviders/jena210:virt_jena2:jar', 'virtuoso:jdbc:virtjdbc4:jar'

}

所需元数据参考 here

不幸的是,这似乎不适用于我的设置,但 Gradle 很高兴并且在需要时下载文件(因为它们被缓存)