Gradle Task命令失败,但可以手动运行

时间:2019-01-23 17:58:21

标签: mongodb gradle

我的 build.gradle 文件中有以下任务:

task init_dev {
    doLast {
        exec {
            workingDir '.'
            commandLine 'mongo', '--port', '27018', '<', 'init/mongodb-init-dev'
        }
    }
}

当我执行它时,我得到以下内容

14:52:49: Executing task 'init_dev --info'...

The client will now receive all logging from the daemon (pid: 3919). The daemon log file: /Users/jscherman/.gradle/daemon/4.8.1/daemon-3919.out.log
Starting 42nd build in daemon [uptime: 49 mins 28.148 secs, performance: 99%, no major garbage collections]
Using 8 worker leases.
Starting Build
Settings evaluated using settings file '/Users/jscherman/IdeaProjects/demo/settings.gradle'.
Projects loaded. Root project using build file '/Users/jscherman/IdeaProjects/demo/build.gradle'.
Included projects: [root project 'demo', project ':mock-metrics-generator', project ':demo-be']

> Configure project :
Evaluating root project 'demo' using build file '/Users/jscherman/IdeaProjects/demo/build.gradle'.
Applying dependency management to configuration 'annotationProcessor' in project 'mock-metrics-generator'
Applying dependency management to configuration 'apiElements' in project 'mock-metrics-generator'
Applying dependency management to configuration 'archives' in project 'mock-metrics-generator'
Applying dependency management to configuration 'bootArchives' in project 'mock-metrics-generator'
Applying dependency management to configuration 'compile' in project 'mock-metrics-generator'
Applying dependency management to configuration 'compileClasspath' in project 'mock-metrics-generator'
Applying dependency management to configuration 'compileOnly' in project 'mock-metrics-generator'
Applying dependency management to configuration 'default' in project 'mock-metrics-generator'
Applying dependency management to configuration 'implementation' in project 'mock-metrics-generator'
Applying dependency management to configuration 'runtime' in project 'mock-metrics-generator'
Applying dependency management to configuration 'runtimeClasspath' in project 'mock-metrics-generator'
Applying dependency management to configuration 'runtimeElements' in project 'mock-metrics-generator'
Applying dependency management to configuration 'runtimeOnly' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testAnnotationProcessor' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testCompile' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testCompileClasspath' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testCompileOnly' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testImplementation' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testRuntime' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testRuntimeClasspath' in project 'mock-metrics-generator'
Applying dependency management to configuration 'testRuntimeOnly' in project 'mock-metrics-generator'
Applying dependency management to configuration 'annotationProcessor' in project 'demo-be'
Applying dependency management to configuration 'apiElements' in project 'demo-be'
Applying dependency management to configuration 'archives' in project 'demo-be'
Applying dependency management to configuration 'bootArchives' in project 'demo-be'
Applying dependency management to configuration 'compile' in project 'demo-be'
Applying dependency management to configuration 'compileClasspath' in project 'demo-be'
Applying dependency management to configuration 'compileOnly' in project 'demo-be'
Applying dependency management to configuration 'default' in project 'demo-be'
Applying dependency management to configuration 'implementation' in project 'demo-be'
Applying dependency management to configuration 'runtime' in project 'demo-be'
Applying dependency management to configuration 'runtimeClasspath' in project 'demo-be'
Applying dependency management to configuration 'runtimeElements' in project 'demo-be'
Applying dependency management to configuration 'runtimeOnly' in project 'demo-be'
Applying dependency management to configuration 'testAnnotationProcessor' in project 'demo-be'
Applying dependency management to configuration 'testCompile' in project 'demo-be'
Applying dependency management to configuration 'testCompileClasspath' in project 'demo-be'
Applying dependency management to configuration 'testCompileOnly' in project 'demo-be'
Applying dependency management to configuration 'testImplementation' in project 'demo-be'
Applying dependency management to configuration 'testRuntime' in project 'demo-be'
Applying dependency management to configuration 'testRuntimeClasspath' in project 'demo-be'
Applying dependency management to configuration 'testRuntimeOnly' in project 'demo-be'

> Configure project :mock-metrics-generator
Evaluating project ':mock-metrics-generator' using build file '/Users/jscherman/IdeaProjects/demo/mock-metrics-generator/build.gradle'.

> Configure project :demo-be
Evaluating project ':demo-be' using build file '/Users/jscherman/IdeaProjects/demo/demo-be/build.gradle'.
All projects evaluated.
Selected primary task 'init_dev' from project :
Tasks to be executed: [task ':init_dev']
:init_dev (Thread[Task worker for ':',5,main]) started.

> Task :init_dev FAILED
Task ':init_dev' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Starting process 'command 'mongo''. Working directory: /Users/jscherman/IdeaProjects/demo Command: mongo --port 27018 < init/mongodb-init-dev
Successfully started process 'command 'mongo''
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27018/%3C
Implicit session: session { "id" : UUID("0b3d098a-00e3-4802-bf9f-af7d1f7a6629") }
MongoDB server version: 4.0.3
2019-01-23T14:52:49.741-0300 E QUERY    [js] SyntaxError: missing ; before statement @init/mongodb-init-dev:1:4
failed to load: init/mongodb-init-dev
:init_dev (Thread[Task worker for ':',5,main]) completed. Took 0.217 secs.

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/jscherman/IdeaProjects/demo/build.gradle' line: 60

* What went wrong:
Execution failed for task ':init_dev'.
> Process 'command 'mongo'' finished with non-zero exit value 253

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
1 actionable task: 1 executed
Process 'command 'mongo'' finished with non-zero exit value 253
Open File
14:52:49: Task execution finished 'init_dev --info'.

mongodb-init-dev

use my_db

db.nodes.drop()

db.nodes.insertMany([
    {
        "_id":"node-1",
        "classifier":{
            "lowerThreshold":3000,
            "upperThreshold":5000,
            "timeUnit":"MILLISECONDS"
        }
    },
    {
        "_id":"node-2",
        "classifier":{
            "lowerThreshold":2,
            "upperThreshold":4,
            "timeUnit":"SECONDS"
        }
    },
    {
        "_id":"node-3",
        "classifier":{
            "lowerThreshold":3000,
            "upperThreshold":5000,
            "timeUnit":"MILLISECONDS"
        }
    },
    {
        "_id":"node-4",
        "classifier":{
            "lowerThreshold":1200,
            "upperThreshold":4000,
            "timeUnit":"MILLISECONDS"
        }
    },
    {
        "_id":"node-5",
        "classifier":{
            "lowerThreshold":2000,
            "upperThreshold":5000,
            "timeUnit":"MILLISECONDS"
        }
    }])

现在,当我手动运行该命令时,它会起作用

madmin’s-MacBook-Pro:demo jscherman$ pwd
/Users/jscherman/IdeaProjects/demo
madmin’s-MacBook-Pro:demo jscherman$ mongo --port 27018 < init/mongodb-init-dev
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27018/
Implicit session: session { "id" : UUID("31e9f04c-4a7b-474d-ac18-93f0fb725c86") }
MongoDB server version: 4.0.3
switched to db demo_db
true
{
    "acknowledged" : true,
    "insertedIds" : [
        "node-1",
        "node-2",
        "node-3",
        "node-4",
        "node-5"
    ]
}
bye

知道为什么会这样吗?预先感谢。

2 个答案:

答案 0 :(得分:0)

use my_db不是有效的JavaScript语法。从mongodb-init-dev脚本中删除use my_db,然后在 build.gradle 中添加'my_db'并删除'<'如下:

commandLine 'mongo', '--port', '27018', 'my_db', 'init/mongodb-init-dev'

顺便说一句,您可以看到更多消息--info,即gradle --info init_dev,以查看其他信息。

答案 1 :(得分:0)

这是因为未配置输出。 Gradle利用任务的输入和输出来确定任务是否需要执行。这是为了检查任务是否为最新版本。

在此处阅读更多内容。 https://docs.gradle.org/5.1/userguide/more_about_tasks.html

要解决问题,请按以下步骤更新任务:

task init_dev {
doLast {
    exec {
        workingDir '.'
        commandLine 'mongo', '--port', '27018', '<', 'init/mongodb-init-dev'

        ext.output = {
            return standardOutput.toString()
        }
    }
}

}