This is an updated tutorial for getting started with Gradle. You will learn how to compile and assemble a Java Enterprise application in a matter of minutes and we will deploy it on WildFly.

More and more developers are turning to Gradle for creating high performance builds. There are several advantages when moving to Gradle, maybe the most immediate is that out of the box, Gradle handles transitive dependencies that resolve across multiple repository types including Maven, Ivy, flat files. Let's see an HelloWorld example. At first Download Gradle from: http://gradle.org/gradle-download/ (You will need the binary distribution).

Next unzip Gradle to a location convenient to you.

unzip gradle-5.6.2-bin.zip -d /usr/share

The only requirement is to include the bin folder (which contains the gradle command) in the user's PATH:

PATH=$PATH:/usr/share/gradle-5.6.2/bin

Great. Now let's create a basic project with Gradle.

Bootstrapping a Gradle project

We need to create a simple project structure for our application. You can either create the project structure using plain shell commands as follows:

mkdir -p src/main/java
mkdir -p src/main/resources
mkdir -p src/main/webapp

touch build.gradle

Otherwise, you can use the gradle-init command to create a basic project structure, with a default configuration file:

gradle init --type java-library

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4] 4

Project name (default: gradle-demo): gradle-demo-wildfly
Source package (default: gradle.demo.wildfly): 
BUILD SUCCESSFUL in 43s

Once done with it, you can just import the Gradle Project in any tool that supports it, like IntelliJ Idea:

gradle tutorial wildfly jboss

Next, we will be adding a Servlet that prints some information from the request:

package gradle.demo.wildfly;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "HelloServlet", urlPatterns = {"hello"}, loadOnStartup = 1)
public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().print("Hello, World!");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        if (name == null) name = "World";
        PrintWriter out = response.getWriter();
        out.println("Hello "+name);
    }
}

and then, under the folder src/main/webapp add the following index.html page which triggers our Servlet:

<html>
<head>
    <title>Web Demo</title>
</head>
<body>
<p>Say <a href="/hello">Hello</a></p>

<form method="post" action="hello">
    <h2>Name:</h2>
    <input type="text" id="say-hello-text-input" name="name" />
    <input type="submit" id="say-hello-button" value="Say Hello" />
</form>
</body>
</html>

Here is the structure of our project:

src
├── main
│   ├── java
│   │   └── gradle
│   │       └── demo
│   │           └── wildfly
│   │               ├── HelloServlet.java
│   ├── resources
│   └── webapp
│       └── index.html

 Now, let's update the build.gradle so that it can compile our project:

plugins {
    id 'java-library'
    id 'war'
}

repositories {
    maven { url 'https://repository.jboss.org/nexus/content/groups/public-jboss' }
    maven { url 'https://repository.jboss.org/nexus/content/repositories' }
    maven { url 'https://repository.jboss.org/nexus/content/repositories/thirdparty-releases' }
    mavenCentral()
}

dependencies {
    providedCompile 'org.wildfly:wildfly-spec-api:16.0.0.Final'
}

test {
    // Use junit platform for unit tests
    useJUnitPlatform()
}

war {
    from 'src/webapp' // adds a file-set to the root of the archive
} 

The build.gradle file contains the War plugin which extends the Java plugin to add support for assembling web application WAR files. It disables the default JAR archive generation of the Java plugin and adds a default WAR archive task.

Besides it, the Maven repositories are included in the repositories section.

Finally notice the dependencies section: here the magic of Gradle happens: by specifying the org.wildfly:wildfly-spec-api:16.0.0.Final, Gradle will be able to trigger transitive dependencies automatically so you have the full wildfly 9 stack available. 

[francesco@localhost demogradle]$ gradle war

The first time you will execute the build, the required libraries will be download from the repositories indicated. You will eventually end-up with the following log:

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:war UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build UP-TO-DATE

BUILD SUCCESSFUL

You can find the packaged war archive in the build/libs folder of your application. Deploy it on WildFly and have fun! In the next tutorial we will see how to create more advanced builds with Gradle and WildFly.

The source code for this tutorial is available at: https://github.com/fmarchioni/mastertheboss/tree/master/gradle-demo

0
0
0
s2sdefault