2

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.

Fontanka16
  • 1,161
  • 1
  • 9
  • 37

1 Answers1

3

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]'
        }
        // 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'

}

reference for required metadata here

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

Qaz
  • 45
  • 1
  • 4
Fontanka16
  • 1,161
  • 1
  • 9
  • 37
  • 1
    An alternate way to do this is to pretend the online jar file is a custom layout ivy repository. The advantage of this is you can use gradle's caching feature so you do not have to download the dependency for every build. See [my answer here](http://stackoverflow.com/questions/23023069/gradle-download-and-unzip-file-from-url/34327202#34327202) – RaGe Jan 08 '16 at 18:52
  • I gave it a try, byt i could not get it to work. Could you provide an example? – Fontanka16 Jan 11 '16 at 09:22
  • @RaGe: It seemes to work well getting the file down. However it does not compile, and intelliJ does not recognize the aritifacts just as my first solution above – Fontanka16 Feb 03 '16 at 10:52
  • There is a `/` in your module name? I don't know if that is the problem, but feels like it could be. – RaGe Feb 03 '16 at 11:49