40

I have a project with MANY modules. We're upgrading to Java7, and I want my editor to reflect this. Now all my modules specifically set the language level to Java6, and there are too many modules for me to change this setting for each module. How do I set all the modules to Java7? Even better, how do I set all the modules to use the project's language level?

Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
  • 1
    This isn't a real answer, but I've changed the compiler version in the configuration of my maven-compiler-plugin and reimported the project. That seemed to work. – Niel de Wet Apr 01 '14 at 11:13

4 Answers4

24

As pointed out in the comment by Lambart to the other answer, the solution doesn't work for changing the target version for all the modules altogether.

Also, observe that setting the target level for the project is fine, but this target version is overridden by the one specified in a module.

If you, like me, are unlucky and need to work on a 100+ modules Java monolith, then changing modules one by one will be a pain.


My solution is "annoying" but works under LINUX. I assume in the example that you want to update form 1.5 to 1.8.

Step 1) You have to go in the .idea folder and look for the file compiler.xml.

Replace all the target values in the tag <module>, e.g.

target="1.5" 

to

target="1.8"

Step 2) go in the project folder and run the following script

find . -type f -name "*.iml" -print0 | xargs -0 sed -i "s/JDK_1_5/JDK_1_8/g"

to replace all the language level in the modules to be JDK8 compliant.

Community
  • 1
  • 1
JeanValjean
  • 17,172
  • 23
  • 113
  • 157
  • 1
    As a side note, you may want to recreate the project. But sometimes there are too much information connected to a project (run profiles, databases connections, etc.) that you would prefer to avoid to delete it and recreate it from scratch – JeanValjean Jan 06 '16 at 16:44
  • 5
    If you are using a mac be aware of the failing `sed -i` (see http://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux) - you have to use `find . -type f -name "*.iml" -print0 | xargs -0 sed -i "" "s/JDK_1_5/JDK_1_8/g"` instead! – luk2302 Mar 18 '16 at 12:31
  • 1
    @luk2302 yeah, I'm on LINUX. I specified this in my answer. Feel free to update it to add the mac usercase – JeanValjean Mar 18 '16 at 13:25
  • @JeanValjean still the module language is set to "5". Good research! – Gaurav Nov 15 '19 at 11:00
19

In IntelliJ IDEA 14.0, go to File | Project Structure | Modules (Ctrl+Shift+Alt+S).

The list of modules supports multiple selection so

  • Ctrl click individual modules
  • Shift click range of modules
  • Ctrl-A all modules

Select in the drop-down menu Language level and choose manually the level or "Use project language level".

Similarly to change the language level for the project, select project in the ribbon on the left under Project Settings. A drop down menu is available for Project language level. Choose the level needed. This will set the default for all project modules.

Sources

Community
  • 1
  • 1
ShooShoSha
  • 956
  • 10
  • 17
  • 3
    Thanks. This was helpful, except that it does NOT appear that selecting multiple modules allows you to change the `Language level` for them all at once. I had to select each individual module, and then was able to set its `Language level` to `Project Default`, which is what I wanted. – Lambart Jun 23 '15 at 22:34
  • 4
    Multiple selection works _only_ when **all** selected modules have the same language level — if the language level is mixed then setting the language level has no effect. Looks like a bug.. – ccpizza Jul 01 '16 at 08:36
  • 3
    Multi-select didn't work for me in IntelliJ 2017.3.2, even after ensuring all modules had the same language level to start with. – Matt Craig Jan 02 '18 at 21:45
  • 2
    @ShooShoSha multi-select didnt work. It just changed the value for first module. – Gaurav Nov 15 '19 at 11:02
  • @gaurav you may need to look at the language level when selecting modules as ccpizza commented. – ShooShoSha Nov 21 '19 at 17:53
6

The other solutions didn't worked for me, even after modifying by hand the compiler.xmland all the .iml files the the LANGUAGE_LEVEL was still set to "JDK_1_5". This was caused by the maven-compiler-plugin plugin's configuration option.

Changing it to the following fixed it:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
    </plugin>
</plugins>

Hope this solves it for others also.

iusting
  • 7,850
  • 2
  • 22
  • 30
  • 1
    Thank you @iusting it is much appreciated. i was working away nicely all day at 1.8 level. I added the above plugin, and suddenly my codebase had compile errors. This "feature" of Intellij is very bad. In fact, the same thing happens in Gradle, but thankfully there it is an in your face setting. Late at night, with a deadline, its a blessing to have a solution like this. Thanks again. – Beezer Feb 24 '20 at 20:18
0

Why not do "Command + Shift + R" (I am on Mac) and then check the box "File mask" and enter "*.iml". Then search and replace whatever you want.

enter image description here

Xun Ren
  • 441
  • 5
  • 12