Tom G
Tom G

Reputation: 2045

In gradle: How to build two fat jars, identical except for different dependency?

I have a gradle project that successfully builds a fat jar (i.e. containing its own dependencies). What I need, however, is for it to build two jars:

Apart from that, they should be identical.

Can I do this in gradle?

I have the requirement that I can only call gradle once (i.e. can't call gradle once for each jar).

The relevant parts of my gradle file look like this:

buildscript {
    ext {
        springBootVersion = '1.3.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.4.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: "io.spring.dependency-management"

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {

    ...

    compile('org.postgresql:postgresql:42.1.1.jre7') // or compile('postgresql:postgresql:8.4-702.jdbc4')

    ...
}

eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

...

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

Upvotes: 1

Views: 702

Answers (2)

Tom G
Tom G

Reputation: 2045

In case this helps anyone, this is how I did it:

  1. extract the existing jar into a folder:

    task extractJar(type: Copy, dependsOn: [otherTasksThatBuildFirstJar]) {
        from zipTree(jar.archivePath)
        into 'build/jarContents'
    }
    
  2. delete the dependency that I don't want:

    task deletePostgres(type: Delete, dependsOn: [extractJar]) {
        delete fileTree('build/jarContents/lib') {
            include '**/postgres*.jar'
        }
    }
    
  3. add in the dependency that I do want:

    task replacePostgres(type: Copy, dependsOn: [deletePostgres]) {
        from 'lib/postgresql-8.4-702.jdbc4.jar'
        into 'build/jarContents/lib'
    }
    
  4. build the second jar:

    task buildSecondJar(type: Jar, dependsOn: [replacePostgres]) {
        baseName 'my-second-jar'
        entryCompression ZipEntryCompression.STORED //prevents compression of jars
        from  files('build/jarContents')
        destinationDir project.rootDir
        manifest {
            attributes(
                    'Start-Class': 'com.somewhere.MyApplication',
                    'Spring-Boot-Classes': 'BOOT-INF/classes/',
                    'Spring-Boot-Lib': 'BOOT-INF/lib/',
                    'Spring-Boot-Version': '1.5.8.RELEASE',
                    'Main-Class': 'org.springframework.boot.loader.JarLauncher'
            )
        }
    }
    

Upvotes: 1

tkruse
tkruse

Reputation: 10685

If it is okay for you to run gradle once for one jar, a simple command line property and an if block can do the job. Also see here: Passing properties to a gradle build

Also look at duplicate Question: Gradle How to build several versions of a jar with different dependencies

Upvotes: 0

Related Questions