问题
I'm pretty new to the JVM World, and I find Maven and Gradle quite extraordinary pieces of tools to handle build and dependencies.
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?
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.
回答1:
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...*/])
}
Putting the URLs directly in the files method gives you a Cannot convert URL "http://url.to.jar" to a file exception
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.
Second solution
Don't leave Gradle. Instead download the files into a libs folder.
Copy task:
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:
dependencies {
compile([
fileTree(dir: 'libs', include: ['*.jar']),
'commons-validator:commons-validator:1.4.1'
/* and all the other Maven dependencies...*/])
}
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:
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'
}
Unfortunately this does not seem to work for my setup, but Gradle is happy and files are downloaded when needed (since they are cached)
来源:https://stackoverflow.com/questions/34678811/how-do-you-handle-web-hosted-jar-files-as-dependencies-in-gradle