Building ThinWARs with Gradle

UPDATE: As it turns out the two POM files (javaee-api 8.0) (microprofile 1.3) consumed by the build files shown here are not real BOMs. A POM is considered a BOM if it defines a <dependencyManagement> block which neither of these files do. They are just regular POM files for which the regular dependency resolution mechanism works out of the box. This means there’s no strict requirement for building ThinWARs with Gradle 5.0, you can use earlier versions too. The Gradle build file has been updated to reflect this change however the screencast remains unchanged.

Java Champion Adam Bien has advocated for some time that when building Java web applications one should aim for designing ThinWARs, a technique that reduces build configuration to its bare minimum. In this post I’ll show how you can build a ThinWAR using Gradle 5.0 as this release adds support for consuming BOMs without requiring an external plugin. After bootstrapping a sample project using Adam’s thinWAR maven archetype I manually created an equivalent Gradle build file.

Here’s the Maven POM once tweaked to have Microprofile as an additional dependency (still within Adam’s ThinWAR rules)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.microprofile</groupId>
            <artifactId>microprofile</artifactId>
            <version>1.3</version>
            <type>pom</type>
            <scope>provided</scope>
        </dependency> 
    </dependencies>
    <build>
        <finalName>sample</finalName>
    </build>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
</project>

And here is the equivalent Gradle build file

apply plugin: 'war'

group = 'com.acme'
version = '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compileOnly('javax:javaee-api:8.0')
    compileOnly('org.eclipse.microprofile:microprofile:1.3')
}

war {
    archiveFileName = 'sample.war'
}

As it turns out the Gradle build file is thinner than it’s Maven counterpart and it also produces a smaller WAR file. The following video shows the two builds in action

Keep on coding!

Trackbacks/Pingbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

ˆ Back To Top