Posted on 2 mins read

Instead of starting a Java project using IntelliJ I experimented with the gradle init plugin. Recall: gradle init --type java-library. Originally, the idea was to have an easy way to start a project for Java but frankly speaking using an IDE already makes it easy enough. Although… I like being able to do gradle run in terminal to execute my code.

Generated build.gradle

apply plugin: 'java'


repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.21'

    testCompile 'junit:junit:4.12'
}

The above build.gradle doesn’t make it easy to work with the project in IntelliJ. We have to make some modifications.

Modified build.gradle

  1. Add IDEA plugin: Generate idea project files.
  2. Add application plugin: Enable gradle run.
  3. Set the main class to be executed.
apply plugin:'application'
mainClassName = "Main"

apply plugin: 'idea'

apply plugin: 'java'

repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {

    compile gradleApi()

    compile 'org.slf4j:slf4j-api:1.7.21'

    testCompile 'junit:junit:4.12'
}

Execution

After setting up, you can run gradle build in terminal and open the project in IntelliJ (I am using the community edition). After making modifications you can execute the tests with gradle test and execute the main program with gradle run.