Gradle + Intellij想法:运行配置

时间:2016-04-29 21:11:34

标签: java intellij-idea gradle

我有gradle项目,有多个复杂的JavaExec任务,为了方便,我想要相应的构思运行配置。

是否可以配置构思运行配置?想法插件或其他什么?

1 个答案:

答案 0 :(得分:10)

为此做了一个自定义任务:

// create IDEA run configurations from Gradle JavaExec tasks
task createRunConfigurations {
    def runConfigurationsDir = new File(".idea/runConfigurations")
    runConfigurationsDir.mkdirs()

    tasks.withType(JavaExec).each { task ->
        def taskName = task.name
        def mainClass = task.main
        def props = task.systemProperties.collect { k, v -> "-D$k=$v" }.join(' ')
        def args = task.args.join(" ")

        def writer = new FileWriter(new File(runConfigurationsDir, "${taskName}.xml"))
        def xml = new MarkupBuilder(writer)

        xml.component(name: "ProjectRunConfigurationManager") {
            configuration(default: 'false', name: taskName, type: "Application", factoryName: "Application", singleton: "true") {
                option(name: 'MAIN_CLASS_NAME', value: mainClass)
                option(name: 'VM_PARAMETERS', value: props)
                option(name: 'PROGRAM_PARAMETERS', value: args)
                option(name: 'WORKING_DIRECTORY', value: 'file://$PROJECT_DIR$')
                module(name: 'module-name')
            }
        }
    }
}