Build options (product tastes) in intellij java application

Is it possible to have build options based on different sets of sources for a traditional java application (NOT an android project) in intellij?

I would like to use a feature like productFlavors, which comes with the android gradle plugin, but for a regular Java application.

Example:

library_red -- HelloImpl.java library_blue -- HelloImpl.java library_common -- Hello.java compiled library_blue -- Hello.class, HelloImpl.class compiled library_red -- Hello.class, HelloImpl.class 
+8
java intellij-idea build.gradle gradle
source share
2 answers

The answer is yes, but you will have to use the new Gradle software model, which is very incubating. It will be a road full of pain as you will be a track blazer as I learned to use it for a C / Cpp project. Here is what your assembly looks like.

 plugins { id 'jvm-component' id 'java-lang' } model { buildTypes { debug release } flavors { free paid } components { server(JvmLibrarySpec) { sources { java { if (flavor == flavors.paid) { // do something to your sources } if (builtType == buildTypes.debug) { // do something for debuging } dependencies { library 'core' } } } } core(JvmLibrarySpec) { dependencies { library 'commons' } } commons(JvmLibrarySpec) { api { dependencies { library 'collections' } } } collections(JvmLibrarySpec) } } 

Link: https://docs.gradle.org/current/userguide/java_software.html

0
source share

We use Gradle multi-module projects for our alternative system. There is a main project containing common code. Setup is simple in subprojects.

 subprojects { dependencies { compile project(':core') } } 

Variant subprojects depend on the kernel and each individual .war files are built. Please note that in this case we do not redefine classes from the main project. We use Spring and, in some cases, SPI to set up the code, but I assume that any dependency injection infrastructure can do this. It just makes you provide explicit extension points in the kernel, which I think is good.

0
source share

All Articles