With so many Gradle plugins to choose from, are you sure you’re making the most of what’s available? If not, check out this list of the 10 best core Gradle plugins to work more effectively in Java projects.

How were these plugins chosen?

These are all core Gradle plugins, written by the Gradle team and bundled inside the Gradle distribution itself. That means they’re high quality and there’s no need to download them externally.

Just apply them by id in the plugins block of your build script like this.

plugins {
    id 'java'
}
plugins {
    java
}

All these plugins have been tried and tested in Java projects. They’ll help build your project following best practices and improve code quality.

Best plugins list

Now let’s jump into the plugins! Read the list below then watch this video to see them in action. 👇

1) base - the parent plugin you never heard of

The base plugin adds several standard tasks to a project to be used by other plugins, like build, assemble, check, and clean. Maybe you recognise some of these?

Functionality is then added to these tasks by other plugins, like the java plugin.

You’ll normally never apply the base plugin yourself, but it’s good to know that it’s there.

2) java - build Java applications with just one plugin

The java plugin sets up all the configuration needed to build Java projects. For example, it adds src/main/java as a source directory and includes many tasks such ascompileJava, jar, and test.

The great thing about the java plugin is that you don’t need to know the details to get started building Java applications. Just apply the plugin and run ./gradlew build to compile your code, produce a jar file, and run tests.

3) application - simplify running your application

If you’re writing an executable application, you’ll likely have a public static void main(String[] args) method as the entry point. The application plugin offers a run task which will execute the configuredmainClass.

application {
    mainClass = 'com.tomgregory.MyAmazingApplication'
}

That’s really helpful for easily running your application from the command line with ./gradlew run.

4) war - generate web application resource files

If you’re deploying to a standalone application server like Tomcat, you’ll need to generate a war file. This format is slightly different from jar, but Gradle offers the war plugin to take care of the details.

Apply the plugin then run ./gradlew assemble and you’ll see a .war file in build/libs.

5) java-library - build Java libraries (not applications)

The 2 main types of Java software components you can build are applications and libraries. To build a library which will get consumed by other Java projects, apply the java-library plugin.

The main thing that this plugin does is add the api dependency configuration. Any dependencies you add here will be added to the compile classpath of any consumers of your library. So it’s important to only add dependencies that form part of your application’s application binary interface (ABI), and put any other dependencies on implementation.

Confused? Check out this article which goes into more detail on api vs. implementation dependencies.

6) jvm-test-suite - create separate test suites

Does your project have integration tests? If so, the jvm-test-suite plugin helps you separate them to run independently of unit tests.

You can create whatever test suites you like, each of which will have its own source directory (e.g. src/integrationTest/java) and task (e.g. integrationTest).

testing {
    suites {
        integrationTest(JvmTestSuite) {
            dependencies {
                implementation project
            }
        }
    }
}

This helps you get fast feedback by running unit tests more frequently than your slower integration tests. Nice!

7) jacoco - generate code coverage stats

The code coverage metric tells you what percentage of your production code has been covered by tests. The jacoco plugin uses the Jacoco tool to calculate this during test execution.

You can also run the jacocoTestReport task to generate an HTML report, or configure it to generate XML to send off to tools like SonarQube.

8) checkstyle - conform to your code style

Once your team has decided how your Java code should look, configure the checkstyle plugin to enforce this during your build.

It uses a checkstyle.xml file where you can add any of the 100s of checks that Checkstyle has available.

For example:

  • use 4 spaces for indentation

  • put the opening curly bracket { on the same line (or not!)

When you run the check task your build passes or fails based on whether your code conforms.

9) pmd - validate your code quality

The pmd plugin works in a similar way to checkstyle, but the focus of PMD is more on code quality than style.

For example, one rule is to avoid System.out.println statements, preferring logging frameworks instead.

The nice thing about PMD is that it comes with some predefined rulesets you can easily apply to your project.

pmd {
    ruleSets = ['category/java/bestpractices.xml']
}

10) maven-publish - time to publish your project

When it comes to publishing your project’s jar file, the maven-publish plugin has you covered.

At a high level you configure what to publish and where to publish to.

Normally that’s the artifact generated by the jar task, to be published to a private remote Maven repository. Just remember not to store your credentials in the build script!