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