Single Source Project creating Multiple Interdependent Jars

I’m migrating a java project with a single source code directory to be built by gradle.

  • I’m currently Building 4 jar files: my-company-1.0.jar, my-company-core-1.0.jar, my-company-images-1.0.jar, my-company-utils-1.0.jar
  • I don’t want the jar file my-company-1.0.jar (containing the entire compiled source) to be created
  • I want the core jar to be created after the images and utils jars
  • I want to add images and utils jars to the Manifest Class-Path of the core jar

I can not split the code into subprojects or modify the source code location.
This is my simplified build.gradle at the moment:

apply plugin: 'java'

version = '1.0';
def myCompany = 'My Company'

repositories {
    mavenCentral()
    flatDir{
        dirs 'internal_jars' 
    }
}

sourceSets {
    main.java.srcDir 'src'
    main.resources { 
        srcDirs = ['src']; 
        exclude "**/Thumbs.db"
        exclude "**/*.old"
    } 
}

dependencies {
    compile name: 'barcode' // from internal_jars directory
    compile 'com.itextpdf:itextpdf:5.5.2' // itext
    compile 'org.jdom:jdom:1.1' // jdom
} 

jar {
}

// Core runnable jar
task coreJar(type: Jar) {  
    appendix = 'core'

    from sourceSets.main.output
    include 'com/my_company/application/**'
    exclude 'com/my_company/application/images/**'
    
    manifest {
        attributes 'Implementation-Title': "MyApp ($archiveName)",
                   'Implementation-Version': version,
                   'Created-By': myCompany,
                   'Main-Class': 'com.my_company.application.Main',
                   'Class-Path': configurations.compile.collect{ it.getName() }.join(' ')
                   // Need to add utils.jar and images.jar here
    }
    
}

// Contains utility classes
task utilsJar(type: Jar) {  
    appendix = 'utils'
    from(sourceSets.main.output) {
        include "com/my_company/utils/**"
    }
}

// Contains images and image related classes
task imagesJar(type: Jar) {  
    appendix = 'images'
    from(sourceSets.main.output) {
        include "com/my_company/application/images/**"
    }
}

artifacts {
    archives utilsJar, imagesJar, coreJar
}

// Closure for adding the baseName my-company to all jar files and the
// manifest to all but the core jar file, as this is runnable with a different
// manifest.
def allJars = tasks.withType(Jar).matching { jar ->
    jar.baseName = 'my-company'
    if (jar.appendix != 'core') {
        //        jar.dependsOn classes
        jar.manifest {
            attributes 'Implementation-Title': "MyApp ($jar.archiveName)",
                       'Implementation-Version': version,
                       'Created-By': myCompany
        }
    }
}

task myCompanyJars(dependsOn: allJars)

Any help/pointers?

Thank you.