Search code examples
javaeclipsegradle-eclipse

Create Eclipse Java Gradle Project


I would like to use Eclipse with Gradle to build POJO's. I installed the Eclipse STS plugin as highlighted in the Gradle tutorial. I create Gradle project with Eclipse using File > New > Gradle(STS) > Gradle(STS) Project then type in the project name and select Java Quickstart. It sets up the project correctly but includes an annoying "org.gradle" Package and Person.java classes throughout the main java/resources and test java/resources. I go in and delete them and create my own com.xxx package and classes. Anyway that I an suppress the creation of these files?


Solution

  • I investigated several methods highlighted by Gradle's Tutorial website and a few other places and found a better way of setting up a Java project utilizing Gradle. Here is what I did.

    1. Using Eclipse, create a Java Project. file > New > Java Project -- Add Project Name & click finish.

    2. Delete the src file and create a new file hierarchy file > New > Source Folder -- Type src/main/java file > New > Package -- com.mycompany.

      Do the same for Resources and Test if required src/main/resources + Package src/test/Java + Package

    3. Add/create your classes and Jars to the project.

    4. Create build.gradle file in project root. Add this simple Gradle project build script,

      apply plugin: 'java' apply plugin: 'application'

      mainClassName = 'com.mycompany.HelloWorld'

      repositories { mavenCentral() }

      jar { baseName = 'myjarname' version = 'v1' manifest { attributes 'Main-Class': 'com.mycompany.myproject' } doFirst { from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } } } }

      sourceCompatibility = 1.8 targetCompatibility = 1.8

      dependencies { compile 'joda-time:joda-time:2.2' testCompile 'junit:junit:4.12' }

    You can get the dependency scripts from Maven Central (search.maven.org). Note that this gradle script will allow you to build a Fat Jar -- a project jar with multiple dependency jars.

    1. Next create the Gradle wrapper. Open a command prompt and go to the project root directory. Type -- gradle wrapper. All of the correct wrapper files are now created for your project.

    2. While in the project root type -- gradlew build. This create other files and builds the project. You can go to project root\build\libs and you will find your executable Jar.

    3. As you make changes to your project from now on you simply go to the project root in command prompt and type -- gradle build.

    I would call this a starter gradle build for newbies. However, it will get you going and as you need more gradle capability you can modify the build.gradle. Also, don't forget, as you add new jar libraries you will need to update the build.gradle dependencies. This process also works with Eclipse so you can directly build the project from eclipse and run it in the console. In this example I was using Gradle 3.0 and Java SE 1.8.