I have Java 6 and 7 installed on my machine. Gradle uses 1.7 (checked using gradle -v
). But I need to compile my code to be compatible with Java 1.6. As far as I understand the documentation I can use the sourceCompatibility
property to do so (and indirectly the targetCompatibility
which defaults to the sourceCompatibility
).
So I added the following line to my build file (on the root level, not in any closure):
sourceCompatibility = 1.6
(to be sure I also added the targetCompatibility = 1.6
in some trials, but that should not make a difference)
To check whether the result was actually compatible with 1.6 I unzipped the resulting jar, cd
into the WEB-INF/classes
folder and used javap -verbose
on the first .class
file I encountered. But no matter whether I set the target compatibility or whether I used 1.5 instead of 1.6 or whether I specified it as string ('1.6'
), each time the result of javap was
minor version: 0
major version: 51
Afaik this means it is Java 1.7 Bytecode, which is wrong.
Any ideas why the sourceCompatibility
-setting doesn't work? Or is javap
not the correct way to check the compatibility?
UPDATE: Yes, this is actually a multi-project build but I only checked one of the subprojects' build results. In this subproject's build file I made the mentioned changes to be sure they are actually applied. In addition, I added the following in the root project's build file (as @Vidya proposed as well):
allprojects {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
But this didn't help either.
UPDATE 2: I checked the setting of sourceCompatibility with this snippet in the relevant build.gradle files:
compileJava.doFirst {
println "source compatibility " + sourceCompatibility
}
It revealed that my sourceCompatibility is set to 1.7 although I tried to set it to 1.6. When I extracted the simplest subproject and built in on its own the sourceCompatibility is set correctly and the Java Byte code is compatible to 1.6. However, even this sub-project uses the wrong sourceCompatibility when used in the multi project build.
BTW: The plugins I use in some of the sub projects are: java
, war
, jetty
, gwt
UPDATE 3:
I changed the built scripts to just use the java plugin (and thus just construct some jars) and removed the usage of the war
, jetty
and gwt
plugin. But still all the projects are set to sourceCompatibility 1.7 despite me setting it in the allprojects
section and in some of the sub projects. All that is left now in the build scripts is the declaration of some decencies (maven, files and other sub projects), the declaration of the repositories to use, the declaration of some others tasks (that the build-task does not depend on, so it shouldn't be affected) and the configuration of the manifest file for the created jar files (I add a specification and an implementation version and title to the manifest file).
I don't see how any of that would affect the sourceCompatibility setting.
targetCompatibility
. But I don't think you can have source compat > target compat, and you are right in that target compat defaults to source compat. Hence I'd expect this to just work. Is this a multi-project build? Are you sure you are settingsourceCompatibility
for the right project? – Limousinemajor version: 50
). At this point it's likely that it's a problem with your build. For example, some build script or third-party plugin might overwrite your configuration. But without a reproducible example, it's hard to help any further. One thing you can try is to check whatcompileJava.doFirst { println sourceCompatibility }
prints. Also try with a clean build, although it shouldn't be necessary. – LimousinecompileJava.doFirst { println sourceCompatibility }
. I added that and all of the sub-projects have sourceCompatibility set to 1.7. I'll check whether it's due to any of the plugins I use. – LeathcompileJava.sourceCompatibility
, which defaults toproject.sourceCompatibility
(which is what you've been setting so far). You can try to set the task-level properties (these are the ones that ultimately matter) directly withtasks.withType(JavaCompile) { sourceCompatibility = "1.6"; targetCompatibility = "1.6" }
, although usually that wouldn't be necessary. – Limousinetasks.withType
snippet in the root build.gradle-file will this also affect the tasks defined by the sub projects' build files or do I have to do it in each of the sub-build-files? – Leathtasks.withType(JavaCompile) { sourceCompatibility = "1.6"; targetCompatibility = "1.6" }
toallProjects{}
and it now works. Even with all the plugins in the original build script. If you add this as an answer I'll accept it. I'm still curious why it didn't work via the project attributes, though. – Leathproject.sourceCompatibility
androotProject.sourceCompatibility
appears to be set correctly but the task'ssourceCompatibility
is different. I am still curious what causes the setting to be overwritten. – Grumousallprojects
(orsubprojects
) clause in the root project. It seems to misbehave: If I sayallprojects { sourceCompatibility = 1.6 }
in the root, the settings does not work correctly in subprojects, not even if I redefine it again in the subproject directly. If I remove theallprojects
clause and only set thesourceCompatibility
in the subproject, it works as expected. It seems like a some kind of bug. If I am able to describe it more specificaly, I am going to report to the developers. Joachim, I can you please verify this in your setup? – Grumous