Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: assembling multiple source-sets into one jar

Tags:

java

gradle

I've asked a related question here JOOQ class generation and gradle

In that question I'm trying to find the best way to do a multi-stage build including generating classes in a middle step. I've gone down the Option Two approach, and now find myself an impasse.

I have the following build.gradle file

apply plugin: 'java'
apply plugin: 'eclipse'

sourceSets
{
    bootstrap 

    generated {
        compileClasspath += bootstrap.output
    }

    main {
        compileClasspath += bootstrap.output
        compileClasspath += generated.output
    }
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'org.jooq:jooq-codegen:3.5.0'
        classpath 'postgresql:postgresql:9.1-901.jdbc4'
        classpath project(":")
    }
}

dependencies
{
    compile 'org.jooq:jooq:3.5.0'
    compile 'org.jooq:jooq-codegen:3.5.0'
    compile 'org.apache.poi:poi:3.10.1'
    compile 'com.google.guava:guava:18.0'

    generatedCompile 'org.jooq:jooq:3.5.0'
    generatedCompile 'org.jooq:jooq-codegen:3.5.0'
    generatedCompile 'org.apache.poi:poi:3.10.1'
    generatedCompile 'com.google.guava:guava:18.0'

    bootstrapCompile 'org.jooq:jooq:3.5.0'
    bootstrapCompile 'org.jooq:jooq-codegen:3.5.0'
    bootstrapCompile 'org.apache.poi:poi:3.10.1'
    bootstrapCompile 'com.google.guava:guava:18.0'
}

task generate << {
    //Use JOOQ to generate classes, with the output going into the generated sourceSet
          .withDirectory(file("src/generated/java").getAbsolutePath())
}

generatedClasses
{
    dependsOn bootstrapClasses
    dependsOn generate
}

jar
{
    dependsOn generatedClasses
    dependsOn bootstrapClasses
}

The structure is that

  • the bootstrap source set holds some core java classes that are required for the code generation, plus an sql file that will be used to pouplate a database
  • The generate task uses the classes and sql file in boostrap to generate classes
  • The generated source set holds the outputs of the generation task, and
  • The main source set holds what might be called the "normal" classes (i.e. the ones that make use of the database being described by the bootstrap and generated classes)

I have a couple of problems, which I can't untangle:

  1. I seem to have to duplicate the dependencies for each source-set
  2. When the jar file gets built, it only contains the classes generated from the main source set

I should note that the build as it stands above will successfully generate each of the source-sets.

Any help would be greatly appreciated.

like image 695
Burleigh Bear Avatar asked Dec 03 '14 04:12

Burleigh Bear


1 Answers

O.K. I think I have found the answer to this question. There were two parts....

The first problem, having to specify the same dependencies multiple times, was fixed by adding this:

configurations {
    generatedCompile {
        extendsFrom compile
    }
    bootstrapCompile { 
        extendsFrom compile
    }
}

The second problem, the jar file not having all the build artefacts, was fixed by changing the jar task to

jar 
{
    from sourceSets.generated.output
    from sourceSets.bootstrap.output
    dependsOn bootstrapClasses
    dependsOn generatedClasses
}
like image 55
Burleigh Bear Avatar answered Oct 23 '22 22:10

Burleigh Bear