Launching Gradle builds from Eclipse

Solution 1:

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/

Installation instructions can be found here: 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.

Solution 2:

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

From Eclipse, use pulldown menu 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:

  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"

Add to Arguments: the switch "-b filename.gradle" if you use a Gradle build file other than "build.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.

Solution 3:

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

I believe this site outlines 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?

Solution 4:

The current answer to this question should be, Use Gradle's own Eclipse plugin 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).

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.

Solution 5:

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:

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!

<?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>