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: */package org.apache.geronimo.mavenplugins.car;
019:
020: import java.io.BufferedOutputStream;
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Properties;
028:
029: import org.apache.maven.artifact.Artifact;
030: import org.apache.maven.artifact.repository.ArtifactRepository;
031: import org.apache.maven.plugin.MojoExecutionException;
032: import org.apache.maven.plugin.MojoFailureException;
033: import org.apache.maven.project.MavenProject;
034: import org.apache.maven.project.MavenProjectHelper;
035: import org.codehaus.mojo.pluginsupport.MojoSupport;
036: import org.codehaus.mojo.pluginsupport.dependency.DependencyHelper;
037: import org.codehaus.mojo.pluginsupport.dependency.DependencyTree;
038: import org.codehaus.mojo.pluginsupport.dependency.DependencyTree.Node;
039: import org.codehaus.mojo.pluginsupport.util.ArtifactItem;
040:
041: /**
042: * Support for <em>packaging</em> Mojos.
043: *
044: * @version $Rev: 615381 $ $Date: 2008-01-25 14:36:57 -0800 (Fri, 25 Jan 2008) $
045: */
046: public abstract class AbstractCarMojo extends MojoSupport {
047: /**
048: * The maven project.
049: *
050: * @parameter expression="${project}"
051: * @required
052: * @readonly
053: */
054: protected MavenProject project;
055:
056: /**
057: * The basedir of the project.
058: *
059: * @parameter expression="${basedir}"
060: * @required
061: * @readonly
062: */
063: protected File basedir;
064:
065: /**
066: * The maven project's helper.
067: *
068: * @component
069: * @required
070: * @readonly
071: */
072: protected MavenProjectHelper projectHelper;
073:
074: /**
075: * dependency resolution for the maven repository
076: *
077: * @component
078: */
079: protected DependencyHelper dependencyHelper = null;
080:
081: //
082: // MojoSupport Hooks
083: //
084:
085: protected MavenProject getProject() {
086: return project;
087: }
088:
089: /**
090: * @parameter expression="${localRepository}"
091: * @readonly
092: * @required
093: */
094: protected ArtifactRepository artifactRepository = null;
095:
096: protected ArtifactRepository getArtifactRepository() {
097: return artifactRepository;
098: }
099:
100: protected void init() throws MojoExecutionException,
101: MojoFailureException {
102: super .init();
103:
104: dependencyHelper.setArtifactRepository(artifactRepository);
105: }
106:
107: /**
108: * Generates a properties file with explicit versions of artifacts of the current project transitivly.
109: */
110: protected void generateExplicitVersionProperties(
111: final File outputFile, DependencyTree dependencies)
112: throws MojoExecutionException, IOException {
113: log.debug("Generating explicit version properties: "
114: + outputFile);
115:
116: // Generate explicit_versions for all our dependencies...
117: Properties props = new Properties();
118:
119: try {
120:
121: Node root = dependencies.getRootNode();
122:
123: // Skip the root node
124: Iterator children = root.getChildren().iterator();
125: while (children.hasNext()) {
126: Node child = (Node) children.next();
127: appendExplicitVersionProperties(child, props);
128: }
129: } catch (Exception e) {
130: throw new MojoExecutionException(
131: "Failed to determine project dependencies", e);
132: }
133:
134: BufferedOutputStream output = new BufferedOutputStream(
135: new FileOutputStream(outputFile));
136: props.store(output, null);
137: output.flush();
138: output.close();
139: }
140:
141: private void appendExplicitVersionProperties(final Node node,
142: final Properties props) {
143: assert node != null;
144: assert props != null;
145:
146: Artifact artifact = node.getArtifact();
147: if ("test".equals(artifact.getScope())) {
148: if (log.isDebugEnabled()) {
149: log.debug("Skipping artifact with scope test: "
150: + artifact);
151: }
152: return;
153: }
154:
155: String name = artifact.getGroupId() + "/"
156: + artifact.getArtifactId() + "//" + artifact.getType();
157: String value = artifact.getGroupId() + "/"
158: + artifact.getArtifactId() + "/"
159: + artifact.getVersion() + "/" + artifact.getType();
160:
161: if (log.isDebugEnabled()) {
162: log.debug("Setting " + name + "=" + value);
163: }
164: props.setProperty(name, value);
165:
166: if (!node.getChildren().isEmpty()) {
167: Iterator children = node.getChildren().iterator();
168:
169: while (children.hasNext()) {
170: Node child = (Node) children.next();
171: appendExplicitVersionProperties(child, props);
172: }
173: }
174: }
175:
176: protected static File getArchiveFile(final File basedir,
177: final String finalName, String classifier) {
178: if (classifier == null) {
179: classifier = "";
180: } else if (classifier.trim().length() > 0
181: && !classifier.startsWith("-")) {
182: classifier = "-" + classifier;
183: }
184:
185: return new File(basedir, finalName + classifier + ".car");
186: }
187:
188: //
189: // Geronimo/Maven Artifact Interop
190: //
191:
192: protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(
193: final org.apache.maven.artifact.Artifact artifact) {
194: assert artifact != null;
195:
196: return new org.apache.geronimo.kernel.repository.Artifact(
197: artifact.getGroupId(), artifact.getArtifactId(),
198: artifact.getVersion(), artifact.getType());
199: }
200:
201: protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(
202: final org.apache.maven.model.Dependency artifact) {
203: assert artifact != null;
204:
205: return new org.apache.geronimo.kernel.repository.Artifact(
206: artifact.getGroupId(), artifact.getArtifactId(),
207: artifact.getVersion(), artifact.getType());
208: }
209:
210: protected org.apache.maven.artifact.Artifact geronimoToMavenArtifact(
211: final org.apache.geronimo.kernel.repository.Artifact artifact)
212: throws MojoExecutionException {
213: assert artifact != null;
214:
215: ArtifactItem item = new ArtifactItem();
216: item.setGroupId(artifact.getGroupId());
217: item.setArtifactId(artifact.getArtifactId());
218: item.setVersion(artifact.getVersion().toString());
219: item.setType(artifact.getType());
220:
221: return createArtifact(item);
222: }
223:
224: /**
225: * Determine if the given artifact is a Geronimo module.
226: *
227: * @param artifact The artifact to check; must not be null.
228: * @return True if the artifact is a Geronimo module.
229: */
230: protected boolean isModuleArtifact(
231: final org.apache.geronimo.kernel.repository.Artifact artifact) {
232: assert artifact != null;
233:
234: return "car".equals(artifact.getType());
235: }
236:
237: protected boolean includeDependency(
238: org.apache.maven.model.Dependency dependency) {
239: if (dependency.getGroupId().startsWith(
240: "org.apache.geronimo.genesis")) {
241: return false;
242: }
243: String scope = dependency.getScope();
244: return scope == null || "runtime".equalsIgnoreCase(scope)
245: || "compile".equalsIgnoreCase(scope);
246: }
247:
248: protected org.apache.maven.model.Dependency resolveDependency(
249: org.apache.maven.model.Dependency dependency,
250: List<org.apache.maven.model.Dependency> artifacts) {
251: for (org.apache.maven.model.Dependency match : artifacts) {
252: if (matches(dependency, match)) {
253: return match;
254: }
255: }
256: throw new IllegalStateException("Dependency " + dependency
257: + " is not resolved in project");
258: }
259:
260: private boolean matches(
261: org.apache.maven.model.Dependency dependency,
262: org.apache.maven.model.Dependency match) {
263: if (dependency.getGroupId() != null
264: && !dependency.getGroupId().equals(match.getGroupId())) {
265: return false;
266: }
267: if (dependency.getArtifactId() != null
268: && !dependency.getArtifactId().equals(
269: match.getArtifactId())) {
270: return false;
271: }
272: if (dependency.getType() != null
273: && !dependency.getType().equals(match.getType())) {
274: return false;
275: }
276: return true;
277: }
278:
279: protected class ArtifactLookupImpl implements
280: Maven2RepositoryAdapter.ArtifactLookup {
281:
282: private final Map<org.apache.geronimo.kernel.repository.Artifact, Artifact> resolvedArtifacts;
283:
284: public ArtifactLookupImpl(
285: Map<org.apache.geronimo.kernel.repository.Artifact, Artifact> resolvedArtifacts) {
286: this .resolvedArtifacts = resolvedArtifacts;
287: }
288:
289: public File getBasedir() {
290: String path = getArtifactRepository().getBasedir();
291: return new File(path);
292: }
293:
294: private boolean isProjectArtifact(
295: final org.apache.geronimo.kernel.repository.Artifact artifact) {
296: MavenProject project = getProject();
297:
298: return artifact.getGroupId().equals(project.getGroupId())
299: && artifact.getArtifactId().equals(
300: project.getArtifactId());
301: }
302:
303: public File getLocation(
304: final org.apache.geronimo.kernel.repository.Artifact artifact) {
305: assert artifact != null;
306:
307: boolean debug = log.isDebugEnabled();
308:
309: Artifact mavenArtifact = resolvedArtifacts.get(artifact);
310:
311: // If not cached, then make a new artifact
312: if (mavenArtifact == null) {
313: mavenArtifact = getArtifactFactory().createArtifact(
314: artifact.getGroupId(),
315: artifact.getArtifactId(),
316: artifact.getVersion().toString(), null,
317: artifact.getType());
318: }
319:
320: // Do not attempt to resolve an artifact that is the same as the project
321: if (isProjectArtifact(artifact)) {
322: if (debug) {
323: log
324: .debug("Skipping resolution of project artifact: "
325: + artifact);
326: }
327:
328: //
329: // HACK: Still have to return something, otherwise some CAR packaging will fail...
330: // no idea what is using this file, or if the files does exist if that will be
331: // used instead of any details we are currently building
332: //
333: return new File(getBasedir(), getArtifactRepository()
334: .pathOf(mavenArtifact));
335: }
336:
337: File file;
338: try {
339: if (!mavenArtifact.isResolved()) {
340: if (debug) {
341: log.debug("Resolving artifact: "
342: + mavenArtifact);
343: }
344: mavenArtifact = resolveArtifact(mavenArtifact);
345:
346: // Cache the resolved artifact
347: resolvedArtifacts.put(artifact, mavenArtifact);
348: }
349:
350: //
351: // HACK: Construct the real local filename from the path and resolved artifact file.
352: // Probably a better way to do this with the Maven API directly, but this is the
353: // best I can do for now.
354: //
355: String path = getArtifactRepository().pathOf(
356: mavenArtifact);
357: file = new File(getBasedir(), path);
358: file = new File(
359: mavenArtifact.getFile().getParentFile(), file
360: .getName());
361: } catch (MojoExecutionException e) {
362: throw new RuntimeException("Failed to resolve: "
363: + mavenArtifact, e);
364: }
365:
366: return file;
367: }
368: }
369:
370: }
|