Using Gradle with Java/IntelliJ Projects

July 15, 2020


We can use Gradle to create projects that build from both command-line, and from within IntelliJ! It takes a little more work to setup, but is worth it to gain consistency and reliability in your build environment.

If you are taking CS 349, make sure you are using Gradle before following this guide! Different offerings of the course may have their own project requirements. If you use Gradle, this post essentially replaces previous methods of setting up projects. We don't have to setup paths, or fix configuration issues, because Gradle will download and manage the dependencies for us (which means that the instructions below are much simpler than they were in previous posts).

In this example, we'll setup the Open JDK 11 toolchain with Gradle and IntelliJ Community 2020.1. We'll create a simple "Hello World" JavaFX application.

1. Install Software

You will need to install the following software:

  1. Java JDK 11: download and install from AdoptOpenJDK using the installer for your platform. Do NOT install from the Oracle site (for IP reasons).
  2. IntelliJ 2020.1 Community: download and install from Jetbrains IntelliJ IDEA home page.
  3. Gradle 6.4: download and install from the Gradle Home Page or use your favourite package manager (e.g. Homebrew on macOS)

2. Setup your environment

I recommend setting up path variables for your JAVA installations. In my .zshrc I’ve added the following entries to track my installation directories and add the Java executables to my path (which enables java, jar etc. from the command-line).

export JAVA_HOME='/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home'
export PATH=$PATH:$JAVA_HOME/bin

This should work for any Unix shell (e.g. bash, zsh) in the appropriate config file. On Windows, you would want to add these as environment variables and update your path accordingly.

3. Setup IntelliJ

Out-of-the-box, IntelliJ won’t know about your Java 11 installation. To fix this:

  1. Launch IntelliJ, and from the Title Screen, click on Configure -> Structure for New Projects.
  1. Under Platform-Settings, click on SDKs, then + and add a new Entry pointing to your Java 11 installation directory. Click OK to save it.
  1. You will need to ensure that the Gradle plugin is installed. From the Title Screen, click on Configure -> Plugins, and make sure that "Gradle" is installed. If it's not installed, find it under the Marketplace tab and install it.

4. Create a Gradle Project

To create a new project in IntelliJ:

  1. From the Title Screen, select "Create New Project. Select Gradle -> Java, then select Next. Provide your project with a name and select Finish.
  1. IntelliJ will generate a new project. Once it's finished loading, it will display a "build.gradle" file contents. Change the "build.gradle" to look like this:
          plugins {
            id 'java'
            id 'application'
            id 'org.openjfx.javafxplugin' version '0.0.8'
          }
          
          group 'ca.uwaterloo.cs349'
          version '1.0-SNAPSHOT'
          
          sourceCompatibility = 11
          
          repositories {
            mavenCentral()
          }
          
          dependencies {
          }
          
          javafx {
            modules = ['javafx.controls', 'javafx.fxml', 'javafx.media']
            version = "11.0.2"
          }
          
          mainClassName = 'Hello'
        
  1. This configuration file will attempt to build a class named "Hello" in the "src/" subdirectory. Let's create something to build! Add a file named "Hello.java" under "src/main/java" (right-click and New, Java Class).
        import javafx.application.Application;
        import javafx.scene.Scene;
        import javafx.scene.control.Label;
        import javafx.scene.layout.StackPane;
        import javafx.stage.Stage;
    
        public class HelloFX extends Application {
            @Override
            public void start(Stage stage) throws Exception {
                // executed after init() method
                Label label = new Label("Hello JavaFX");
                Scene scene = new Scene(new StackPane(label), 320, 240);
    
                stage.setTitle("HelloFX");
                stage.setScene(scene);
                stage.show();
                System.out.println("Start");
            }
        }
    
    1. To build and run, use the Gradle Menu: View -> Tool Window -> Gradle. Use "build" and "run".