Gradle, WildFly 8 with Eclipse

Hello World,
Searching for the web it’s hard to find a good ready to use initial project structure, so I made my own project set up. A little bit of copy paste, a little bit of research and some of it I learn from mistakes in the past. I call the project Ugly, because nothing is smooth when you combine more then one component-oriented software (a.k.a frameworks) in one project.
Project Structure (classical approach)

  • ugly-web, is for web presentation (a.k.a web pages)
  • ugly-ejb-services, is for ejb business components
  • ugly-dao-services, is for your dao objects
  • ugly-entities, is for your domain objects a.k.a database tables mapped in classes
  • ugly-common,  is for common classes used by all application layers
  • ugly-data-generator is for generating test data and also proving the correctness of your domain model

Gradle is a new build tool, which in couple of years will replace Maven as de-facto build tool.It’s easy to use even if you don’t know Groovy language (like me) and setup a project structure and start working 🙂
Whole build.gradle script

description = "Ugly project because awesome is too fucking mainstream"
apply from: 'http://www.tellurianring.com/projects/gradle-plugins/gradle-templates/apply.groovy'

allprojects {
	apply plugin: "java"
	apply plugin: "eclipse"
	apply plugin: 'eclipse-wtp'
	sourceCompatibility = 1.7
	targetCompatibility = 1.7
	version = '1.0'
	repositories {
		mavenCentral()
                maven{
			url "http://htmleasy-maven.googlecode.com/svn/trunk/"
		}
		mavenRepo name: 'jboss-nexus', url: "http://repository.jboss.org/nexus/content/groups/public/"
	}

	configurations {
	    provided
	}
	sourceSets {
	    main.compileClasspath += configurations.provided
	    test.compileClasspath += configurations.provided
	    test.runtimeClasspath += configurations.provided
	}
	eclipse {
	  classpath {
	    plusConfigurations += configurations.provided
	  }
	}

}

// Domain projects
project(":ugly-entities") {
	dependencies {
	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  provided "javax.validation:validation-api:1+"
	  provided "org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_1.1_spec:1+"
	  compile project(":ugly-common")
	}
}

project(":ugly-data-generator") {

	dependencies {
	  // Dependencies for all entities
	  compile project(":ugly-entities")
	  compile project(":ugly-dao-services")
	  compile "org.fluttercode.datafactory:datafactory:0.8"
	  compile "org.hibernate:hibernate-core:4+"
	  compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  compile "javax.validation:validation-api:1+"
	  compile "org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_1.1_spec:1+"
	  compile "org.hibernate:hibernate-entitymanager:4+"
	  compile "postgresql:postgresql:9+"
	}

}
project(":ugly-common") {
	dependencies {
	  // Dependencies for all entities
	}
}

// EJB Projects
project(":ugly-ejb-services") {
	dependencies {
	  // Dependencies for ejb services
	  compile project(":ugly-dao-services")
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "javax.enterprise:cdi-api:1+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	}
}

project(":ugly-dao-services") {
	dependencies {
	  // Dependencies for daos
	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate:hibernate-entitymanager:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"
	  provided "postgresql:postgresql:9+"
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "javax.enterprise:cdi-api:1+"
	  compile project(":ugly-entities")
	}

}
// WEB Projects
project(":ugly-web") {
	apply plugin: 'eclipse-wtp'
	apply plugin: 'war'

	eclipse {
	  wtp {
	    facet {
	      facet name: 'jst.java', version: '1.7'
	      facet name: 'jst.web', version: '3.1'
	    }
	  }
	}

	dependencies {
	  // Dependencies for web app
	  compile project(":ugly-ejb-services")
	  provided "org.jboss.spec.javax.ws.rs:jboss-jaxrs-api_1.1_spec:1+"
	  provided "javax.enterprise:cdi-api:1+"
	  provided "org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec:1+"
	  provided "org.jboss.resteasy:resteasy-jaxrs:3+"
	  provided "com.googlecode.htmleasy:htmleasy:0.7"
	  provided "javax.servlet:jstl:1+"
	  provided "jstl:jstl:1+"
	  provided "taglibs:standard:1+"
	  provided "javax.servlet:servlet-api:3+"
	  provided "javax.servlet:jsp-api:2+"

	  provided "org.hibernate:hibernate-core:4+"
	  provided "org.hibernate:hibernate-entitymanager:4+"
	  provided "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1+"

	}
}

How let examine the script step by step.
First we apply gradle plug-in for easy creating couple of projects like Java, Java Web, Scala, Groovy. It’s nice plug-in if you don’t want to use IDE support for creating projects

apply from: 'http://www.tellurianring.com/projects/gradle-plugins/gradle-templates/apply.groovy'

Next section is called allprojects and here we define all common properties for all projects. In this section we apply plug-ins for java, eclipse and eclipse-wtp. The last plug-in eclipse-wtp provides useful features for customizing the nature (Project Facets) of eclipse project. After that we override value on default properties, which in our case are important and need to set on every project. These properties are sourceCompatibility, targetCompatibility and version. There names are self-explanatory and in my case they are set to the latest java 1.7 and the version is for project version which is by default 1.0 🙂 The next thing we setup the repositories for dependency management and enable provided feature for jars to all projects. Provided isn’t available in Gradle by default, so we need to implement which is easy (if you Google currently). In configuration section, we declare provided then in sourceSets we added provided dependency to classpaths. This is only for java plug-in, which in most cases isn’t good because not everybody use text editors to develop java, most of developers use IDE, which in my case is Eclipse, so we append the same in eclipse plug-in. With this step we finished with building foundation for our ugly projects. Now it’s time to define them in ground-up fashion.
First project that we explore is ugly-common and its is share by all projects. The purpose of this project is to put classes which can be share from all project like Convertors, Utilities, Exceptions etc. This project in my case doesn’t have any jar dependency, but when your project grow in size it’s nice to have some utility jars like Apache Common or other similar jars.

Next project is ugly-entities which serves as separate logical unit for our domain objects. The dependency javax.ws.rs is for rs bindings like @Form and @FormParam, because our domain object will be reused in web-ui.
Project ugly-dao-services contains dao services, which are part from dao layer and these services execute database operations.
Project ugly-ejb-services contains ejb services and project ugly-web contains web UI for the application.In our case for MVC pattern we use resteasy (Controler), htmleasy (View dispatcher) and jsp (View renderer).

In most cases, developers will use Spring MVC, but if you don’t use Spring eco-system like in my case, adding Spring MVC to your project will add half of spring core projects :). In order to emulate MVC pattern (use similar clean programming API) like in spring MVC, we only need htmleasy as View dispatcher, because other things are provided by wildfly standard distribution. Even without htmleasy we can emulate spring MVC programming api, but in that case we need to make 5-6 classes, which is already done by htmleasy (only 20k in size).

At last, we need to start coding our ugly project in eclipse, so for that purpose we have four command, which you need to execute in order to import gradle project to eclipse

  • gradle clean
  • gradle build
  • gradle cleanEclipse
  • gradle eclipse

After this will import project to eclipse, as gradle projects as shown in the picture below
eclipseGradleImport

Here is the link to svn source repository
Ugly project svn repo
In next posts we will explore ugly sub-projects in more detail way and there are some interesting concepts for reducing complexity of the building software.

4 thoughts on “Gradle, WildFly 8 with Eclipse

  1. Pingback: Reusable MVC | Java Dev
  2. Hello there,
    Firstly, thanks for the tuto.
    Second, I’m getting a problem when I try to clean my project with gradle.

    I’m getting:

    FAILURE: Build failed with an exception.

    * Where:
    Build file ‘/home/halkernel/Projects/Eclipse Workspace/ugly/build.gradle’ line: 16

    * What went wrong:
    A problem occurred evaluating root project ‘ugly’.
    > Could not find method mavenRepo() for arguments [{name=jboss-nexus, url=http://repository.jboss.org/nexus/content/groups/public/}] on repository container.

    * Try:
    Run with –stacktrace option to get the stack trace. Run with –debug option to get more log output.

    • Gradle version is old and the mavenRepo definition is change
      The solution: maven {url “http://repository.jboss.org/nexus/content/groups/public/”}
      Also if you are running on the latest Gradle (2.10)
      you need to change
      eclipse {
      classpath {
      plusConfigurations += configurations.provided
      }
      }
      Into
      eclipse {
      classpath {
      plusConfigurations += [configurations.provided]
      }
      }

Leave a reply to dario Cancel reply