如何在 gradle.build 中调试空指针异常

how to debug a null pointer exception in gradle.build

我有这个 gradle.build groovy 文件:

task BL_generate_parallel_warmup(type: JavaExec) {
     if (project.hasProperty('serverversion')) {
        args(serverversion)
    }
    if (project.hasProperty('input_flavor')) {
        systemProperties['input_flavor'] = input_flavor
        print "gradle input_flavor" + input_flavor
    }
    jvmArgs = ["-Xms1024m", "-Xmx1024m"]
    classpath sourceSets.main.runtimeClasspath
    dependsOn resources_cleaner_bl
    systemProperties['isDummyRun'] = 'true'
    main = "astar.BlParallelGenerator"
}

在我重构之后:

def setSystemProperties() {
    if (project.hasProperty('serverversion')) {
        args(serverversion)
    }
    if (project.hasProperty('input_flavor')) {
        systemProperties['input_flavor'] = input_flavor
        print "gradle input_flavor" + input_flavor
    }
    jvmArgs = ["-Xms1024m", "-Xmx1024m"]
    classpath sourceSets.main.runtimeClasspath
}



//warm up

task BL_generate_parallel_warmup(type: JavaExec) {
    setSystemProperties()
    dependsOn resources_cleaner_bl
    systemProperties['isDummyRun'] = 'true'
    main = "astar.BlParallelGenerator"
}

我收到这个错误:

Error:(121, 0) A problem occurred evaluating root project 'MyProject'.
<a href="openFile">Open File</a>

更新

我通过更改解决了这个问题:

def setSystemProperties(project) {
        if (project.hasProperty('serverversion')) {
            args(serverversion)
        }
        if (project.hasProperty('input_flavor')) {
            systemProperties['input_flavor'] = input_flavor
            print "gradle input_flavor" + input_flavor
        }
        jvmArgs = ["-Xms1024m", "-Xmx1024m"]
        classpath sourceSets.main.runtimeClasspath
    }



    //warm up

    task BL_generate_parallel_warmup(type: JavaExec) {
        setSystemProperties(project)
        dependsOn resources_cleaner_bl
        systemProperties['isDummyRun'] = 'true'
        main = "astar.BlParallelGenerator"
    }

如何在 intellij 中调试它?

我在调试中按下 运行 并在 gradle 构建中添加了一个断点,但它并没有在任何地方停止。

我试过 "edit configuration" 这样的:

但它并没有使代码在断点处停止

Gradle调试参考文档可以在这里找到:https://www.jetbrains.com/idea/help/create-run-debug-configuration-for-gradle-tasks.html

要为 Gradle 任务创建调试配置,请在 Gradle 工具中右键单击该任务 window 并 select 创建。您将获得有关如何配置调试配置的选项。

由于您正在使用 JavaExec 类型的任务,因此还有另一种选择。 您可以在 main class 中放置一个断点,并在 IntelliJ 中配置远程调试会话。

如图所示,我已经创建了一个远程类型的调试配置,它正在侦听端口 5005。 然后在您的 JavaExec 类型任务中,您将添加 debug=true 选项,如下所示:

task runApp(type: JavaExec){
    classpath  file('c:/data/test')
    main = 'TestMain'
    debug = true
}

要调试它,您可以在 IntelliJ 的 Gradle 插件中单击 runApp 任务,然后启动 Local_Port_5005 远程调试会话并让任务在您的断点处停止.