2

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!

2
  • Interesting requirements! Could you give some more concrete examples what those settings and dependencies are? Commented May 8, 2020 at 11:30
  • I'm was concerned that more specifics might throw off the discussion. Commented May 8, 2020 at 12:03

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.