Packaging Java Enterprise Edition applications

We have discussed how to create ZIP, TAR, and JAR archives with Gradle in this chapter and the previous one. In a Java project, we can also package our applications as Web application Archive (WAR) or Enterprise Archive (EAR) files. For a web application, we would like to package our application as a WAR file, while a Java Enterprise Edition application can be packaged as an EAR file. Gradle also supports these types of archives with plugins and tasks.

Creating a WAR file

To create a WAR file, we can add a new task of the War type to our Java project. The properties and methods of the War task are the same as for the other archive tasks, such as Jar. In fact, the War task extends the Jar task.

The War task has an extra webInf() method to define a source directory for the WEB-INF directory in a WAR file. The webXml property can be used to reference a web.xml file that needs to be copied to the WAR file. This is just another way to include a web.xml file, we can also place the web.xml file in the WEB-INF directory of the root source directory that we defined for the WAR file.

With the classpath() method, we can define a dependency configuration or directory with libraries or class files that we want copied to our WAR file. If the file is a JAR or ZIP file, it is copied to the WEB-INF/lib directory and other files are copied in the WEB-INF/classes directory.

In the following sample build file, we will define a new War task. We set the root of the WAR file contents to the src/main/webapp directory. We use the webInf() and classpath() methods to customize the contents of the WEB-INF, WEB-INF/classes, and WEB-INF/lib folders. We also set a custom web.xml file with the webXml property of the task, as follows:

apply plugin: 'java' 
 
version = '1.0' 
 
// Custom archive task with 
// specific properties for a WAR archive. 
task war(type: War) { 
    dependsOn classes 
 
    from 'src/main/webapp' 
 
    // Files copied to WEB-INF. 
    webInf { 
        from 'src/main/webInf' 
    } 
 
    // Copied to WEB-INF/classes. 
    classpath sourceSets.main.runtimeClasspath 
 
    // Copied to WEB-INF/lib. 
    classpath fileTree('libs') 
 
    // Custom web.xml. 
    webXml = file('  ') 
    baseName = 'gradle-webapp' 
} 
 
assemble.dependsOn war 

To create the WAR file, we can execute the War or assemble task. The War task is added to the assemble task as a task dependency. This is why if we invoke the assemble task, Gradle will execute the War task. Once we have executed the task, the gradle-webapp-1.0.war WAR file is created in the build/libs directory:

$ gradle war
:compileJava
:processResources
:classes
:war
BUILD SUCCESSFUL
Total time: 0.727 secs
$ ls build/libs
gradle-webapp-1.0.war

Creating an EAR file

To create an EAR file, we can create a new task of the Ear type. This task has the same properties and methods as the Jar task. The Ear task extends the Jar task.

With the lib() method, we can define the files that need to be copied to the lib directory in the EAR file.

The following build file has a simple Ear task:

apply plugin: 'java' 
 
version = '1.0' 
 
// Create custom archive task 
// with specific properties to 
// create an EAR archive file. 
task ear(type: Ear) { 
    from 'src/main/application' 
 
    lib { 
        from fileTree('earLibs') 
    } 
 
    baseName = 'gradle-enterprise-app' 
} 
 
assemble.dependsOn ear 

We can execute the Ear task and look in the build/libs directory to see the resulting gradle-enterprise-app-1.0.ear file:

$ gradle ear
:ear
BUILD SUCCESSFUL
Total time: 0.694 secs
$ ls build/libs
gradle-enterprise-app-1.0.ear
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.70.101