Building a slimmer WAR file

The generated WAR file, task-time-tracker-1.0.war, is very large in size at the moment; in fact, it is approximately 32 MB! The default behavior of the maven-war-plugin is to add all of the directories in the webapp folder to the WAR file. For production deployments we do not need a large number of these files, and it is best practice to trim down the WAR file by excluding the content that is not required. We will exclude the entire Ext JS 4 SDK and all of the Sencha Cmd-generated folders under the webapp directory. We will also exclude all the resources that are not applicable for production use, including the index*.html files used during development. The only file served by GlassFish will be the yet-to-be-created index.jsp:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="UTF-8">
    <title>TTT</title>
    <link rel="stylesheet" href="resources/ext-theme-classic-all.css">
    <link rel="stylesheet" href="resources/styles.css">    
<script type="text/javascript" src="all-classes.js"></script>
  </head>
<body></body>
</html>

You will note that the location of the ext-theme-classic-all.css file is in the resources directory, not in the deeply nested ext/packages/ext-theme-classic/build/resources location that is used in the HTML pages. The WAR file generation process will copy the appropriate content to the resources directory from the Ext JS 4 SDK location. This removes the need to include the SDK directory structure in the WAR file.

The production index.jsp file will now become our default welcome-file and we will adjust the WEB-INF/web.xml file accordingly:

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

Running the application after this change in the web.xml file will ensure that the index.jsp file is served by GlassFish when a resource is not specified in the URL.

The changes required in the maven-war-plugin for building a slimmer production WAR file are highlighted in the following code snippet:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <warName>${project.build.finalName}</warName>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <webResources>
      <resource>
        <directory>src/main/webapp/ext/packages/ext-theme-classic/build/resources</directory>
        <targetPath>resources</targetPath>
        <excludes>
          <exclude>ext-theme-classic-all-*</exclude>
        </excludes>                            
      </resource>                            
    </webResources>       
    <packagingExcludes>.sencha/**,app/**,sass/**,overrides/**,build/**,ext/**,app.json,bootstrap.css,bootstrap.js,build.xml, index.html,index-dev.html,index-prod.html,app.js</packagingExcludes>             
  </configuration>
</plugin>

The webResources definition will copy the content of the Ext JS 4 classic CSS theme to the resources directory. The targetPath property is always relative to the webapp directory; hence, we do not need a full path for the resources directory. The directory property is always relative to the root of the Maven project; hence, it needs a full path.

The packagingExcludes property lists all of the directories and files that should not be included in the WAR file. The ** symbol denotes that all of the subdirectories should be excluded. This will ensure that all of the Sencha Cmd-generated folders that are not required by our production WAR file will be excluded.

Executing the Maven build will now generate a WAR file of approximately 6.6 MB that contains only the files required for a production application.

..................Content has been hidden....................

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