Gradle println在未被调用时打印 [英] Gradle println prints when it is not called

查看:131
本文介绍了Gradle println在未被调用时打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是每当 idea> 任务运行时都向控制台发送一条消息,但不幸的是,只要运行任何东西,就会打印消息。为什么当 idea> 任务没有运行时,打印行会执行?只有当 idea> 任务被执行时,我如何显示消息?

build.gradle

  apply plugin:'idea'

任务hello<< {
println'Hello world!'
}

tasks.idea(){
println'************** *******************************************'
println' *您应该从IntelliJ中的*'
println'*打开build.gradle作为本地项目。 *'
println'****************************************** ***************'
}

输出命令 gradle hello

  ******* ************************************************** 
*您应该从IntelliJ中的*
*开始将build.gradle作为本地项目打开。 *
********************************************* ************
:hello
Hello world!

建立成功

总时间:2.846秒

工作解决方案

  tasks.getByPath('idea')<< {
println'******************************************* **************'
println'*您应该从IntelliJ中的*'
println'*打开build.gradle作为本地项目。 *'
println'****************************************** ***************'
}


解决方案

您需要将 println s放入一个动作中并将其添加到主意中任务。以下示例显示了使用 doFirst 操作:

  ideaProject。 doFirst {
println'****************************************** ***************'
println'*您应该从IntelliJ中的*'
println'*开始将build.gradle作为本地项目打开。 *'
println'****************************************** ***************'
}

在运行 idea 任务之前,您的代码被执行的具体原因是:它被视为在 Gradle的构建生命周期。在执行阶段只执行动作。你的 hello 任务就是这样。



编辑: idea 是 org.gradle.plugins.ide.idea.model.IdeaModel 而不是任务。


My goal is to have a message printed to the console whenever the idea task is run, but unfortunately the message is printed whenever anything is run. Why are the print lines executing when the idea task is not being run? How can I display a message only when the idea task is executed?

build.gradle

apply plugin: 'idea'

task hello << {
    println 'Hello world!'
}

tasks.idea() {
    println '*********************************************************'
    println '* You should open build.gradle as a native project from *'
    println '* within IntelliJ.                                      *'
    println '*********************************************************'
}

Output of the command gradle hello

*********************************************************
* You should open build.gradle as a native project from *
* within IntelliJ.                                      *
*********************************************************
:hello
Hello world!

BUILD SUCCESSFUL

Total time: 2.846 secs

Working Solution

tasks.getByPath('idea') << {
    println '*********************************************************'
    println '* You should open build.gradle as a native project from *'
    println '* within IntelliJ.                                      *'
    println '*********************************************************'
}

解决方案

You will need to put your printlns into an action and add it to the idea task. The following example shows the use of the doFirst action:

ideaProject.doFirst {
    println '*********************************************************'
    println '* You should open build.gradle as a native project from *'
    println '* within IntelliJ.                                      *'
    println '*********************************************************'
}

There's a specific reason your code is executed before the idea task is run: It's evaluated as configuration code executed during the configuration phase of Gradle's build lifecycle. Only actions are executed during the execution phase. Your hello task does that.

EDIT: idea is org.gradle.plugins.ide.idea.model.IdeaModel in this context and not the task.

这篇关于Gradle println在未被调用时打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆