This might be a noob question, please excuse if it is…
I have a webapp which has 2 “flavors” (cloud, onprem).
Both use some common java source and some common webapp files.
So I have a directory tree looking like:
theWebapp
---- common
-------- src
------------ main
---------------- java
---------------- resources
---------------- webapp
------------ test
---- cloud
-------- src
------------ main
---------------- java
---------------- resources
---------------- webapp
------------ test
-------- build.gradle
---- onprem
-------- src
------------ main
---------------- java
---------------- resources
---------------- webapp
------------ test
-------- build.gradle
---- build.gradle
---- settings.gradle
common is never supposed to be compiled or deployed alone.
common source can’t be compiled without flavor source.
common is included in both cloud and onprem flavors.
Also when directly building theWebapp, I’d like it to build the cloud project as the default.
The root settings.gradle has: include "common", "cloud"
Note that this is badly supported by IDEs and most of the time unneeded. I think you’d generally be better off if you rewrite your common code so that it can be compiled by itself and so that the flavor projects can plug into it.
If you really want to compile the flavors and common code together, you wouldn’t do that with separate projects, but instead use one source set per flavor. But I’m pretty sure you’d have to tune a lot of things to make gretty and the war plugin work with that. So I encourage you to follow my first suggestion of decoupling your projects better.
Because your common project buids a ‘war’. Wars are not magically included into each other. What you probably want is a web library (a jar file containing the common resources). This is supported by Servlet 3.x. You put them into META-INF/resources and they are visible in downstream webapps as if you had included them in their resource directory.
Why don’t you just give it a try and see how it goes? I’d generally go with the more modern approach though. This is exactly what Servlet 3.x was designed for, so you don’t have to explode wars anymore.
Finally, works!
For those who are interested in an answer.
2 ways to acheive this properly:
Using gretty
The flavor project (lets say cloud) build.gradle just needs the following settings:
gretty {
overlay ':common'
}
The following command will start a webserver with merged webapp files: gradle :cloud:appRun
To build a merged war: gradle :cloud:assemble
Using servlet3.0 resource jar
Move all the common webapp files from theWebapp/common/src/main/webapp/*
to theWebapp/common/src/main/resources/META-INF/resources/
Then just call classic: gradle :cloud:war