001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.geronimo.mavenplugins.car;
021:
022: import java.io.File;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.StringReader;
026: import java.util.Collections;
027: import java.util.List;
028:
029: import org.apache.geronimo.kernel.config.ConfigurationData;
030: import org.apache.geronimo.kernel.config.ConfigurationStore;
031: import org.apache.geronimo.kernel.config.InvalidConfigException;
032: import org.apache.geronimo.kernel.config.NoSuchConfigException;
033: import org.apache.geronimo.kernel.repository.Artifact;
034: import org.apache.geronimo.system.configuration.RepositoryConfigurationStore;
035: import org.apache.geronimo.system.plugin.PluginInstallerGBean;
036: import org.apache.geronimo.system.plugin.PluginXmlUtil;
037: import org.apache.geronimo.system.plugin.model.ArtifactType;
038: import org.apache.geronimo.system.plugin.model.DependencyType;
039: import org.apache.geronimo.system.plugin.model.LicenseType;
040: import org.apache.geronimo.system.plugin.model.PluginArtifactType;
041: import org.apache.geronimo.system.plugin.model.PluginType;
042: import org.apache.geronimo.system.repository.Maven2Repository;
043: import org.apache.maven.model.License;
044: import org.apache.maven.model.Plugin;
045: import org.apache.maven.model.PluginExecution;
046: import org.apache.maven.model.Resource;
047: import org.codehaus.plexus.configuration.PlexusConfiguration;
048: import org.codehaus.plexus.util.xml.Xpp3Dom;
049:
050: /**
051: * Generate a geronimo-plugin.xml file based on configuration in the pom and car-maven-plugin configuration.
052: *
053: * @version $Rev: 617774 $ $Date: 2008-02-02 01:06:58 -0800 (Sat, 02 Feb 2008) $
054: * @goal prepare-metadata
055: */
056: public class PluginMetadataGeneratorMojo extends AbstractCarMojo {
057:
058: /**
059: * The Geronimo repository where modules will be packaged up from.
060: *
061: * @parameter expression="${project.build.directory}/repository"
062: * @required
063: */
064: private File targetRepository = null;
065:
066: /**
067: * Directory for generated plugin metadata file.
068: *
069: * @parameter expression="${project.build.directory}/resources/"
070: * @required
071: */
072: protected File targetDir = null;
073:
074: /**
075: * Name of generated plugin metadata file.
076: *
077: * @parameter default-value="META-INF/geronimo-plugin.xml"
078: * @required
079: */
080: protected String pluginMetadataFileName = null;
081:
082: /**
083: * Full path of generated plugin metadata file.
084: *
085: * @ parameter expression="${project.build.directory}/resources/META-INF/geronimo-plugin.xml"
086: * @required
087: */
088: // protected File targetFile = null;
089: /**
090: * Whether licenses (copied from maven licence elements) are OSI approved.
091: *
092: * @parameter default-value="false"
093: */
094: private boolean osiApproved;
095:
096: /**
097: * Category of the geronimo plugin.
098: *
099: * @parameter
100: */
101: private String category;
102:
103: /**
104: * Dependencies explicitly listed in the car-maven-plugin configuration
105: *
106: * @parameter
107: */
108: private List<Dependency> dependencies = Collections.emptyList();
109:
110: /**
111: * Configuration of use of maven dependencies. If missing or if value element is false, use the explicit list in the car-maven-plugin configuration.
112: * If present and true, use the maven dependencies in the current pom file of scope null, runtime, or compile. In addition, the version of the maven
113: * dependency can be included or not depending on the includeVersion element.
114: *
115: * @parameter
116: */
117: private UseMavenDependencies useMavenDependencies;
118:
119: /**
120: * Shared configuration from parent that we merge since maven is incompetent at it. This is a plugin-artifactType element without moduleId or dependencies.
121: * Do not attempt to include more than one of these in the parent poms since maven will not merge them correctly.
122: *
123: * @parameter
124: */
125: private PlexusConfiguration commonInstance;
126:
127: /**
128: * Configuration for this instance itself. This is a plugin-artifactType element without moduleId or dependencies. Do not include more than one of these in the parent poms
129: * since maven will not merge them correctly. Actually we have to fish this out of the model since maven mangles the xml for us.
130: */
131: // private PlexusConfiguration instance;
132: protected void doExecute() throws Exception {
133:
134: PluginType metadata = new PluginType();
135: metadata.setName(project.getName());
136: metadata
137: .setAuthor(project.getOrganization() == null ? "unknown"
138: : project.getOrganization().getName());
139: metadata.setUrl(project.getOrganization() == null ? "unknown"
140: : project.getOrganization().getUrl());
141: metadata.setDescription(project.getDescription());
142: metadata.setCategory(category);
143:
144: if (project.getLicenses() != null) {
145: for (Object licenseObj : project.getLicenses()) {
146: License license = (License) licenseObj;
147: LicenseType licenseType = new LicenseType();
148: licenseType.setValue(license.getName());
149: licenseType.setOsiApproved(osiApproved);
150: metadata.getLicense().add(licenseType);
151: }
152: }
153:
154: PluginArtifactType instance;
155: Plugin plugin = (Plugin) project
156: .getModel()
157: .getBuild()
158: .getPluginsAsMap()
159: .get(
160: "org.apache.geronimo.buildsupport:car-maven-plugin");
161: if (plugin == null) {
162: throw new Error("Unable to resolve car plugin");
163: }
164:
165: Xpp3Dom dom;
166: if (plugin.getExecutions().isEmpty()) {
167: dom = (Xpp3Dom) plugin.getConfiguration();
168: } else {
169: if (plugin.getExecutions().size() > 1) {
170: throw new IllegalStateException(
171: "Cannot determine correct configuration for PluginMetadataGeneratorMojo: "
172: + plugin.getExecutionsAsMap().keySet());
173: }
174: dom = (Xpp3Dom) ((PluginExecution) plugin.getExecutions()
175: .get(0)).getConfiguration();
176: }
177: Xpp3Dom instanceDom = dom.getChild("instance");
178:
179: if (instanceDom == null
180: || instanceDom.getChild("plugin-artifact") == null) {
181: instance = new PluginArtifactType();
182: } else {
183: String instanceString = instanceDom.getChild(
184: "plugin-artifact").toString();
185: instance = PluginXmlUtil
186: .loadPluginArtifactMetadata(new StringReader(
187: instanceString.replace("#{", "${")));
188: }
189: if (this .commonInstance != null
190: && this .commonInstance.getChild("plugin-artifact") != null) {
191: PluginArtifactType commonInstance = PluginXmlUtil
192: .loadPluginArtifactMetadata(new StringReader(
193: this .commonInstance.getChild(
194: "plugin-artifact").toString()
195: .replace("#{", "${")));
196: //merge
197: if (instance.getArtifactAlias().isEmpty()) {
198: instance.getArtifactAlias().addAll(
199: commonInstance.getArtifactAlias());
200: }
201: if (instance.getConfigSubstitution().isEmpty()) {
202: instance.getConfigSubstitution().addAll(
203: commonInstance.getConfigSubstitution());
204: }
205: //it doesn't make sense to copy a "generic" config.xml content into a specific module.
206: // if ((instance.getConfigXmlContent() == null || instance.getConfigXmlContent().getGbean().isEmpty())
207: // && (commonInstance.getConfigXmlContent() != null && !commonInstance.getConfigXmlContent().getGbean().isEmpty())) {
208: // instance.setConfigXmlContent(new ConfigXmlContentType());
209: // instance.getConfigXmlContent().getGbean().addAll(commonInstance.getConfigXmlContent().getGbean());
210: // }
211: if (instance.getCopyFile().isEmpty()) {
212: instance.getCopyFile().addAll(
213: commonInstance.getCopyFile());
214: }
215: if (instance.getDependency().isEmpty()) {
216: instance.getDependency().addAll(
217: commonInstance.getDependency());
218: }
219: if (instance.getGeronimoVersion().isEmpty()) {
220: instance.getGeronimoVersion().addAll(
221: commonInstance.getGeronimoVersion());
222: }
223: if (instance.getJvmVersion().isEmpty()) {
224: instance.getJvmVersion().addAll(
225: commonInstance.getJvmVersion());
226: }
227: if (instance.getObsoletes().isEmpty()) {
228: instance.getObsoletes().addAll(
229: commonInstance.getObsoletes());
230: }
231: if (instance.getPrerequisite().isEmpty()) {
232: instance.getPrerequisite().addAll(
233: commonInstance.getPrerequisite());
234: }
235: if (instance.getSourceRepository().isEmpty()) {
236: instance.getSourceRepository().addAll(
237: commonInstance.getSourceRepository());
238: }
239: }
240: metadata.getPluginArtifact().add(instance);
241:
242: ArtifactType artifactType = new ArtifactType();
243: artifactType.setGroupId(project.getGroupId());
244: artifactType.setArtifactId(project.getArtifactId());
245: artifactType.setVersion(project.getVersion());
246: ArtifactType existingArtifact = instance.getModuleId();
247: if (existingArtifact != null
248: && existingArtifact.getType() != null) {
249: artifactType.setType(existingArtifact.getType());
250: } else {
251: artifactType.setType(project.getArtifact().getType());
252: }
253: instance.setModuleId(artifactType);
254: addDependencies(instance);
255: targetDir.mkdirs();
256: File targetFile = new File(targetDir.toURI().resolve(
257: pluginMetadataFileName));
258: targetFile.getParentFile().mkdirs();
259: FileOutputStream out = new FileOutputStream(targetFile);
260: try {
261: PluginXmlUtil.writePluginMetadata(metadata, out);
262: } finally {
263: out.close();
264: }
265: Resource resource = new Resource();
266: resource.setDirectory(targetDir.getPath());
267: resource.addInclude(pluginMetadataFileName);
268: getProject().getResources().add(resource);
269: }
270:
271: private void addDependencies(PluginArtifactType instance)
272: throws InvalidConfigException, IOException,
273: NoSuchConfigException {
274: if (useMavenDependencies == null
275: || !useMavenDependencies.isValue()) {
276: for (Dependency dependency : dependencies) {
277: instance.getDependency().add(
278: dependency.toDependencyType());
279: }
280: } else {
281: if (targetRepository.exists()
282: && targetRepository.isDirectory()) {
283: Maven2Repository targetRepo = new Maven2Repository(
284: targetRepository);
285: ConfigurationStore configStore = new RepositoryConfigurationStore(
286: targetRepo);
287: Artifact pluginArtifact = PluginInstallerGBean
288: .toArtifact(instance.getModuleId());
289: ConfigurationData data = configStore
290: .loadConfiguration(pluginArtifact);
291: PluginInstallerGBean.addGeronimoDependencies(data,
292: instance.getDependency(), useMavenDependencies
293: .isIncludeVersion());
294: } else {
295: List<org.apache.maven.model.Dependency> includedDependencies = project
296: .getOriginalModel().getDependencies();
297: List<org.apache.maven.model.Dependency> artifacts = project
298: .getDependencies();
299: for (org.apache.maven.model.Dependency dependency : includedDependencies) {
300: dependency = resolveDependency(dependency,
301: artifacts);
302: if (includeDependency(dependency)) {
303: DependencyType gdep = toGeronimoDependency(
304: dependency, useMavenDependencies
305: .isIncludeVersion());
306: instance.getDependency().add(gdep);
307: }
308: }
309: }
310:
311: }
312: }
313:
314: private static DependencyType toGeronimoDependency(
315: final org.apache.maven.model.Dependency dependency,
316: boolean includeVersion) {
317: DependencyType dependencyType = new DependencyType();
318: dependencyType.setGroupId(dependency.getGroupId());
319: dependencyType.setArtifactId(dependency.getArtifactId());
320: if (includeVersion) {
321: dependencyType.setVersion(dependency.getVersion());
322: }
323: dependencyType.setType(dependency.getType());
324: dependencyType.setStart(true);
325: return dependencyType;
326: }
327: }
|