The idea plugin breaks the new Intellij 13 .iml configuration

If I use the “ideaModule” task against a new Intellij 13 .iml file, it breaks some of the new settings for “resources” and “test resources”. Below is a snippet of my .iml file.

NEW (this is the new Intellij 13 format for “Mark as” sources). You can now mark directories as: Sources, Tests, Resources, Test Resources, Excluded. The old format was only: Sources, Tests, and Excluded.

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />

GRADLE OLD. The Gradle idea plugin reverts the IML file back to the old format. This causes an error in Intellij 13 because I now have the same “resources” included in two different locations (you can see an example of that below). You can see in the format above the “type” attribute is dropped and the format is restructured. The plugin is basically breaking the new formatting in the configuration.

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
      <excludeFolder url="file://$DEVROOT$/build/arch" />
      <excludeFolder url="file://$MODULE_DIR$/.gradle" />

The idea plugin also removes any assigned “module dependencies”.

This is gradle version 1.8, intellij version 13, java 1.7.45.

Thanks for the report. Apparently, the file format in IDEA 13 has changed incompatibly, and Gradle’s ‘idea’ plugin may have to catch up with that. In any case, the preferred way to integrate with IDEA 13 is to import the project using IDEA’s Gradle support, rather than generating project files with ‘gradle idea’. You can still apply the ‘idea’ plugin though and configure it as necessary. Does this solve the problem?

Sorry, no it doesn’t solve it. We have “ideaModule” in our default tasks. We use this at the beginning to keep the .iml files for each module in sync with the dependencies listed in build.gradle. When the build is executed any new or broken dependencies were fixed by the build process. This would keep the Intellij module and dependencies in sync with the build.gradle. They still are, but the problem above breaks a different area (at least I believe it still works, I didn’t notice it breaking the dependencies).

We can work around it by just manually fixing the module in intellij. I have just removed the “ideaModule” task for now to keep it from regenerating/changing the iml. We can work around it. I just wanted to bring it to your attention.

Thanks for the response.

When using IDEA’s Gradle support, you no longer run the ‘ideaModule’ task, but press "Sync with Gradle’ whenever necessary. I believe there is also a way to sync the project automatically, though I’m not completely sure.

Hey Peter I’m actually struggling with the IDEA’s Gradle support. My java modules have customized source/resource/test folders and Intellij doesn’t seem to be able to pick those up. Moreover, I am not able to run ScalaTest specs in my scala modules. Complete disaster!

Generally, the Gradle support in IDEA 13 works quite well, much better than in 12. (Make sure to use IDEA’s “import” function, and don’t generate the project files with Gradle.) If you have concrete problems, please report them to JetBrains.

I am using Gradle (1.11) and IntelliJ 13.1.4. When I click the icon to “Refresh all Gradle Projects” (Blue circular arrows on the Gradle tool window), directories that I have marked as Test roots and Resources roots are cleared of their markings. I do not believe that I am using the idea module. I have tried commenting out the apply plugin: ‘idea’ in my gradle file, but that doesn’t prevent the clearing of the markings.

Try using the “Local Gradle distribution” under “Preferences > Gradle”. Although IntelliJ recommends that you choose the “Default Gradle wrapper”, I discovered that it does not add the files under “resources” to the classpath.

‘apply plugin: “idea”’ is necessary no matter how you integrate with IntelliJ.

Looks like it is an IntelliJ bug, but there is a good workaround: http://youtrack.jetbrains.com/issue/IDEA-91952#comment=27-769929

Put this in your build.gradle:

apply plugin: 'idea'
idea {
    module {
        inheritOutputDirs = true
    }
}

or this:

apply plugin: 'idea'
idea {
    module {
        inheritOutputDirs = false
        outputDir = file('muchBetterOutputDir')
        testOutputDir = file('muchBetterTestOutputDir')
    }
}

Alas, it does not appear that this affects resource directories.

what if I had to exclude a folder under a directory?

excludeDir -= file(‘the folder under source directory’)?

This (workaround?) worked for setting up a “testResource” directory. Just modify as required for a normal “resource” directory. (Improvements welcome!)

Intellij IDEA Ultimate 14 Gradle 2.2

apply plugin: 'idea'
  idea {
    module.iml.withXml {
        def atts = it.asNode().component.content.sourceFolder
                .find { it.@url == 'file://$MODULE_DIR$/src/test/resources' }
                .attributes()
  atts.remove('isTestSource')
  atts.put('type', 'java-test-resource')
    }
}