【问题标题】:set variable from task only when task is called仅在调用任务时从任务中设置变量
【发布时间】:2015-03-30 11:18:50
【问题描述】:

我定义了一个名为 debug 的任务,它应该设置应用程序插件的 applicationDefaultJvmArgs,以便 spring-boot 停止并等待调试器。

我想将此任务与我定义的另一个名为 local 的任务结合使用,该任务设置一个 spring.profiles.active 变量。

apply plugin: 'spring-boot'
apply plugin: 'application'
apply plugin: 'org.flywaydb.flyway'

dependencies {
    compile group: "org.apache.camel", name: "camel-netty4-http", version: camelVersion
    compile group: "org.apache.camel", name: "camel-spring-boot", version: camelVersion
    compile group: "org.springframework.boot", name: "spring-boot-starter", version: "1.2.1.RELEASE"
}

flyway {
    url = 'jdbc:mysql://localhost:3306/foobar'
    user = 'foo'
    password = 'bar'
}

task local {
    tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
        systemProperty('spring.profiles.active', 'local')
    }
}

task debug << {
    project.ext {
        applicationDefaultJvmArgs = [
                "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
        ]
    }
}

当我使用gradle local debug 调用 gradle 调试时,applicationDefaultJvmArgs 已设置,但当我调用 gralde local 时,它们也已设置?

我试过了

task debug << { ... 

但这不会有什么不同。

【问题讨论】:

    标签: gradle spring-boot


    【解决方案1】:

    applicationDefaultJvmArgs 在哪里定义?你能提供你的构建脚本的完整代码吗? 我相信下一段代码应该可以完成这项工作:

    task debug << {
        project.ext {
            applicationDefaultJvmArgs = [
                "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"    
            ]
        }
    }
    

    变化:

    1. 分配 applicationDefaultJvmArgs 作为调试任务操作的一部分(而不是配置)
    2. 将 applicationDefaultJvmArgs 定义为项目属性

    【讨论】:

    • applicationDefaultJvmArgs 在应用程序插件中定义:gradle.org/docs/current/userguide/application_plugin.html 建议的解决方案不起作用。我还更新了给出的代码示例。
    • 奇怪,因为我用一个不依赖 spring-boot 和 flyway 插件的简单项目验证了建议的解决方案。只是为了验证您的假设,您是否可以添加以下任务 task printJvmArgs &lt;&lt; { println applicationDefaultJvmArgs } 并为这两种情况添加一个调用(即先调用 gradle local debug printJvmArgs 然后 gradle local printJvmArgs)?
    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多