Adding a local jar to a web app


(Blake McBride) #1

Greetings,

New to Gradle here. Sorry about the really basic question.

I am building a web app. I tried the tutorial to build, run locally, and deploy the war. All works fine.

The problem I am having is adding some local jar files. I tried adding my jar files to the src/main/resources directory. They get added to the app root of the war file but the app won’t compile because they’re not in the build classpath. Rather than kludge a result, I thought I’d ask what the correct solution is.

So, in brief, I want to add some local jar files. These jar files must:

  1. work at compile time
  2. be included in the war
  3. be accessible when the war is deployed

Thank you.

Blake McBride


(Blake McBride) #2

Also, in case it helps, here is my build.gradle:

plugins {
    id 'war'  
    id 'org.akhikhl.gretty' version '1.4.2'
}

repositories {
    jcenter()
}

dependencies {
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0' 
    testCompile 'junit:junit:4.12'
}

(uklance) #3

Firstly, I suggest that you deploy the jars to a proper repository rather than having local jars in a folder.

Failing that I suggest that you store your files in a maven style directory layout.

Eg

myLocalRepo/com/foo/bar/1.0/bar-1.0.jar
myLocalRepo/com/foo/baz/2.0/baz-2.0.jar

build.gradle

repositories {
   maven {
      uri file('myLocalRepo')
   }
} 
dependencies {
   compile 'com.foo:bar:1.0'
   compile 'com.foo:baz:2.0'
}

(Blake McBride) #4

Thanks for the help, Lance. I have two projects that are both under very active development. A repo to hold a jar doesn’t make sense for me just yet. I ended up with the following that does what I need:

plugins {
    id 'war'  
    id 'org.akhikhl.gretty' version '1.4.2'
}

repositories {
    jcenter()
}

dependencies {
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0' 
    testCompile 'junit:junit:4.12'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

(uklance) #5

Using files like this the artifacts can’t participate in Gradle’s dependency resolution. You can’t have transitive dependencies in poms and you also can’t associate javadocs and sources with the jars (to be linked in your IDE).

If you’re happy with these shortcomings then fileTree(...) is fine. Personally I’d use a maven directory layout instead, when you do move to a proper repository it will be simple