Gradle 5.0 has been released, and officially it is the fastest, most secure, and most powerful version ever released. Improved incremental compilation and incremental annotation handling build on proven performance that already has build caching and up-to-date checking capabilities. Dependency alignment and version locking provide an extensible and flexible dependency management model. Build scans have been significantly improved with new performance and dependency management, logging, and deprecated API usage checks. Statically typed Kotlin DSLS provide code completion, refactoring, and other IDE assistance when creating build logic.

According to the official document, this update mainly brings the following aspects of improvement:

Faster builds

Fine-grained transfer dependency management

Write Gradle build logic

More efficient memory execution

New Gradle call options

New Gradle tasks and plug-in apis

Faster builds

A slow build process is a waste of money and effort, but with Gradle 5.0’s new build caching and incremental processing features, the build process will be substantially improved. After upgrading to Gradle 5.0, your build speed will increase significantly, and you can further improve your build performance by using and configuring the other features described in this section.

Build a cache

Gradle builds very quickly by reusing build caches to avoid repetitive build work. Gradle 4.0 introduces build caches with the purpose of reusing the output from previous calls to Gradle.

Gradle 5.0 is now available as a plug-in for Android, Kotlin, c++, Scala, and many other plug-ins, making tasks cacheable and therefore reusable across different machines. Effective use of build caches can reduce build times by about 90%.

In addition, build caching in Gradle 5.0 is enabled in many scenarios, such as when a task declares an @OutputDirectory or @OutputFiles collection.

Java incremental compilation

In Gradle 5.0, the incremental compiler is highly optimized and uses incremental compilation by default. This is a great message because compiling Java tasks does not require recompiling all of the source files, which makes compiling code much more efficient except for the first time.

Incremental annotation processing

The incremental compiler in Gradle 5.0 supports incremental annotation processing, which can greatly improve the efficiency of incremental compilation when annotation handlers are available. This is an important innovation because more and more projects rely on annotation processors.

To take advantage of incremental annotation processing, be sure to upgrade to the version of the annotation handler that selected the feature. You can find out whether a given annotation process has delta support by either info logging or by looking at the annotation handler.

The new annotationProcessor configuration makes it easy to manage annotation processors and place them on annotationProcessor paths.

Build the scanning

Build scanning has been significantly improved with new checks on performance, dependency management, logging, and use of deprecated apis. Gradle is a free service provided to Gradle users when they need to add scans. Gradle or Apply and configure builds scans from the command line.

Fine-grained transfer dependency management

Gradle 5.0 provides several new features to customize how dependencies are selected, as well as improved POM and BOM support:

  • Dependency constraints allow you to define versions or version ranges to limit and pass dependent versions (Maven does not support this).
  • Platform definitions (aka Maven BOM dependencies) are natively supported, allowing you to import things like Spring without using external plug-ins.
  • Dependency alignment allows different modules in a logical group (such as the Jackson module) to be aligned to the same version.

Depend on the constraint

Dependency constraints provide reliability control over transitive dependencies, and declared constraints are listed in the improved dependency insight report and build scan. For example, here are common Groovy dependency scripts:

dependencies {
    implementation 'org.apache.httpcomponents:httpclient'
    constraints {
        implementation('org, apache httpcomponents: httpclient: 4.5.3') {
            because 'previous versions have a bug impacting this application'
        }
        implementation('Commons - codec: the Commons - the codec: 1.11') {
            because 'version 1.9 pulled from httpclient has bugs affecting this application'}}}Copy the code

OM support

Gradle 5.0 can import BOM files, which can effectively solve the problem of different versions of the same project.

dependencies {
    // import a BOM
    implementation platform('org. Springframework. The boot: spring - the boot - dependencies: 1.5.8. RELEASE')

    // define dependencies without versions
    implementation 'com.google.code.gson:gson'
    implementation 'dom4j:dom4j'
}
Copy the code

In addition, Gradle 5.0 provides a more seamless experience when building generated dependencies using Maven.

  • Gradle correctly separates compilation and runtime scopes when using POM files. This can effectively avoid the performance degradation and dependency leakage problems caused by previously including runtime dependencies in the compiled classpath.
  • Gradle can now also be used with < POm > elements.

Depend on the alignment

Dependency version alignment allows different modules belonging to the same logical group (platform) to have the same version in a dependency.

This ensures that all Spring or Hibernate dependencies have the same version of the problem. In fact, many libraries are distributed as collections, where each library has the same version.

Version dependent locking

You can use Gradle 5.0 to lock dynamic or remote dependencies to a specific version, making dependency resolution more deterministic and reproducible, which prevents build problems from being broken by changes to dependencies.

Write Gradle build logic

You can now use Kotlin to write Gradle build scripts. In addition, Gradle Init extends project types and interactivity.

Kotlin DSL help information

The Kotlin DSL has provided Gradle support since version 1.0. Static typing in Kotlin allows tools to provide better IDE help, including debugging and refactoring build scripts, auto-completion, and everything else you might expect.

If you are interested in writing builds in Kotlin, you can start with the Gradle Kotlin DSL

Expansion and interaction

Users who want to build projects using Gradle can choose other project types to use together, such as Kotlin-Library and Kotlin-Application. In addition, you can choose to generate Groovy or Kotlin DSL build scripts and customize project names and packages.

More instructional documentation

In addition, Gradle documentation and getting started guides provide more information and are easier to discover and access.

  • Improved pages including getting started, troubleshooting, CLI references, managing delivery dependencies, and more.
  • Searchable reference documents hosted by Algolia DocSearch.
  • A reformatted PDF for offline viewing.
  • Classification navigation.
  • Document version selection.

More efficient memory execution

Features such as more efficient in-memory execution of command-line arguments allow for better development workflows, while lower memory requirements and cache cleaning can reduce Gradle’s overhead on the system.

Lower memory requirements

Upgrading Gradle 5.0 not only makes builds faster, but also greatly reduces memory usage. Many caching mechanisms have been optimized in Gradle 5.0, and the default maximum memory for Gradle processes has been greatly reduced.

Process Type Gradle 4.x default heap Gradle 5.0 default heap
Command-line client 1 GB 64 MB
Gradle Daemon 1 GB 512 MB
Worker processes 1/4 of physical memory 512 MB

Staged cache cleanup

Now that the days of manual cache cleaning are over, Gradle can now periodically clean expired caches. Gradle also tracks stale task outputs more accurately and cleans them up without causing incorrect results.

New Gradle call options

JUnit tests

Gradle 5.0 adds support for JUnit 5 tests: JUnit platform, JUnit Jupiter, and JUnit Vintage. This will allow you to enable test grouping and filtering and include custom test engines.

test {
    useJUnitPlatform {
        excludeTags 'slow'
        includeEngines 'junit-vintage'
        failFast = true}}Copy the code

You can use the Fail-fast flag to enable a faster red-green loop, which is further improved by the default, version 5.0 will execute failed tests first.

The log

In Gradle 5.0, log messages can be grouped into tasks based on non-interactive environments such as continuous integration execution.

In addition to showing which tests are being executed, Gradle’s rich command-line console displays a colorful build status that can be easily identified as those test failures. You can also ask Gradle to log tasks as they are executed in Detail console mode.

Finally, Gradle can also summarize, silence, or extend Gradle warning logs by configuring warning mode.

The composite building

Composite builds allow you to include other independent projects; for example, you can develop applications and dependent libraries at the same time.

Now you can also use build scans to examine composite builds. Composite builds can also be continuous compatible. They are built in parallel by default and can now be nested.

JVM command-line arguments

Running Java applications with custom parameters with Gradle 5.0 will be much easier than with other versions because you can simply use them on the command line or through the IDE.

New Gradle tasks and plug-in apis

Gradle 5.0 provides a number of new apis that support faster and more generic build logic.

High-performance API

The new Worker API will allow you to execute tasks safely in parallel and asynchronously. Following our guide to enabling faster build tasks for custom tasks using the Worker API, the following figure shows the difference between using and not using the Worker API.

Improved I/O tasks

Correctly declaring inputs and outputs is critical to incremental builds and build cache behavior. Gradle 5.0 strengthens constraints and introduces new apis for input/output declarations that can help you avoid low-level problems.

Avoid redundant configurations

Some projects generate so many tasks during execution that it does not make sense to configure all these operations when only a few are performed. This is the magic of the new Gradle 5.0 configuration that avoids apis. By adopting these configurations in custom tasks, large projects can save up to 10% in configuration time.

APIs

Gradle 5.0 introduces new apis to improve the management of Maven and Ivy repositories. Mainly include:

  • The signing plug-in supports signing all published artifacts.
  • Configuration scoped dependency exclusion is published.
  • The Maven publishing and Ivy publishing plug-ins provide type-safe DSLS to customize POM or Ivy modules generated as part of a publishing.

Task Timeout Processing

Now you can specify a timeout for the task, after which the task will be interrupted.

Customize Cli parameters

Gradle 5.0 provides new methods that allow users to configure custom tasks. First, you can create custom command-line options using @option, which the user can do by executing gradle help — task your-task.

public class UrlVerify extends DefaultTask {
    private String url;

    @Option(option = "url", description = "Configures the URL to be verified.")
    public void setUrl(String url) {
        this.url = url;
    }

    @Input
    public String getUrl() {
        return url;
    }

    @TaskAction
    public void verify() {
        getLogger().quiet("Verifying URL '{}'", url);

        // verify URL by making a HTTP call
    }
}
Copy the code

Custom nested DSLS

Provides a custom nested DSL for tasks Gradle that use internal apis. Gradle 5.0 provides a first-class API for nesting DSL elements, providing greater flexibility when considering how users configure tasks.

Gradle 5.0 provides additional API convenience for calculating (or deferring) task inputs and outputs. This makes it possible to wire the Gradle models together when customizing tasks without worrying about a given property value being modified or avoiding resource-intensive work during task configuration.

Upgrade guide

To upgrade to version 5.0. Before upgrading, we offer the following suggestions:

  • Upgrade to Gradle 4.10.2 using the Gradle wrapper.
  • Run Gradle help: Scan and list all recommended Gradle apis and their locations, including plug-ins.
  • Update your Gradle plug-ins, especially those that build scan alerts.
  • Upgrading to JDK 8 or later requires running Gradle 5.0.

If you encounter problems, please refer to the troubleshooting guide or contact the community forum. You can also refer to our upgrade documentation.

What is new in Gradle 5.0