How to Import Gradle Project into Eclipse

Gradle is a great configuration tool, but you obviously need to work inside your IDE too! The Gradle “eclipse” plugin supports easily importing your project into Eclipse. Here is an example:

Create a new folder called basic-eclipse.

Inside the folder, create folders src/main/java.

Create the Gradle build file in the basic-eclipse folder with the following contents:

apply plugin: 'java'
apply plugin: 'eclipse'

repositories {
    mavenCentral()
}

targetCompatibility = 1.6

dependencies {
    compile 'commons-lang:commons-lang:20030203.000129'
    testCompile 'junit:junit:4.+'
}

From the command line, run gradle eclipse

Now, go into Eclipse and choose File -> Import -> Existing Projects into Workspace and choose the basic-eclipse folder that you created.

import-eclipse-project Notice that src/main/java is automatically configured to be the source directory. Also, the Commons-Lang and Junit jars have been downloaded and added to our build path automatically (in addition to Hamcrest, a jar that Junit depends on).

Let’s create and run a simple Java file. Right-click (or ctrl-click if on Mac) the source directory, src/main/java, and choose “New -> Class”. Create a class called SampleMain in com.codetutr package with the following contents: package com.codetutr;

import org.apache.commons.lang.StringUtils;

public class SampleMain {
    public static void main(String[] args) {
        System.out.println(StringUtils.reverse("dlrow ,olleH"));
    }
} 

Run the class. You should see Hello, world output to your console. Notice that we have used StringUtils which is part of the Commons-Lang dependency that we included in our build.gradle file.

Full Source: basic-eclipse.zip
Unpack the archive. Open command prompt at unarchived root. Type gradle eclipse. Import into Eclipse. Run SampleMain.

Posted in Gradle Tagged with: ,
  • tvd27285

    thanks very much

  • samuelteixeiras

    thanks for share

  • http://itconsultant.kr Jaehyun Kim

    thank you very much ~^^

Sponsored

Follow Me

RSS FeedFollow me on Twitter