View Javadoc
1   package org.apache.maven.plugins.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   */
33  public class WebModule
34      extends AbstractEarModule
35  {
36      /**
37       * Default type of the artifact of a Web application module.
38       */
39      public static final String DEFAULT_ARTIFACT_TYPE = "war";
40  
41      private static final String WEB_MODULE = "web";
42  
43      private static final String WEB_URI_FIELD = "web-uri";
44  
45      private static final String CONTEXT_ROOT_FIELD = "context-root";
46  
47      private static final String DEFAULT_LIB_DIRECTORY = "WEB-INF/lib";
48  
49      private String contextRoot;
50  
51      /**
52       * Create an instance.
53       */
54      public WebModule()
55      {
56          this.type = DEFAULT_ARTIFACT_TYPE;
57          this.libDirectory = DEFAULT_LIB_DIRECTORY;
58      }
59  
60      /**
61       * @param a {@link Artifact}
62       */
63      public WebModule( Artifact a )
64      {
65          super( a );
66          this.contextRoot = getDefaultContextRoot( a );
67          this.libDirectory = DEFAULT_LIB_DIRECTORY;
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      public void appendModule( XMLWriter writer, String version, Boolean generateId )
74      {
75          startModuleElement( writer, generateId );
76          writer.startElement( WEB_MODULE );
77          writer.startElement( WEB_URI_FIELD );
78          writer.writeText( getUri() );
79          writer.endElement(); // web-uri
80  
81          writer.startElement( CONTEXT_ROOT_FIELD );
82          writer.writeText( getContextRoot() );
83          writer.endElement(); // context-root
84  
85          writer.endElement(); // web
86  
87          writeAltDeploymentDescriptor( writer, version );
88  
89          writer.endElement(); // module
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public void resolveArtifact( Set<Artifact> artifacts )
96          throws EarPluginException, MojoFailureException
97      {
98          // Let's resolve the artifact
99          super.resolveArtifact( artifacts );
100 
101         // Context root has not been customized - using default
102         if ( contextRoot == null )
103         {
104             contextRoot = getDefaultContextRoot( getArtifact() );
105         }
106     }
107 
108     /**
109      * Returns the context root to use for the web module.
110      *
111      * Note that this might return {@code null} till the artifact has been resolved.
112      * 
113      * @return the context root
114      */
115     public String getContextRoot()
116     {
117         return contextRoot;
118     }
119 
120     /**
121      * Generates a default context root for the given artifact, based on the {@code artifactId}.
122      * 
123      * @param a the artifact
124      * @return a context root for the artifact
125      */
126     private static String getDefaultContextRoot( Artifact a )
127     {
128         if ( a == null )
129         {
130             throw new NullPointerException( "Artifact could not be null." );
131         }
132         return "/" + a.getArtifactId();
133     }
134 }