[英]Partially compile gradle project with java-6 and java-7
I want to compile some of my code in my project with java6 and some with java7. 我想用java6编译项目中的某些代码,并用java7编译一些代码。 It doesn't seem like I can assign different source/target compatibilities for different source sets. 我似乎无法为不同的源集分配不同的源/目标兼容性。 I know this is a weird setup, but the java7 code depends on some java7 libraries and the java6 code has to be loaded in by some other java6 code (beyond my control) 我知道这是一个奇怪的设置,但是java7代码依赖于某些java7库,并且java6代码必须由其他java6代码加载(超出我的控制范围)
I've been trying to use a subproject 我一直在尝试使用子项目
java7-project
\build.gradle (compatibility = 1.7, compile project(':java6-submodule'))
\settings.gradle
\java6-module
\build.gradle (compatibility = 1.6)
This actually work fine, BUT, I want java6-module to be included as part of java7-project when I create the jar. 这实际上可以正常工作,但是,当我创建jar时,我希望将java6-module作为java7-project的一部分包含在内。 I can also do this... using (some code from the internet) 我也可以使用...(互联网上的一些代码)来执行此操作
jar {
from {
project(':java6-module').configurations.archives.allArtifacts.files.collect {
zipTree(it)
}
}
}
however, when I run the :install task to put the library in my local maven repo, the generated pom for java7-project has a compile dependency on "java6-module". 但是,当我运行:install任务将库放入本地maven存储库中时,为java7-project生成的pom对“ java6-module”具有编译依赖性。
I want to be able to compile different sections of code with different java versions while also treating it (or simulating the behavior) as one module? 我希望能够使用不同的Java版本编译不同的代码部分,同时将其(或模拟行为)视为一个模块?
I ended up using a separate sourceSet for my java6 code as Peter suggested, and add to the build file something like this : 我最终按照Peter的建议为Java6代码使用了一个单独的sourceSet,然后将如下所示添加到构建文件中:
sourceSets {
java6Src // new source set
main { // make sure our new source set is included as part of the main so it compiles and runs
compileClasspath += java6src.output
runtimeClasspath += java6Src.output
}
}
compileJava6SrcJava { // set the compile options
sourceCompatibility = 1.6
targetCompatibility = 1.6
// if jdk6.home is defined use it for compatibility
def jdk6Home = System.properties['jdk6.home']
if(jdk6Home) {
options.bootClasspath = (new File(jdk6Home,"/jre/lib/rt.jar")).canonicalPath
}
}
jar { // include java6Src set in the jar
from {
sourceSets.java6Src.output
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.