1

I have two web apps A and B. B uses parts of code from A.

If I declare A as a dependency for B the A.war is included WEB-INF/lib of B.war

This kind of include is meaningless to the container.

The only solution I see is to change A to a Java project (JAR) and create a dummy web project (WAR) called A_WEB. Then declare A as dependency for A_WEB and B

However this will require me to meddle with the project structure. A big reason for choosing gradle was that it promised not to make the project adapat to the tool like maven does. So I hope there is a non messy way out of this.

Dojo
  • 5,374
  • 4
  • 49
  • 79

1 Answers1

3

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.

Alternatively, if you really need to hack something around, you can selectively copy things from A:

apply plugin: 'war'

war.dependsOn(':A:war')

war {
    from project(':A').sourceSets.main.output.classesDir
}

classesDir is an instance of java.io.File, so you may filter files as described in Gradle's tutorial on working with files.

However, if you write that "B uses parts of code from A", this means your A actually contains a sub-project C which is the code shared both by A and B. You just don't accept the fact. What you are trying to do is to hack around and hide the dependency on C from anyone - instead of making it clear by extracting C as a separate project, setting up proper inter-project dependencies and letting Gradle default behaviour take care of building all properly. I'd say that what's messy is trying to do anything to prevent re-organizing files on disk even if inter-module dependencies suggest you should extract a common part.

Piotr Kubowicz
  • 121
  • 1
  • 8
  • apply plugin: 'war' dependencies { compile project(':A') }. I was already doing this. It inserts a fully built A.war into lib directory of B.war. The second alternative did work well. Thanks – Dojo May 21 '15 at 17:58
  • Hi, while the war is being built properly. When I deploy the project (not the war built by gradle) on tomcat via eclipse, it still includes `A.war`. Like you defined the sourceSet for war task, I guess, the same needs to be done for the process(s) involved in deploying the web app in eclipse. Any pointers? – Dojo Jun 11 '15 at 11:09
  • You probably need to configure WTP as described in [Eclipse Plugin documentation](https://docs.gradle.org/current/userguide/eclipse_plugin.html#N14181). Besides, I am surprised that the first solution did not work - I saw correct results on my machine on a simple project, there was no `A.war` copied. Were you using command-line Gradle 2.4 to check it? – Piotr Kubowicz Jun 14 '15 at 09:30
  • Maybe I was not testing it properly, yes the first solution works too but only during build. just like the second solution. i.e. if you run the build tasks, it will generate JAR in WAR as expected. But if you deploy project B on tomcat via eclipse then it will deploy WAR in WAR and the project fails to run. It can be verified by browsing to the deployment location of Eclipse's Tomcat instance. See the test project https://www.dropbox.com/s/000yn6mm23keqio/gradletest.zip?dl=0 – Dojo Jun 15 '15 at 05:37
  • Import the two projects in eclipse and deploy B on Tomcat via Eclipse. Then rightclick on the server and choose Browse Deployment Location. When there, go to B/WEB-INF/lib/ . You will find A.war there. – Dojo Jun 15 '15 at 05:40
  • Because the current problem is different, I have created a new question here: http://stackoverflow.com/questions/30839093/gradle-eclipse-wtp-war-file-gets-included-in-another-war-file – Dojo Jun 15 '15 at 07:04