如何在Gradle中创建路径jar

Ray*_*lus 6 groovy gradle

在Windows环境中运行groovyc时,由于类路径的长度,我遇到了问题.我想通过创建一个路径jar来解决这个问题,然后将该jar放在cp上.如何使用gradle中自动指定的所有类路径条目创建路径jar,然后将该jar添加到cp?

Pet*_*ser 10

这是经过测试的解决方案:

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

compileGroovy {
    dependsOn(pathingJar)
    classpath = files(pathingJar.archivePath)
}    
Run Code Online (Sandbox Code Playgroud)

根据您的具体要求,您可能需要稍微调整一下.例如,如果您使用Groovy编写测试,则还需要一个用于测试编译类路径的路径Jar.在这种情况下,您需要重复上述配置,如下所示:

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

compileTestGroovy {
    dependsOn(testPathingJar)
    classpath = files(testPathingJar.archivePath)
}    
Run Code Online (Sandbox Code Playgroud)


Ray*_*lus 5

我终于得到了"路径罐子"的想法.我认为这是一个永久的解决方法.如果它成为gradle本身的一部分,这可以被认为是一种解决方案.

最初的路径jar代码由Peter提供,但它没有用.问题:路径jar中引用的类路径元素必须相对于路径jar的位置.所以,这似乎对我有用.

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)
}
Run Code Online (Sandbox Code Playgroud)

  • 这[在3.0.8和3.0.8之间的Groovy版本中不起作用](https://github.com/grails/grails-core/issues/9300)。链接的Grails问题还包括使用此方法的完整解决方法。 (2认同)