简体   繁体   中英

Gradle include jar produced by another project in war

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM