Section 3.5. Using the WAR Plug-in


3.5. Using the WAR Plug-in

Now that you have built the core subproject, you should move on to the web subproject. This is a good occasion for you to learn how to use the Maven WAR plug-in. Figure 3-4 shows the directory structure for the web subproject.

Figure 3-4. Directory structure for the web subproject


A project that uses the WAR plug-in contains three source trees:


src/main

Contains the runtime Java sources. There is only one servlet (mdn.qotd.web.QuoteServlet) for the web subproject.


src/test

Contains the JUnit tests for unit testing the Java code found in src/main. The mdn.qotd.web.QuoteServletTest tests methods from the QuoteServlet servlet. Note that these unit tests are performed in isolation (i.e., without a running container). They are not functional tests.


src/webapp

Contains all the web application resources (HTML files, JSP files, configuration files, etc.). For the web subproject you have only a WEB-INF/web.xml file, which maps the QuoteServlet to a web context. This exceedingly simple web.xml file follows:

<?xml version="1.0" encoding="ISO-8859-1"?>    <!DOCTYPE web-app     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"     "http://java.sun.com/dtd/web-app_2_3.dtd">    <web-app>      <servlet>     <servlet-name>QuoteServlet</servlet-name>     <servlet-class>mdn.qotd.web.QuoteServlet</servlet-class>   </servlet>      <servlet-mapping>     <servlet-name>MainServlet</servlet-name>     <url-pattern>/</url-pattern>   </servlet-mapping>    </web-app>

As shown in Section 1.1, src/main and src/test locations are defined in the project.xml file under the build element. The following build element tells Maven where to find the source and tests for this project:

  <build>     <sourceDirectory>src/main</sourceDirectory>     <unitTestSourceDirectory>src/test</unitTestSourceDirectory>     <unitTest>       <includes>         <include>**/*Test.java</include>       </includes>     </unitTest>   </build>

The location of the web application resources (src/webapp) is not defined in project.xml. It is an extension brought by the WAR plug-in. It is configured by a plug-in property named maven.war.src, which defaults to the src/webapp location.

Remember that the web subproject depends on the core subproject, which means you want to have the core JAR bundled in your WAR, under WEB-INF/lib. You also need to bundle the Rome and JDOM JARs which are required by the core subproject. You achieve this by tagging the core, Rome, and JDOM JARs' dependency elements defined in project.xml with the war.bundle property, as shown here:

    <dependency>       <groupId>mdn</groupId>       <artifactId>qotd-core</artifactId>       <version>${pom.currentVersion}</version>       <properties>         <war.bundle>true</war.bundle>       </properties>     </dependency>     <dependency>       <groupId>rome</groupId>       <artifactId>rome</artifactId>       <version>0.5</version>       <properties>         <war.bundle>true</war.bundle>       </properties>     </dependency>     <dependency>       <groupId>jdom</groupId>       <artifactId>jdom</artifactId>       <version>1.0</version>       <properties>         <war.bundle>true</war.bundle>       </properties>     </dependency>

Tagging a dependency with a property is a common feature used by several Maven plug-ins. It allows you to specify some special actions that the plug-in should perform on certain artifacts. Here, the WAR plug-in will copy the core JAR to WEB-INF/lib when asked to generate the WAR by calling the war goal.


Tip: If you were building a project containing EJBs and producing an EAR, you would apply the same tagging technique in your EAR's subproject to bundle the WARs and EJBs.

Maven copies these dependencies to WEB-INF/lib during the war:webapp goal execution in the following console output:

C:\dev\mavenbook\code\qotd\web>maven war  _ _  _ _ |  \/  |_ _ _Apache_ _ _ _ _ | |\/| / _` \ V / -_) ' \  ~ intelligent projects ~ |_|  |_\_ _,_|\_/\_ _ _|_||_|  v. 1.0.2    build:start:    war:init:    war:war-resources:     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\qotd-web     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF    java:prepare-filesystem:     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\classes    java:compile:     [echo] Compiling to C:\dev\mavenbook\code\qotd\web/target/classes     [javac] Compiling 1 source file to C:\dev\mavenbook\code\qotd\web\target\classes    java:jar-resources:    test:prepare-filesystem:     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\test-classes     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\test-reports    test:test-resources:    test:compile:     [javac] Compiling 1 source file to C:\dev\mavenbook\code\qotd\web\target\test-classes    test:test:     [junit] Testsuite: mdn.qotd.web.QuoteServletTest     [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0,261 sec     [junit]     [junit] Testcase: testSendQuote took 0,011 sec    war:webapp:     [echo] Assembling webapp qotd-web     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\lib     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\tld     [mkdir] Created dir: C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\classes     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\lib     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\lib     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\lib     [copy] Copying 1 file to C:\dev\mavenbook\code\qotd\web\target\qotd-web\WEB-INF\classes    war:war:     [echo] Building WAR qotd-web     [jar] Building jar: C:\dev\mavenbook\code\qotd\web\target\qotd-web.war BUILD SUCCESSFUL Total time: 3 seconds

As shown in Figure 3-5, the generated WARtarget/qotd-web.warcontains the core, Rome, and JDOM JARs in WEB-INF/lib.

Figure 3-5. Content of generated WAR file showing that the core JAR has been included


Finally, you need to deploy the web WAR to the Maven local repository so that it can be made available for the acceptance and packager subprojects (see Figure 3-1). You achieve this by calling the war:install goal from the WAR plug-in:

C:\dev\mavenbook\code\qotd\web>maven war:install [...] war:war:     [echo] Building WAR qotd-web     [jar] Building jar: C:\dev\mavenbook\code\qotd\web\target\qotd-web.war Copying: from 'C:\dev\mavenbook\code\qotd\web\target\qotd-web.war' to: 'C:\Documents and Settings\Vincent Massol\.maven\repository\mdn\wars\qotd-web-1.0.war' Copying: from 'C:\dev\mavenbook\code\qotd\web\project.xml' to: 'C:\Documents and Settings\Vincent Massol\.maven\repository\mdn\poms\qotd-web-1.0.pom' BUILD SUCCESSFUL Total time: 3 seconds

3.5.1. What just happened?

You just created a WAR file by running maven war. You then installed a WAR into your local repository using maven war:install. To bundle dependencies with the generated WAR file, you set the war.bundle property on each dependency to true.


Note: Your local repository holds more than JARs. It can also hold WAR files, or any artifact created by Maven.

3.5.2. What about...

...the other build actions available from the WAR plug-in?

You can obtain the full list of goals by typing maven -P war. You can find more information about the goals and properties of this plug-in at http://maven.apache.org/reference/plugins/war/goals.html and http://maven.apache.org/reference/plugins/war/properties.html. Others not discussed in this section include support for adding tag libraries, generating expanded WARs (useful for quick round-trip development for Servlet containers supporting dynamic reloading), and installing the generated WAR in the Maven remote repository.



Maven. A Developer's Notebook
Maven: A Developers Notebook (Developers Notebooks)
ISBN: 0596007507
EAN: 2147483647
Year: 2003
Pages: 125

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net