View Javadoc
1   package org.apache.maven.plugin.ear;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.codehaus.plexus.util.xml.XMLWriter;
25  
26  import java.util.Set;
27  
28  /**
29   * The {@link EarModule} implementation for a Web application module.
30   * 
31   * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
32   * @version $Id: WebModule.java 1645331 2014-12-13 17:31:09Z khmarbaise $
33   */
34  public class WebModule
35      extends AbstractEarModule
36  {
37      private static final String WEB_MODULE = "web";
38  
39      private static final String WEB_URI_FIELD = "web-uri";
40  
41      private static final String CONTEXT_ROOT_FIELD = "context-root";
42  
43      private String contextRoot;
44  
45      /**
46       * Create an instance.
47       */
48      public WebModule()
49      {
50      }
51  
52      /**
53       * @param a {@link Artifact}
54       */
55      public WebModule( Artifact a )
56      {
57          super( a );
58          this.contextRoot = getDefaultContextRoot( a );
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      public void appendModule( XMLWriter writer, String version, Boolean generateId )
65      {
66          startModuleElement( writer, generateId );
67          writer.startElement( WEB_MODULE );
68          writer.startElement( WEB_URI_FIELD );
69          writer.writeText( getUri() );
70          writer.endElement(); // web-uri
71  
72          writer.startElement( CONTEXT_ROOT_FIELD );
73          writer.writeText( getContextRoot() );
74          writer.endElement(); // context-root
75  
76          writer.endElement(); // web
77  
78          writeAltDeploymentDescriptor( writer, version );
79  
80          writer.endElement(); // module
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      public void resolveArtifact( Set<Artifact> artifacts )
87          throws EarPluginException, MojoFailureException
88      {
89          // Let's resolve the artifact
90          super.resolveArtifact( artifacts );
91  
92          // Context root has not been customized - using default
93          if ( contextRoot == null )
94          {
95              contextRoot = getDefaultContextRoot( getArtifact() );
96          }
97      }
98  
99      /**
100      * Returns the context root to use for the web module.
101      * <p/>
102      * Note that this might return <tt>null</tt> till the artifact has been resolved.
103      * 
104      * @return the context root
105      */
106     public String getContextRoot()
107     {
108         return contextRoot;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     public String getType()
115     {
116         return "war";
117     }
118 
119     /**
120      * Generates a default context root for the given artifact, based on the <tt>artifactId</tt>.
121      * 
122      * @param a the artifact
123      * @return a context root for the artifact
124      */
125     private static String getDefaultContextRoot( Artifact a )
126     {
127         if ( a == null )
128         {
129             throw new NullPointerException( "Artifact could not be null." );
130         }
131         return "/" + a.getArtifactId();
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     public String getLibDir()
138     {
139         return "WEB-INF/lib";
140     }
141 }