10 Best Gradle Plugins for Java Projects

10 Killer Gradle Plugins You Can't Live Without

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.

Did you know? The plugin adds clean<TaskName> tasks for any task that produces outputs e.g. cleanJar, cleanTest etc.

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.

Did you know? You can enforce a specific version of Java using Gradle’s Java toolchain. Gradle will always use the configured version to compile and run code.

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.

Did you know? The application plugin automatically applies the java plugin, so there’s no need to apply both.

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 it’s 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!

Did you know? The plugin automatically declares JUnit 5 dependencies for any test suites you add. So no need to add anything to the dependencies block.

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.

Did you know? Configure the jacocoTestCoverageVerification task to fail your build if your code coverage drops below a set threshold.

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!

Did you know? the plugin includes the publishToMavenLocal task to publish to your local Maven repository at ~/.m2/repository. Perfect for testing changes locally.

Get going with Gradle course
Gradle icon

Want to learn more about Gradle?
Check out the full selection of Gradle tutorials.

10 Best Gradle Plugins for Java Projects

Leave a Reply

Your email address will not be published.

Scroll to top

FREE GRADLE COURSE!

Getting started with Gradle just got A LOT easier!

Get Going with Gradle is the fastest way to a working knowledge of Gradle.

✅ Work effectively in basic Gradle projects
✅ Know how to setup Java projects in Gradle
✅ Understand the Gradle fundamentals

And it's yours today for FREE!

Where shall I send your access details?