| 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 关联的源文件。默认为 true。 |
excludeDirs | 要排除的目录。 |
generatedSourceDirs | 包含生成的源文件(包括生产源文件和测试源文件)的目录。 |
iml | |
inheritOutputDirs | 如果为 true,则此模块的输出目录将位于项目输出目录下;否则,它们将设置为 |
jdkName | 此模块要使用的 JDK。如果为 |
languageLevel | 此模块要使用的模块特定语言级别。当为 |
name | 配置模块名称,即 *.iml 文件的名称。 |
outputDir | 生产类的输出目录。如果为 |
outputFile | 配置输出 *.iml 文件。它是可选的,因为任务应该为您正确配置它(包括确保它在多模块构建中是唯一的)。如果您确实需要更改输出文件名(或模块名),通过moduleName属性进行更改要容易得多! |
resourceDirs | 包含资源的目录。 |
scopes | 此映射的键是 IDEA 范围。每个键指向另一个映射,该映射有两个键,加和减。这些键的值是 |
sourceDirs | 包含生产源文件的目录。例如,请参阅 |
targetBytecodeVersion | 此模块要使用的模块特定字节码版本。当为 |
testOutputDir | 测试类的输出目录。如果为 |
testResources | 完整且最新的测试资源目录集合。 |
testSources | 完整且最新的测试源目录集合 |
| 方法 | 描述 |
iml(action) | 启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。 |
| 块 | 描述 |
iml | 启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。 |
IdeaModuleIml iml (只读)
Boolean inheritOutputDirs
如果为 true,则此模块的输出目录将位于项目输出目录下;否则,它们将设置为 IdeaModule.getOutputDir() 和 IdeaModule.getTestOutputDir() 指定的目录。
例如,请参阅 IdeaModule 的文档。
- 与
idea插件的默认值 null- 默认与
idea和java插件 null
String jdkName
此模块要使用的 JDK。如果为 null,则使用现有或默认 ipr XML(继承)的值。如果设置为 inherited,则使用项目 SDK。否则,此模块将使用 Java 版本对应值的 SDK。
例如,请参阅 IdeaModule 的文档。
- 与
idea插件的默认值 'inherited'
IdeaLanguageLevel languageLevel
此模块要使用的模块特定语言级别。当为 null 时,模块将继承 IDEA 项目的语言级别。
Idea 模块语言级别基于关联 Gradle 项目的 sourceCompatibility 设置。
- 默认与
idea和java插件 project.sourceCompatibility
String name
配置模块名称,即 *.iml 文件的名称。
它是可选的,因为任务应该为您正确配置它。默认情况下,它将尝试使用project.name或使用project.path的一部分作为前缀,以确保模块名称在多模块构建范围内是唯一的。模块名称的“唯一性”对于正确导入 IDEA 是必需的,任务将确保名称是唯一的。
自 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- 默认与
idea和java插件 null
File outputFile
配置输出 *.iml 文件。它是可选的,因为任务应该为您正确配置它(包括确保它在多模块构建中是唯一的)。如果您确实需要更改输出文件名(或模块名),通过moduleName属性进行更改要容易得多!
请参阅 moduleName 属性的文档。在 IntelliJ IDEA 中,模块名称与 *.iml 文件的名称相同。
- 与
idea插件的默认值 - #name +
'.iml'
包含资源的目录。
例如,请参阅 IdeaModule 的文档。
- 与
idea插件的默认值 []- 默认与
idea和java插件 - 来自
project.sourceSets.main.resources的资源目录
Map<String, Map<String, Collection<Configuration>>> scopes
Map<String, Map<String, Collection<Configuration>>>此映射的键是 IDEA 范围。每个键指向另一个映射,该映射有两个键,加和减。这些键的值是 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插件的默认值 [:]- 默认与
idea和java插件 -
COMPILE->project.configurations.compileClasspathRUNTIME->project.configurations.runtimeClasspath - project.configurations.compileClasspathTEST->project.configurations.testRuntimeClasspath - project.configurations.runtimeClasspathPROVIDED
包含生产源文件的目录。例如,请参阅 IdeaModule 的文档。
- 与
idea插件的默认值 []- 默认与
idea和java插件 - project.sourceSets.main.allJava
JavaVersion targetBytecodeVersion
此模块要使用的模块特定字节码版本。当为 null 时,模块将继承 IDEA 项目的字节码版本。
Idea 模块字节码版本基于关联 Gradle 项目的 targetCompatibility 设置。
- 默认与
idea和java插件 project.targetCompatibility
File testOutputDir
测试类的输出目录。如果为 null,则不会创建任何条目。
例如,请参阅 IdeaModule 的文档。
- 与
idea插件的默认值 null- 默认与
idea和java插件 null
ConfigurableFileCollection testResources (只读)
完整且最新的测试资源目录集合。
- 与
idea插件的默认值 []- 默认与
idea和java插件 - 基于可用测试套件中的资源
ConfigurableFileCollection testSources (只读)
完整且最新的测试源目录集合
- 与
idea插件的默认值 []- 默认与
idea和java插件 - 基于可用测试套件中的源目录
void iml(Action<? super IdeaModuleIml> action)
Action<? super IdeaModuleIml>启用高级配置,例如修改输出 XML 或影响现有 *.iml 内容与 gradle 构建信息合并的方式。
例如,请参阅 IdeaModule 的文档。