IdeaModule

API 文档IdeaModule

启用对 IDEA 插件的模块详情 (*.iml 文件) 进行微调。

一个混合了大多数可能属性的使用示例。通常你无需直接配置此模型,因为 Gradle 会为你配置它。

plugins {
    id 'java'
    id 'idea'
}

//for the sake of this example, let's introduce a 'performanceTestCompile' configuration
configurations {
  performanceTestCompile
  performanceTestCompile.extendsFrom(testCompile)
}

dependencies {
  //performanceTestCompile "some.interesting:dependency:1.0"
}

idea {

  //if you want parts of paths in resulting files (*.iml, etc.) to be replaced by variables (Files)
  pathVariables GRADLE_HOME: file('~/cool-software/gradle')

  module {
    //if for some reason you want to add an extra sourceDirs
    sourceDirs += file('some-extra-source-folder')

    //and some extra test source dirs
    testSources.from(file('some-extra-test-dir'))

    //and some extra resource dirs
    resourceDirs += file('some-extra-resource-dir')

    //and some extra test resource dirs
    testResources.from(file('some-extra-test-resource-dir'))

    //and hint to mark some of existing source dirs as generated sources
    generatedSourceDirs += file('some-extra-source-folder')

    //and some extra dirs that should be excluded by IDEA
    excludeDirs += file('some-extra-exclude-dir')

    //if you don't like the name Gradle has chosen
    name = 'some-better-name'

    //if you prefer different output folders
    inheritOutputDirs = false
    outputDir = file('muchBetterOutputDir')
    testOutputDir = file('muchBetterTestOutputDir')

    //if you prefer different SDK than the one inherited from IDEA project
    jdkName = '1.6'

    //put our custom test dependencies onto IDEA's TEST scope
    scopes.TEST.plus += [ configurations.performanceTestCompile ]

    //if 'content root' (as IDEA calls it) of the module is different
    contentRoot = file('my-module-content-root')

    //if you love browsing Javadoc
    downloadJavadoc = true

    //and hate reading sources :)
    downloadSources = false
  }
}

为了应对边缘情况,用户可以对生成的 XML 文件进行高级配置。也可以通过 beforeMerged 和 whenMerged 闭包影响 IDEA 插件合并现有配置的方式。

beforeMerged 和 whenMerged 闭包接收一个 Module 参数

高级配置示例

plugins {
    id 'java'
    id 'idea'
}

idea {
  module {
    iml {
      //if you like to keep *.iml in a secret folder
      generateTo = file('secret-modules-folder')

      //if you want to mess with the resulting XML in whatever way you fancy
      withXml {
        def node = it.asNode()
        node.appendNode('iLoveGradle', 'true')
        node.appendNode('butAlso', 'I find increasing pleasure tinkering with output *.iml contents. Yeah!!!')
      }

      //closure executed after *.iml content is loaded from existing file
      //but before gradle build information is merged
      beforeMerged { module ->
        //if you want skip merging exclude dirs
        module.excludeFolders.clear()
      }

      //closure executed after *.iml content is loaded from existing file
      //and after gradle build information is merged
      whenMerged { module ->
        //you can tinker with Module
      }
    }
  }
}

属性

属性描述
contentRoot

模块的内容根目录。

downloadJavadoc

是否下载并添加与依赖 jar 关联的 javadoc。默认为 false。

downloadSources

是否下载并添加与依赖 jar 关联的 sources。默认为 true。

excludeDirs

需要排除的目录。

generatedSourceDirs

包含生成源代码(生产和测试源代码)的目录。

iml

参阅 IdeaModule.iml(org.gradle.api.Action)

inheritOutputDirs

如果为 true,则此模块的输出目录将位于项目的输出目录下;否则,它们将被设置为由 IdeaModule.getOutputDir()IdeaModule.getTestOutputDir() 指定的目录。

jdkName

此模块使用的 JDK。如果为 null,则使用现有或默认 ipr XML 的值(继承)。如果设置为 inherited,则使用项目 SDK。否则,此模块将使用相应 Java 版本的 SDK。

languageLevel

此模块使用的特定模块语言级别。当为 null 时,模块将继承 IDEA 项目的语言级别。

name

配置模块名称,即 *.iml 文件的名称。

outputDir

生产类文件的输出目录。如果为 null,则不会创建条目。

outputFile

配置输出 *.iml 文件。它是可选的,因为 task 应该会为你正确配置它(包括确保它在多模块构建中是唯一的)。如果你确实需要更改输出文件名(或模块名称),通过 moduleName 属性来实现要容易得多!

resourceDirs

包含资源的目录。

scopes

此 map 的键是 IDEA scope。每个键指向另一个 map,该 map 有两个键:plus 和 minus。这些键的值是 Configuration 对象的集合。plus configuration 的文件会被添加,减去 minus configuration 的文件。请参阅下面的示例...

sourceDirs

包含生产源代码的目录。例如,请参阅 IdeaModule 的文档

targetBytecodeVersion

此模块使用的特定模块字节码版本。当为 null 时,模块将继承 IDEA 项目的字节码版本。

testOutputDir

测试类文件的输出目录。如果为 null,则不会创建条目。

testResourceDirs
已弃用

包含测试资源的目录。

testResources

测试资源目录的完整和最新集合。应优先使用此项而不是 IdeaModule.getTestResourceDirs(),因为它将包含对默认目录的最新更改。

testSourceDirs
已弃用

包含测试源代码的目录。请注意,对默认测试目录的后期更改可能不会在此集合中反映出来,应优先使用 IdeaModule.getTestSources()例如,请参阅 IdeaModule 的文档 此字段已 @Deprecated,请改用 IdeaModule.getTestSources()

testSources

测试源代码目录的完整和最新集合。应优先使用此项而不是 IdeaModule.getTestSourceDirs(),因为它将包含对默认目录的最新更改。

方法

方法描述
iml(action)

启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。

脚本块

描述
iml

启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。

属性详情

File contentRoot

模块的内容根目录。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
project.projectDir

boolean downloadJavadoc

是否下载并添加与依赖 jar 关联的 javadoc。默认为 false。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
false

boolean downloadSources

是否下载并添加与依赖 jar 关联的 sources。默认为 true。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
true

Set<File> excludeDirs

需要排除的目录。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
[project.layout.buildDirectory, project.file('.gradle')]

Set<File> generatedSourceDirs

包含生成源代码(生产和测试源代码)的目录。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
[]

Boolean inheritOutputDirs

如果为 true,则此模块的输出目录将位于项目的输出目录下;否则,它们将被设置为由 IdeaModule.getOutputDir()IdeaModule.getTestOutputDir() 指定的目录。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
null
使用 ideajava 插件时的默认值
null

String jdkName

此模块使用的 JDK。如果为 null,则使用现有或默认 ipr XML 的值(继承)。如果设置为 inherited,则使用项目 SDK。否则,此模块将使用相应 Java 版本的 SDK。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
'inherited'

IdeaLanguageLevel languageLevel

此模块使用的特定模块语言级别。当为 null 时,模块将继承 IDEA 项目的语言级别。

IDEA 模块语言级别基于关联 Gradle 项目的 sourceCompatibility 设置。

使用 ideajava 插件时的默认值
project.sourceCompatibility

String name

配置模块名称,即 *.iml 文件的名称。

它是可选的,因为 task 应该会为你正确配置它。默认情况下,它会尝试使用 project.name 或在其前面加上 project.path 的一部分,以确保模块名称在多模块构建范围内是唯一的。模块名称的“唯一性”是正确导入 IDEA 的必要条件,task 会确保名称是唯一的。

始于 1.0-milestone-2

如果你的项目存在名称唯一性问题,建议始终从根目录运行 gradle idea,即针对所有子项目运行。如果你只针对单个子项目生成 IDEA 模块,结果可能会有所不同,因为唯一名称是根据特定构建运行中涉及的 IDEA 模块计算的。

如果你更新了模块名称,请务必从根目录运行 gradle idea,例如针对所有子项目,包括生成 IDEA 项目。原因可能是存在依赖于已修改模块名称的子项目。因此你需要同时生成它们,因为模块依赖需要引用已修改的项目名称。基本上,对于非简单项目,建议始终从根目录运行 gradle idea

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
${project.name} (有时以 ${project.path} 的部分内容作为前缀以保证唯一性)

File outputDir

生产类文件的输出目录。如果为 null,则不会创建条目。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
null
使用 ideajava 插件时的默认值
null

File outputFile

配置输出 *.iml 文件。它是可选的,因为 task 应该会为你正确配置它(包括确保它在多模块构建中是唯一的)。如果你确实需要更改输出文件名(或模块名称),通过 moduleName 属性来实现要容易得多!

请参阅 moduleName 属性的文档。在 IntelliJ IDEA 中,模块名称与 *.iml 文件的名称相同。

使用 idea 插件时的默认值
#name + '.iml'

Set<File> resourceDirs

包含资源的目录。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
来自 project.sourceSets.main.resources 的资源目录

此 map 的键是 IDEA scope。每个键指向另一个 map,该 map 有两个键:plus 和 minus。这些键的值是 Configuration 对象的集合。plus configuration 的文件会被添加,减去 minus configuration 的文件。请参阅下面的示例...

如何使用 scopes 属性在输出的 *.iml 文件中启用 'performanceTestCompile' 依赖的示例

plugins {
    id 'java'
    id 'idea'
}

configurations {
  performanceTestCompile
  performanceTestCompile.extendsFrom(testCompile)
}

dependencies {
  //performanceTestCompile "some.interesting:dependency:1.0"
}

idea {
  module {
    scopes.TEST.plus += [ configurations.performanceTestCompile ]
  }
}
使用 idea 插件时的默认值
[:]
使用 ideajava 插件时的默认值
  • COMPILE -> project.configurations.compileClasspath
  • RUNTIME -> project.configurations.runtimeClasspath - project.configurations.compileClasspath
  • TEST -> project.configurations.testRuntimeClasspath - project.configurations.runtimeClasspath
  • PROVIDED

Set<File> sourceDirs

包含生产源代码的目录。例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
project.sourceSets.main.allJava

JavaVersion targetBytecodeVersion

此模块使用的特定模块字节码版本。当为 null 时,模块将继承 IDEA 项目的字节码版本。

IDEA 模块字节码版本基于关联 Gradle 项目的 targetCompatibility 设置。

使用 ideajava 插件时的默认值
project.targetCompatibility

File testOutputDir

测试类文件的输出目录。如果为 null,则不会创建条目。

例如,请参阅 IdeaModule 的文档

使用 idea 插件时的默认值
null
使用 ideajava 插件时的默认值
null

Set<File> testResourceDirs

注意:此属性已弃用,并将在 Gradle 的下一个主要版本中移除。

包含测试资源的目录。

例如,请参阅 IdeaModule 的文档 此字段已 @Deprecated,请改用 IdeaModule.getTestResources()

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
来自 project.sourceSets.test.resources 的资源目录

ConfigurableFileCollection testResources (只读)

测试资源目录的完整和最新集合。应优先使用此项而不是 IdeaModule.getTestResourceDirs(),因为它将包含对默认目录的最新更改。

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
基于可用测试套件中的资源

Set<File> testSourceDirs

注意:此属性已弃用,并将在 Gradle 的下一个主要版本中移除。

包含测试源代码的目录。请注意,对默认测试目录的后期更改可能不会在此集合中反映出来,应优先使用 IdeaModule.getTestSources()例如,请参阅 IdeaModule 的文档 此字段已 @Deprecated,请改用 IdeaModule.getTestSources()

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
project.sourceSets.test.allJava

ConfigurableFileCollection testSources (只读)

测试源代码目录的完整和最新集合。应优先使用此项而不是 IdeaModule.getTestSourceDirs(),因为它将包含对默认目录的最新更改。

使用 idea 插件时的默认值
[]
使用 ideajava 插件时的默认值
基于可用测试套件中的源代码目录

方法详情

void iml(Action<? super IdeaModuleIml> action)

启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。

例如,请参阅 IdeaModule 的文档。

脚本块详情

iml { }

启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。

例如,请参阅 IdeaModule 的文档。

委托给
IdeaModuleIml 来自 iml