从 Eclipse 启动 Gradle 构建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6442619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 20:15:37  来源:igfitidea点击:

Launching Gradle builds from Eclipse

eclipsegradle

提问by csl

We're looking at converting our Ant build to Gradle.

我们正在考虑将我们的 Ant 构建转换为 Gradle。

In regards to integration with Eclipse, we are looking for (conceptually) equivalent functionality for launching builds from the IDE. Eclipse provides a nice Ant view for invoking Ant targets.

关于与 Eclipse 的集成,我们正在寻找(概念上)等效的功能来从 IDE 启动构建。Eclipse 为调用 Ant 目标提供了一个很好的 Ant 视图。

There is an Eclipse plugin for launching Gradle builds (http://www.breskeby.com/downloads/gradle/eclipse/update/), but this seems to require users to jump through hoops to invoke different targets (edit the launch configuration, etc.).

有一个用于启动 Gradle 构建的 Eclipse 插件(http://www.breskeby.com/downloads/gradle/eclipse/update/),但这似乎需要用户跳过箍来调用不同的目标(编辑启动配置,等等。)。

How are others using Gradle from inside Eclipse?

其他人如何从 Eclipse 内部使用 Gradle?

采纳答案by c_maker

The short answer is that we do not invoke gradle scripts from Eclipse.

简短的回答是我们不从 Eclipse 调用 gradle 脚本。

I believe this siteoutlines where this work is at the moment and it does not seem to be much right now.

我相信这个网站概述了这项工作目前的位置,现在似乎并不多。

I am a bit curious what kind of tasks you want to run from Eclipse. Why not run tasks from the command line?

我有点好奇你想从 Eclipse 运行什么样的任务。为什么不从命令行运行任务?

回答by Keith P

This is kind of an old post, but the SpringSource STS team has released a plugin: http://static.springsource.org/sts/docs/latest/reference/html/gradle/

这是一篇旧帖子,但 SpringSource STS 团队发布了一个插件:http: //static.springsource.org/sts/docs/latest/reference/html/gradle/

Installation instructions can be found here: http://static.springsource.org/sts/docs/latest/reference/html/gradle/installation.html

安装说明可以在这里找到:http: //static.springsource.org/sts/docs/latest/reference/html/gradle/installation.html

My experience has been pretty positive with it so far. For straight-up Java projects it works quite well. I am having some issues generating correct war files through Eclipse, while using the Gradle plugin, but gradle itself does it wonderfully. I am relatively new to gradle and the plugin though, so it could be something that I am missing.

到目前为止,我的经验非常积极。对于直接的 Java 项目,它工作得很好。在使用 Gradle 插件时,我在通过 Eclipse 生成正确的War文件时遇到了一些问题,但 gradle 本身做得很好。我对 gradle 和插件比较陌生,所以它可能是我遗漏的东西。

回答by Blaine

Interim work-around more palatable to those with command-line phobia.

对于那些有命令行恐惧症的人来说,临时解决办法更合适。

From Eclipse, use pulldown menu Run / External Tools / External Tools Configurations...

在 Eclipse 中,使用下拉菜单 Run / External Tools / External Tools Configurations...

Select "Program" in left navigator panel then click the "New launch configuration" button in the tool bar (first button in my tool bar).

在左侧导航器面板中选择“程序”,然后单击工具栏中的“新启动配置”按钮(我工具栏中的第一个按钮)。

With Main tab selected, fill out the following fields:

选择 Main 选项卡后,填写以下字段:

  1. Name: (above the tabs) to "Gradle" (or whatever name you want for the launcher).
  2. Location: Use "Browse File System..." button to navigate to your "gradle.bat" or "gradlew.bat" to run.
  3. Working Directory: Use "Browse Workspace..." button to select directory with the "build.gradle" file for the desired project.
  4. Arguments: Enter "--gui"
  1. 名称:(在选项卡上方)到“Gradle”(或任何您想要的启动器名称)。
  2. 位置:使用“浏览文件系统...”按钮导航到您的“gradle.bat”或“gradlew.bat”以运行。
  3. 工作目录:使用“浏览工作区...”按钮为所需项目选择包含“build.gradle”文件的目录。
  4. 参数:输入“--gui”

Add to Arguments: the switch "-b filename.gradle" if you use a Gradle build file other than "build.gradle".

添加到参数:如果您使用“build.gradle”以外的 Gradle 构建文件,则使用开关“-b filename.gradle”。

After this, your developers can use the Run / External Tools or tool bar button to launch the Gradle Gui. They can do this and close it after each use, or (to avoid startup lag), minimize it when not in use.

在此之后,您的开发人员可以使用运行/外部工具或工具栏按钮来启动 Gradle Gui。他们可以这样做并在每次使用后关闭它,或者(为了避免启动延迟),在不使用时将其最小化。

回答by FkYkko

The current answer to this question should be, Use Gradle's own Eclipse plugin Buildship:

这个问题的当前答案应该是,使用 Gradle 自己的 Eclipse 插件Buildship

http://projects.eclipse.org/projects/tools.buildship

http://projects.eclipse.org/projects/tools.buildship

This is supported and developed by the Gradle team and will be continuously improved in the future. It can also handle older versions (with functionality based on Gradle version availability).

这是由 Gradle 团队支持和开发的,将来会不断改进。它还可以处理旧版本(具有基于 Gradle 版本可用性的功能)。

Edit 2017-03-01: After the release of Buildship 2.0, the integration is even better and I recommend it (unless your already using IntelliJ, then your already set). There are still some features missing like the ability to directly debug JavaExec tasks and the ability to run precompile/preimport tasks, but those are in their Github issues list.

编辑 2017-03-01:在 Buildship 2.0 发布后,集成更好,我推荐它(除非你已经在使用 IntelliJ,那么你已经设置了)。仍然缺少一些功能,例如直接调试 JavaExec 任务的能力和运行预编译/预导入任务的能力,但这些都在他们的Github 问题列表中

回答by JoeG

I know this is an old question, but I still do not think it is possible to have Eclipse run a gradle build for you. The Spring Gradle plugin is a great start, if you use that, you can define an external tool builder to run gradle when you want. If you have lots of projects and all are being built with gradle, you can even have gradle add the capability to your eclipse projects for you. While this can be cleaned up, you can add something like this to your gradle build file:

我知道这是一个老问题,但我仍然认为不可能让 Eclipse 为您运行 gradle 构建。Spring Gradle 插件是一个很好的开始,如果您使用它,您可以定义一个外部工具构建器来在需要时运行 gradle。如果你有很多项目并且都是用 gradle 构建的,你甚至可以让 gradle 为你的 eclipse 项目添加功能。虽然这可以清除,但您可以将这样的内容添加到您的 gradle 构建文件中:

apply plugin: 'eclipse'

eclipse {
    project {
        // Store a copy of the desired Gradle_Builder.launch file in a top-level 'master'
        //    directory.  Then this code searches for it, and by copying it, 
        //    adds the launch file to the specifc project that will run gradle
        String launchFileNameOrig = '.externalToolBuilders/Gradle_Builder.launch'
        String launchFileName = launchFileNameOrig
        File launchFile = file(launchFileName)
        boolean needToCopy = false
        while (!launchFile.exists()) {
            launchFileName = '../' + launchFileName
            launchFile = file(launchFileName)
            needToCopy = true
        }
        if (needToCopy) {
             copy {
                from (launchFile)
                into '.externalToolBuilders'
            }
        }

        buildCommand 'org.eclipse.ui.externaltools.ExternalToolBuilder', LaunchConfigHandle: '<project>/'+launchFileNameOrig
        file {
            // when they made the "buildCommand" it looks like they left off 'triggers', so parse the XML until
            // the right place is found, then insert it.
            withXml {
                def projectNode = it.asNode()
                projectNode.iterator().each { subNode ->
                    String subNodeText = '' + subNode
                    if (subNodeText.startsWith('buildSpec')) {
                        subNode.iterator().each { buildCmd ->
                            String nameNode = buildCmd?.name
                            if (nameNode.contains('ExternalToolBuilder')) {
                                buildCmd.appendNode('triggers', 'full')
                            }
                        }
                    }
                }
            }
        }
    }

This is the content of the file stored at the top of the directory hierarchy under: ./.externalToolBuilders/Gradle_Builder.launch. As defined here, this will only run after a "clean" [Gradle is more expensive time-wise than the native Java Builder, so continue to use that for auto-building]. Note: the file contents below also assumes that you are using "git" and the gradle wrapper. You see this on the ATTR_LOCATION value. Adjust as needed. One nice thing about this approach though is that you can have the gradle wrapper be any version of gradle you want, and then eclipse will use that version when it runs!

这是存储在目录层次结构顶部的文件内容:./.externalToolBuilders/Gradle_Builder.launch。如此处所定义,这只会在“干净”之后运行 [Gradle 在时间方面比原生 Java Builder 更昂贵,因此请继续使用它进行自动构建]。注意:下面的文件内容还假设您正在使用“git”和 gradle 包装器。您可以在 ATTR_LOCATION 值上看到这一点。根据需要进行调整。不过,这种方法的一个好处是,您可以让 gradle 包装器成为您想要的任何版本的 gradle,然后 eclipse 将在运行时使用该版本!

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${workspace}"/>
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${git_dir}/../gradlew"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_TOOL_ARGUMENTS" value="assemble"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>

回答by bhagyas

You can create a custom launcher from eclipse which would invoke your Gradle build.

您可以从 eclipse 创建一个自定义启动器,它会调用您的 Gradle 构建。

回答by opticyclic

As ever, you are better off with IDEA. Support has been around since 2009 http://blogs.jetbrains.com/idea/2009/08/gradle-support/

与以往一样,使用 IDEA 会更好。自 2009 年以来一直支持http://blogs.jetbrains.com/idea/2009/08/gradle-support/

and even fuller support is available now via the EAP (free to use!) http://blogs.jetbrains.com/idea/2011/09/keen-to-try-gradle-integration-in-intellij-idea/

现在可以通过 EAP 获得更全面的支持(免费使用!) http://blogs.jetbrains.com/idea/2011/09/keen-to-try-gradle-integration-in-intellij-idea/

回答by Bozhidar Batsov

You can use the Gradle Eclipse pluginlike gradle eclipseto generate an Eclipse project from the gradle project. It adds a lot of targets to the generated Eclipse projects and is highly customizable.

您可以使用摇篮Eclipse插件一样gradle eclipse,产生从gradle这个项目的Eclipse项目。它为生成的 Eclipse 项目添加了很多目标,并且是高度可定制的。

回答by DivDiff

You can use Buildshipas stated by FkYkko. However, one thing to keep in mind is that Ant tasks are considered first-class citizens in gradle. This means you can simply import an ant build script, referencing its tasks the same way you reference gradle tasks. Doing things this way allows you to simply use your ant tasks out of the box with gradle, without converting them to gradle tasks.

您可以按照 FkYkko 的说明使用 Buildship。但是,要记住的一件事是 Ant 任务在 gradle 中被认为是一等公民。这意味着您可以简单地导入 ant 构建脚本,以与引用 gradle 任务相同的方式引用其任务。通过这种方式,您可以简单地使用 gradle 开箱即用的 ant 任务,而无需将它们转换为 gradle 任务。

For example, say you have a build script named build.xml in the same directory as your build.gradle, and a task in build.xml named buildProj. You can import build.xml, and call build proj from build.gradle like this:

例如,假设您在 build.gradle 所在的目录中有一个名为 build.xml 的构建脚本,并且在 build.xml 中有一个名为 buildProj 的任务。您可以导入 build.xml,并像这样从 build.gradle 调用 build proj:

task antDeps {
    ant.importBuild 'build.xml'
}

task buildAll (dependsOn:['buildProj']) {
    // the rest of your gradle task here.
}

See this, or thisfor more info.

看到这个,或者这个了解更多信息。

回答by Philip Kirkbride

I came across this question after installing the newer Buildship plugin which is phasing out Spring Tool Suite mentioned by others here.

我在安装较新的 Buildship 插件后遇到了这个问题,该插件正在逐步淘汰其他人在这里提到的 Spring Tool Suite。

Instructions for using task view with Buildship are as follows:

在 Buildship 中使用任务视图的说明如下:

Using the Gradle Tasks view

After successfully importing a Gradle project, the project is shown in the Gradle Tasks view. enter image description hereBy right clicking on a certain Gradle task in the Gradle Tasks view, you can run the selected Gradle task.

enter image description here

By default the result is displayed in the Gradle Executions view. It is also displayed in the Console view similar to the output you would get if you run the task via the command line.

使用 Gradle 任务视图

成功导入 Gradle 项目后,该项目将显示在 Gradle 任务视图中。 在此处输入图片说明通过右键单击 Gradle 任务视图中的某个 Gradle 任务,您可以运行所选的 Gradle 任务。

在此处输入图片说明

默认情况下,结果显示在 Gradle Executions 视图中。它也显示在控制台视图中,类似于通过命令行运行任务时获得的输出。

Source

来源