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 non J2EE module such as third party libraries.
29 *
30 * <p>Such module is not incorporated in the generated {@code application.xml}
31 * but some application servers support it. To include it in the generated
32 * deployment descriptor anyway, set the {@code includeInApplicationXml} boolean flag.
33 * </p>
34 *
35 * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
36 */
37 public class JarModule extends AbstractEarModule {
38 /**
39 * Default type of the artifact of a non Java EE module such as third party library.
40 */
41 public static final String DEFAULT_ARTIFACT_TYPE = "jar";
42
43 private Boolean includeInApplicationXml = Boolean.FALSE;
44
45 /**
46 * Create an instance.
47 */
48 public JarModule() {
49 this.type = DEFAULT_ARTIFACT_TYPE;
50 this.classPathItem = true;
51 }
52
53 /**
54 * @param a {@link Artifact}
55 * @param defaultLibBundleDir The default library bundle directory.
56 * @param includeInApplicationXml Include the application xml or not.
57 */
58 public JarModule(Artifact a, String defaultLibBundleDir, Boolean includeInApplicationXml) {
59 super(a);
60 setLibBundleDir(defaultLibBundleDir);
61 this.includeInApplicationXml = includeInApplicationXml;
62 this.classPathItem = true;
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 public void appendModule(XMLWriter writer, String version, Boolean generateId) {
69 // Generates an entry in the application.xml only if
70 // includeInApplicationXml is set
71 if (includeInApplicationXml) {
72 startModuleElement(writer, generateId);
73 writer.startElement(JAVA_MODULE);
74 writer.writeText(getUri());
75 writer.endElement();
76
77 writeAltDeploymentDescriptor(writer, version);
78
79 writer.endElement();
80 }
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 public void resolveArtifact(Set<Artifact> artifacts) throws EarPluginException, MojoFailureException {
87 // Let's resolve the artifact
88 super.resolveArtifact(artifacts);
89
90 // If the defaultLibBundleDir is set and no bundle dir is
91 // set, set the default as bundle dir
92 setLibBundleDir(earExecutionContext.getDefaultLibBundleDir());
93 }
94
95 private void setLibBundleDir(String defaultLibBundleDir) {
96 if (defaultLibBundleDir != null && bundleDir == null) {
97 this.bundleDir = defaultLibBundleDir;
98 }
99 }
100
101 /**
102 * {@inheritDoc}
103 */
104 public boolean changeManifestClasspath() {
105 return false;
106 }
107 }