简体   繁体   English

使用Java-6和Java-7部分编译Gradle项目

[英]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.

相关问题 Java-6到Java-7 Kerberos-打破行为更改sessionKey现在AP-REQ.Authenticator.subkey - Java-6 to Java-7 Kerberos - breaking behaviour change sessionKey now AP-REQ.Authenticator.subkey 为什么Gradle无法编译Java项目? - Why Gradle fails to compile a Java project? 无法在Debian机器上安装Java-7 - Not able to install Java-7 on Debian machine 不使用gradle的java插件编译java项目? - Compile java project without using gradle's java plugin? 无法使用相邻项目中添加的.java文件编译Gradle项目 - Not able to compile a Gradle project with added .java files from a neighboring project Java-如何在不通过Gradle项目进行编译的情况下使用依赖项 - Java - How to use a dependency without compile it with the Gradle project Gradle - 是否有可能在项目的其余部分之前编译单个 Java 类? - Gradle - Is there a possibility to compile a single Java class before the rest of the project? 在Gradle编译一个JDK 8项目+一个JDK 9“module-info.java” - Compile a JDK 8 project + a JDK 9 "module-info.java" in Gradle Java-5 ThreadPoolExecutor相对于Java-7 ForkJoinPool有什么优势? - What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool? Gradle编译生成的java文件 - Gradle compile generated java file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM