开发者

How can I create a pathing jar in Gradle

开发者 https://www.devze.com 2023-02-20 07:48 出处:网络
When running groovyc in a Windows env, I am running into issues due to the length of the classpath, in my situation.I would like to work around this by creating a pathing jar, and then put that jar on

When running groovyc in a Windows env, I am running into issues due to the length of the classpath, in my situation. I would like to work around this by creating a pathing jar, and then put that jar on the cp. How can I create a pathing jar w/ all of the classpath entries s开发者_Python百科pecified automatically in gradle and then add that jar to the cp?


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)
}


This is what helped me:

"The filename or extension is too long error" using gradle

In other words: use the com.github.ManifestClasspath plugin.

The other solutions did not work for me because the actual project main class ended up no being included in the classpath at execution time.

0

精彩评论

暂无评论...
验证码 换一张
取 消