Grade tutorial for WildFly users

This is a quickstart 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 9.

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-2.9-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-2.9/bin

Great. Now create a simple Java EE project, for example a Web project using the tool of your like. We will include just a Servlet that has a dependency to an external library, in our case itext:

@WebServlet(name = "pdf", urlPatterns = {"/pdf"})
public class CreatePDF extends HttpServlet {

  
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        response.setContentType("application/pdf");
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, response.getOutputStream());
            document.open();
            document.add(new Paragraph("Hello World"));
            document.add(new Paragraph(new Date().toString()));
            document.close();
        } catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

 
}

Here is the structure of our project:

└── src
    └── main
        ├── java
        │   └── com
        │       └── sample
        │           └── CreatePDF.java
        └── webapp
            └── WEB-INF

Now create at the root of your project a file named build.gradle with the following content:

description = 'demo project'  
    
apply plugin: 'java' 
apply plugin: 'war' 
  
sourceCompatibility = 1.8  
targetCompatibility = 1.8 
  
  
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:9.0.2.Final'  
    compile 'com.itextpdf:itextpdf:5.0.6'

 
}  

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:9.0.2.Final, Gradle will be able to trigger transitive dependencies automatically so you have the full wildfly 9 stack available. The providedCompile directive ensures that the WildFly libraries aren't packaged in the WEB-INF/lib folder. On the other hand, the com.itextpdf:itextpdf:5.0.6 marked as compile dependency will be packaged along with your application.

[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.

Follow us on Twitter