[GRADLE-2198] Tooling API: Deduce project language level from the java plugin properties via the Tooling API Created: 27/Mar/12  Updated: 11/Mar/16  Resolved: 17/Feb/16

Status: Resolved
Project: Gradle
Affects Version/s: 1.0-milestone-9
Fix Version/s: 2.11-rc-1

Type: Bug
Reporter: Denis Zhdanov Assignee: Unassigned
Resolution: Fixed Votes: 37

Attachments: PNG File スクリーンショット 2016-03-06 22.05.52.png    

 Description   

Consider the following simple script:

build.gradle
apply plugin: 'java'

configurations {
	all*.exclude group: 'commons-logging', module: 'commons-logging'
}

repositories {
	mavenLocal()
}

dependencies {}

test {
	useTestNG()
}

compileJava {
	sourceCompatibility = '1.7'
	targetCompatibility = '1.7'
	
	options.encoding = 'UTF-8'
	options.compilerArgs = ['-Xlint', '-Xlint:-serial', '-Xlint:-path']
}

compileTestJava {
	sourceCompatibility = '1.7'
	targetCompatibility = '1.7'
	
	options.encoding = 'UTF-8'
	options.compilerArgs = ['-Xlint', '-Xlint:-serial', '-Xlint:-path']
}

We're expecting to see language level '1.7' from org.gradle.tooling.model.idea.IdeaProject.getLanguageLevel() built against the script. Unfortunately, it always returns 1.6 (default value?).



 Comments   
Comment by Szczepan Faber [ 24/Apr/12 ]

Hey,

You should configure the compatibility outside of the tasks, i.e. as top-level elements of the build.gradle:

apply plugin: 'java'

sourceCompatibility = '1.7'
targetCompatibility = '1.7'
Comment by Szczepan Faber [ 24/Apr/12 ]

Denis,

If the users prefers different language level than the one assigned by Gradle he can configure it: http://gradle.org/docs/current/dsl/org.gradle.plugins.ide.idea.model.IdeaProject.html#org.gradle.plugins.ide.idea.model.IdeaProject

After configuring it, the tooling api will use that setting.

I'll close this issue if that's ok. Please reopen if necessary!

Comment by Szczepan Faber [ 18/May/12 ]

Yet another incarnation of somewhat poor support for language level in the Gradle's idea plugin.

Blocking this JetGradle feature: http://youtrack.jetbrains.com/issue/IDEA-83528

Comment by Dimitar Dimitrov [ 03/Mar/15 ]

There is still no way to set language level per module (as opposed to globally for the project).

A use case would be, where we have a project that has multiple modules, some of them API libraries, shared with other applications. We need the whole project to use 1.8, except for the API we need to keep to 1.6.

Our Gradle build does proper cross-compilation, and we can hack the IDEA projects generated from Gradle, but the IDEA tooling can not access the per module language level as it is not exposed by the tooling api.

Comment by Asaf Mesika [ 30/Nov/15 ]

I have a multi module projects. I've placed the attributes as specified in the top level build.gradle file, and still IDEA looks at my module as if it is Java 6 instead of java 8:

sourceCompatibility = 1.8
targetCompatibility = 1.8
Comment by Maciej Łabędzki [ 03/Dec/15 ]

Hi Guys,
I face similar issue to what Asaf describes. Every time I start IDEA modules have Java language level reverted back to 1.6. Then I manually set this value to 1.8 for each module separately. This is remembered until I restart IDEA. Then I need to do that manual work again. It's really annoying.

For time being, can you suggest some work-around for this issue?

Comment by Michel Jung [ 16/Jan/16 ]

Is this project dead? Almost 4 years later, no solution?

Comment by Vasiliy Kudriyavtsev [ 12/Feb/16 ]

Can any one clarify the status of this issue? It had been targeted for gradel 2.11-rc1, then reopened, but gradle 2.11 final Release Notes (https://docs.gradle.org/current/release-notes#enhanced-support-for-developing-java-projects-in-intellij-idea-and-eclipse) mention it like implemented.
But it does not work for me with gradle 2.11 and idea 15

Comment by Jacob Ilsø [ 12/Feb/16 ]

From reading the 2.11 release notes I think IDEA needs a change as well to support this.

Comment by J Q [ 06/Mar/16 ]

This issue has not been fixed yet.
Gradle's plugin for IDEA does not respect build.gradle's sourceCompatibility/targetCompatibility property, instead, it use IDEA's setting: File -> Other Settings -> Default Project Structure -> Project Language Level -> 6 (By default).

I have a gradle Java project. The build.gradle file is here,

apply plugin: 'java'
...
sourceCompatibility = 1.8
targetCompatibility = 1.8
...

When i run gradle in command line, the sourceCompatibility/targetCompatibility property does works.
(Because If i change it to 1.6, build fails. My gradle version is 2.11)

Then, i use IntelliJ IDEA 15 to import this gradle project, it generates some configuration files in .idea dir.
The .idea/misc.iml contains a language level property which is always set to 1.6:

  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/classes" />
  </component>

Once i change IDEA's Default Project Structure's Project Language Level to 8, (see above screenshot, change 6 to 8),
then the imported project will works as excepted: use Language Level 8.
The auto-generated .idea/misc.xml contains:

  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/classes" />
  </component>
Comment by nabarun nag [ 11/Mar/16 ]

I am still facing the same issue with language level defaulting back to 6 while the gradle file had the setting [ Gradle 2.11 and IntelliJ 15CE]
Current settings:

javac.configure {
        sourceCompatibility '1.8'
        targetCompatibility '1.8'
        options.encoding = 'UTF-8'
      }

After reading through the 2.11 release docs,

If a Gradle build script uses the DSL to explicitly specify idea.project.languageLevel, the sourceCompatibility level is not taken into account. In this case only the generated IDEA project will contain a value for 'Language Level', and no module-specific overrides will be generated.

I tried setting the languageLevel as 1.8 but IntelliJ threw an error

idea {
    project{
    languageLevel = '1.8'
    }
      module {
      downloadSources = true
      scopes.PROVIDED.plus += [ configurations.provided ]
    }
  }

Error :

Cannot set property 'languageLevel' on null object

Any updates or code modification are welcomed.

Generated at Wed Jun 30 12:15:23 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.