Intellij Idea Gradle project

This is the continuation fo the post Introduction to Gradle.

Sometime back, I wrote a blog How To: GitHub projects in Spring Tool Suite. At that time, my development environment is mostly Windows and Linux and favorite Java IDE was Eclipse becasue heavy use of Wizards. Later Intellij Idea came with better wizards. Not only that, the quality of the Idea deviated me from the Eclipse. My current environment is macOS. Most of the tools I install using homebrew: sublime, pipenv and so on. Another great installation tool is SDKMAN which I used to install the JDK, gradle and maven. As a Java Developer I need to create a gradle project in the Intellij Idea. But I want to avoid all the fancy wizards and open the project direcly in the Idea. In this example I use Gradle Idea Plugin. Here is my workflow as shown in the diagram,

It is necessary to create directories for the source sets:

Java

mkdir -p src/main/java/<package>
mkdir -p src/main/resources/<package>

Test

mkdir -p src/test/java/<package>
mkdir -p src/test/resources/<package>

However, you can use the Gradle init plugin as follows:

gradle init --type java-library

The init plugin will create a project with initial build file and the java default structure.

├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── src
    ├── main
    │   └── java
    │       └── Library.java
    └── test
        └── java
            └── LibraryTest.java

Now you can modify the build.gradle. For example, Here is my sample build.gradle file:

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'
version = '1.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'Java Lambda Examples',
                   'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
}

test {
    useJUnitPlatform()
}

idea {
    // I used SDKMAN to install the gradle
    pathVariables GRADLE_HOME: file('/Users/ojitha/.sdkman/candidates/gradle/current/bin/gradle')
    module {
        // This is the JDK value for the 'name' in the File -> Project Structure -> JDKs
            jdkName = '10'
            downloadJavadoc = true
            downloadSources = false
    }
}

task sourceSetProperties << {
    sourceSets {
        main {
            println "java.srcDirs = ${java.srcDirs}"
            println "resources.srcDirs = ${resources.srcDirs}"
            println "java.files = ${java.files.name}"
            println "allJava.files = ${allJava.files.name}"
            println "resources.files = ${resources.files.name}"
            println "allSource.files = ${allSource.files.name}"
            println "output.classesDir = ${output.classesDir}"
            println "output.resourcesDir = ${output.resourcesDir}"
            println "output.files = ${output.files}"
        }
    }
}

As shown in the above source line 26, Junit 5 has been used. As shown in the line 31, I have specified the JDKMAN based GRADLE_HOME.

echo $GRADLE_HOME

As shown in the line 34, the jdkName has given as in the Intellij Idea, this has been automatically created by the Idea when it first started.

Now you need to first create the gradle wrapper:

gradle wrapper

This will generate the gradlew file. Use this to open the project in the Idea ( you already installed Idea):

./gradlew openIdea

This will create all the necessary files and open the project in the idea.

You can create GitHub account and connect to GitHub via Idea GitHub plugin. The project you can share with the GitHub via Idea: VCS -> Import into version control -> Share Project on GitHub context menu./

You can download the source from the GitHub.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python