Reputation: 4276
I'm using (or trying to use) Gradle to build a plain Java (not Android) multi-module project, which contains a CLI and several micro-services. I have a simple, single-configuration build working.
I'd like to be able to build it two different ways: a "development" build with one group of settings and dependencies, and a "deployment" build with different settings and dependencies. Some settings and dependencies will overlap between the two.
In other build tools, this would correspond to "Debug" and "Release" build configurations. But for Gradle, I've seen build types, variants, flavors, and capabilities, and combinations of all of those—some of which seem to be Android specific, some depending on plugins that seem to have fallen out of date. But I can't seem to locate a straightforward example of a "traditional" debug/release build setup.
I have a simple approach working using manually created buildDebug
, buildRelease
, assembleDebug
, assembleRelease
, etc. tasks, but it feels like I'm working around Gradle rather than with it.
Does anyone have such an example who would be willing to share their work? Many thanks!
Upvotes: 2
Views: 989
Reputation: 4276
It looks like my early searches (i.e. "gradle debug and release builds") and my expectation of something built into Gradle sent me down the wrong rabbit hole. I finally stumbled across this question only after it occurred to me to search on "gradle equivalent of maven build profiles".
It's possible I'm missing a Gradle feature (i.e. variants) I could be taking advantage of, but it appears the correct solution may be:
ext {
env = findProperty('env') ?: 'debug'
}
dependencies {
// shared dependencies
if (env == 'debug') {
// debug build dependencies
}
if (env == 'release') {
// release build dependencies
}
}
The build is selected by setting the env
property on the command line:
# debug build; can use either
$ gradle build
$ gradle build -Penv=debug
# release build
$ gradle build -Penv=release
Hope that helps a fellow Gradle newbie.
Upvotes: 2