Question

I want to compile some of my code in my project with java6 and some with 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)

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. 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".

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?

Was it helpful?

Solution

I ended up using a separate sourceSet for my java6 code as Peter suggested, and add to the build file something like this :

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
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top