如何在Gradle中将Web托管的.jar文件作为依赖项处理? [英] How do you handle web hosted .jar-files as dependencies in Gradle?

查看:81
本文介绍了如何在Gradle中将Web托管的.jar文件作为依赖项处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JVM World还是很陌生,我发现Maven和Gradle可以处理构建和依赖项的工具非常出色。

I'm pretty new to the JVM World, and I find Maven and Gradle quite extraordinary pieces of tools to handle build and dependencies.

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

I need to incorporate two jar-files in my solution. They are not hosted in any Maven repository. Must i use a libs-folder and share the files among the developers or in the repo?

jar文件不受我控制,我也不想去忙于在Maven Central上放置类似内容。我相信jar文件的网址是相当持久的。

The jar files are not controlled by me, and i do not want to go through the hustle of putting up something on Maven Central or something similar. I trust the url of the jar files to be quite persistent.

推荐答案

第一个解决方案

不要离开Gradle。而是尝试使用文件集合。它应该工作!但对我而言,不是第二种解决方案

First solution
Don't leave Gradle. Instead, try use a file collection. It should work! But not for me, se second solution

 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将为您提供无法转换URL http://url.to.jar 到文件异常

Putting the URLs directly in the files method gives you a Cannot convert URL "http://url.to.jar" to a file exception

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

By some reason this did not work for me. The dependencies were downloaded and showed up in the gradle plugin of IntelliJ, but when compiling the comilpiler seemed not to be able to find the.

第二个解决方案

不要离开Gradle。而是将文件下载到libs文件夹中。

Second solution
Don't leave Gradle. Instead download the files into a libs folder.

复制任务:

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)

示例文件:

Third Solution (Cortesey of @RaGe)
Example files:


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:

build.gradle:

repositories {
    ivy {
        url 'http://example.com/'
        layout 'pattern', {
            artifact '/uda/[organisation]/7.2/[module]/[revision].[ext]'
        }
    }
    mavenCentral()
}

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

}

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

Unfortunately this does not seem to work for my setup, but Gradle is happy and files are downloaded when needed (since they are cached)

这篇关于如何在Gradle中将Web托管的.jar文件作为依赖项处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆