How can I create a pathing jar in Gradle

Here is a tested solution:

task pathingJar(type: Jar) {
  appendix = "pathing"
  doFirst {
    manifest {
      attributes "Class-Path": configurations.compile.files.join(" ")
    }
  }
}

compileGroovy {
    dependsOn(pathingJar)
    classpath = files(pathingJar.archivePath)
}    

Depending on your exact requirements, you might have to tweak this a bit. For example, if you have tests written in Groovy, you will also need a pathing Jar for the test compile class path. In this case you'll need to repeat above configuration as follows:

task testPathingJar(type: Jar) {
  appendix = "testPathing"
  doFirst {
    manifest {
      attributes "Class-Path": configurations.testCompile.files.join(" ")
    }
  }
}

compileTestGroovy {
    dependsOn(testPathingJar)
    classpath = files(testPathingJar.archivePath)
}    

I finally got the "pathing jar" idea to work. I consider this to be a permanent workaround. This could be considered a solution if it is made part of gradle itself.

The original pathing jar code was provided by Peter, but it didn't work. The problem: classpath elements referenced in the pathing jar must be relative to the location of the pathing jar. So, this appears to work for me.

task pathingJar(type: Jar , dependsOn: 'cleanPathingJar') {
/**
 * If the gradle_user_home env var has been set to 
     * C:\ on a Win7 machine, we may not have permission to write the jar to
 * this directory, so we will write it to the caches subdir instead.  
     * This assumes a caches subdir containing the jars
 * will always exist.
 */
gradleUserHome = new File(gradle.getGradleUserHomeDir(), "caches")

relativeClasspathEntries = configurations.compile.files.collect {
    new File(gradleUserHome.getAbsolutePath()).toURI().
                  relativize(new File(it.getAbsolutePath()).toURI()).getPath()
}
appendix = "pathing"
destinationDir = gradleUserHome
doFirst {
    manifest {
        attributes "Class-Path": relativeClasspathEntries.join(" ")
    }
}
}

compileGroovy {
    dependsOn(pathingJar)
    classpath = files(pathingJar.archivePath)
}

Tags:

Groovy

Gradle