Build multiple cans from a single project source

I am new to gradle.

I would like to know if it is possible to build several cans from the same project source. I looked through previous similar questions, but my situation is a little different. I would like to compile all the source files twice to create two different banks, each of which has different dependencies and different artifact identifiers. Conceptually, it can be represented as follows:

  • project "FooBase"
    • Src / main / java / ...
    • Depends on the Bar library version 1.0.0
    • compiles the entire source and creates "org.foo/Foo1" (group / artifact)
  • project "Foo2"
    • there is no own source or can add a source
    • Depends on the Bar library version 2.0.0
    • compiles the entire source in "FooBase" and its source and produces "org.foo/Foo2" (group / artifact)

Is such a construction possible with gradle? What would be a better approach? Thanks!

+4
source share
1 answer

One easy way to achieve what you want is to create a multi-project assembly with two subprojects (foobase and foo2), where the source kits for foo2 are configured to contain the foobase source kits in addition to their own sources,

To get different dependencies for artifacts, you just need to declare the dependencies section differently in subprojects.

To test this, I created a multiproject assembly with one java file in each subproject. To simplify the output here, the build.gradle root file contains everything, including special subproject settings. However, in "real life" I always added specific subproject configurations to the build.gradle file at the correct subproject level.

The gradle build file contains

  • Adding source sets from foobase to foo2
  • Different dependencies in two projects
  • Deploy to a local maven repository (to make sure pom are created correctly)
  • Additional IDE Plugins for IntelliJ IDEA and Eclipse

In general, I did the following:

Project structure

 build.gradle => root project build file settings.gradle => specification of included subprojects foo2\ => foo2 subproject folder src\ main\ java\ Foo2.java => Empty class foobase\ => foobase subproject folder src\ main\ java\ FooBase.java => Empty class 

settings.gradle

 include ':foobase', ':foo2' 

build.gradle

 allprojects { apply plugin: 'idea' apply plugin: 'eclipse' group = 'org.foo' version = '1.0' } subprojects { apply plugin: 'java' apply plugin: 'maven' repositories { mavenCentral() } uploadArchives { it.repositories.mavenDeployer { repository(url: "file:///tmp/maven-repo/") } } } project(':foobase') { dependencies { compile 'log4j:log4j:1.2.13' } } project(':foo2') { dependencies { compile 'log4j:log4j:1.2.16' } sourceSets.main.java.srcDirs project(':foobase').sourceSets.main.java sourceSets.main.resources.srcDirs project(':foobase').sourceSets.main.resources sourceSets.test.java.srcDirs project(':foobase').sourceSets.test.java sourceSets.test.resources.srcDirs project(':foobase').sourceSets.test.resources } 

Note that I have added source directories for resources and tests. You can omit the last three lines if this is not required.

To check the assembly:

  • Make sure the two jar files contain all the classes you need.
  • Make sure the two deployed maven pom have the correct dependencies.

In my case:

  • foobase-1.0.jar contains only FooBase.class
  • foo2-1.0.jar contains both FooBase.class and Foo2.class
  • the loaded foobase-1.0.pom contains a dependency on log4j-1.2.13
  • the downloaded foo2-1.0.pom contains a dependency on log4j-1.2.16
+5
source

All Articles