gradle - 如何在 Gradle 中将 Web 托管的 .jar 文件作为依赖项处理?

标签 gradle dependency-management

我对 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...*/])
    }

将 URL 直接放入 files 方法中会出现无法将 URL“http://url.to.jar”转换为文件异常

由于某种原因,这对我不起作用。依赖项已下载并显示在 IntelliJ 的 gradle 插件中,但编译时编译器似乎找不到该依赖项。

第二种解决方案
不要离开 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

构建.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 很高兴,并且在需要时下载文件(因为它们被缓存)

关于gradle - 如何在 Gradle 中将 Web 托管的 .jar 文件作为依赖项处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34678811/

相关文章:

java - 由于容器未启动,无法运行 Spring 集成测试

gradle - 如何根据现有插件向子项目添加插件?

gradle - 如何为 kotlin-multiplatform (kotlin 1.3.50) 添加对 build.gradle.kts 的依赖项?

gradle - 为什么 LATEST 是依赖管理反模式?

homebrew - 与电子打包器一起分发 Homebrew 依赖项

android - 通过读取.properties文件收集的buildConfigField

java - 运行使用 Gradle 构建的 JAR 文件时“没有主要 list 属性”

java - Ivy 依赖版本的调用者优先级

dependency-management - Cargo 的项目特定覆盖

java - 我的 Android 应用程序遇到问题