3

Currently I have two projects with gradle build.gradle. The first is going to create a fat jar file, which I would like to include in a war file. I thought compiling it would be enough, but it doesn't seem to be ending up in the /lib directory of my war file. Anyone have thoughts I am quite new to gradle.

dependencies {
    compile project(':JarProject')
    providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
    providedCompile 'org.apache.tomcat:tomcat-jsp-api:7.0.55'

}


war {
    archiveName 'WarProject.war'
    from 'JarProject/build/libs'
    webXml = file('src/web.xml')
}

Does the second project war need to be in providedRuntime? Or should I publish the jar from the other project in the local maven repo and include it that way?

user2524908
  • 861
  • 4
  • 18
  • 46

2 Answers2

2

The War task essentially behaves like a CopyTask with regards to stuff it packs in the war, so the documentation on working with files is useful. In essence, I think you need something like (untested):

from fileTree('JarProject/build/libs') {
    into("lib")
}

That being said, using mavenLocal() and publishing there also works, but it can lead to unexpected results when the war includes some old version from local, picking up the jar explicitly from the file system like above is better.

I think the elegant solution would be to use multi project builds and project level dependencies. You would have the two builds as separate projects of the same Gradle build and add the "jar project" as a regular compile dependency.

Alpar
  • 2,807
  • 23
  • 16
  • Thanks for the comment, I am currently using multi project builds. Adding ' from configurations.runtime' to the war file creation did drop the jar from the other project. However it also placed all the dependencies in the root war directory along with web-inf/lib – user2524908 Oct 04 '16 at 14:33
  • 1
    You still need `into "web-inf/lib"` to place them elsewhere, the root of the war is the default. I just realized that your initial example has the compile time dependency of `JarProject` so it should include it in the war without further effort on your side. You might need to provide more from you project and gradle scripts to be able to tell what's missing. The only common reason I could find is [this answer](https://stackoverflow.com/questions/30238040/missing-dependencies-in-war-gradle-build-project) – Alpar Oct 04 '16 at 14:44
0

How have you declared the dependency? I assume you have a multi-project build with subprojects A and B, both using the War plugin. I made an experiment using Gradle 2.4 and if I declare B/build.gradle like this:

apply plugin: 'war'

dependencies {
    compile project(':A')
}

then B.war contains WEB-INF/lib/A.jar. If you correctly follow conventions of Gradle War plugin (place web resources in A/src/main/webapp/ and code-related resources in A/src/main/resources/), then A.jar should contain what you want.

see this

Community
  • 1
  • 1
chaitanya
  • 693
  • 2
  • 7
  • 16