Skip to content

Instantly share code, notes, and snippets.

@zedar
Last active August 29, 2015 14:11
Show Gist options
  • Save zedar/c213f4f57f590e92184c to your computer and use it in GitHub Desktop.
Save zedar/c213f4f57f590e92184c to your computer and use it in GitHub Desktop.
Gradle - conficting jars in plugins

Gradle - conflicting jar files (plugin) on classpath

Problem description

If you get an issue related to incorrect jar files on classpath while invoking gradle plugin, move the plugin's tasks into seperate build file. Use GradleBuild task type with reference to seperated build file.

Example

When developing small extension for ratpack manual I had to add gradle-js plugin with two tasks for combining javascript files into one file and then minifying it. Minification task thrown exception idicating that there are conflicting jar file versions for google's guava library:

java.lang.NoSuchMethodError: com.google.common.io.ByteStreams.limit

Solution

Create separate gradle build file with plugin dependcies and task definitions:

// minifyjs.gradle
buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath "com.eriwen:gradle-js-plugin:1.12.1"
  }
}

apply plugin: "com.eriwen.gradle.js"

// grunt-js tasks
// merge listed js files into one file
combineJs {
  source = [
    "${projectDir}/src/assets/js/googleAnalytics.js",
    "${projectDir}/src/assets/js/modernizr.js",
    "${projectDir}/src/assets/js/prism.js",
    "${projectDir}/src/assets/js/jquery.js",
    "${projectDir}/src/assets/js/anchorHighlight.js",
    "${projectDir}/src/assets/js/toggleImports.js"
  ]
  dest = file("${buildDir}/assets/js/all.js")
}

minifyJs {
  source = combineJs
  dest = file("${buildDir}/assets/js/all.min.js")
}

Add new task definition in your main gradle build file with reference to minifyjs.gradle:

// ratpack-manual.gradle
task buildJs(type: GradleBuild) {
  buildFile = "minifyjs.gradle"
  tasks = ["combineJs", "minifyJs"]
}

Call new task in the target task

// ratpack-manual.gradle
task stylizeManual(type: Sync) {
  into "$buildDir/stylized-manual"
  from compileManual
  from compassCompile
  from buildJs    // <=== call external tasks
}

The minifyjs.gradle tasks are executed in their own environment, so with their own classpath.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment