KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > mavenplugins > car > AbstractCarMojo


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
20 package org.apache.geronimo.mavenplugins.car;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26
27 import java.util.Iterator JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.apache.geronimo.genesis.MojoSupport;
32 import org.apache.geronimo.genesis.util.ArtifactItem;
33 import org.apache.geronimo.genesis.dependency.DependencyHelper;
34 import org.apache.geronimo.genesis.dependency.DependencyTree;
35 import org.apache.geronimo.genesis.dependency.DependencyTree.Node;
36
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.project.MavenProject;
40 import org.apache.maven.project.MavenProjectHelper;
41 import org.apache.maven.artifact.Artifact;
42 import org.apache.maven.artifact.repository.ArtifactRepository;
43
44 /**
45  * Support for <em>packaging</em> Mojos.
46  *
47  * @version $Rev: 480326 $ $Date: 2006-11-28 20:48:06 -0500 (Tue, 28 Nov 2006) $
48  */

49 public abstract class AbstractCarMojo
50     extends MojoSupport
51 {
52     /**
53      * The maven project.
54      *
55      * @parameter expression="${project}"
56      * @required
57      * @readonly
58      */

59     protected MavenProject project;
60
61     /**
62      * The basedir of the project.
63      *
64      * @parameter expression="${basedir}"
65      * @required
66      * @readonly
67      */

68     protected File JavaDoc basedir;
69
70     /**
71      * The maven project's helper.
72      *
73      * @component
74      * @required
75      * @readonly
76      */

77     protected MavenProjectHelper projectHelper;
78     
79     /**
80      * @component
81      */

82     protected DependencyHelper dependencyHelper = null;
83     
84     //
85
// MojoSupport Hooks
86
//
87

88     protected MavenProject getProject() {
89         return project;
90     }
91
92     /**
93      * @parameter expression="${localRepository}"
94      * @readonly
95      * @required
96      */

97     protected ArtifactRepository artifactRepository = null;
98
99     protected ArtifactRepository getArtifactRepository() {
100         return artifactRepository;
101     }
102     
103     protected void init() throws MojoExecutionException, MojoFailureException {
104         super.init();
105         
106         dependencyHelper.setArtifactRepository(artifactRepository);
107     }
108     
109     /**
110      * Generates a properties file with explicit versions of artifacts of the current project transitivly.
111      */

112     protected void generateExplicitVersionProperties(final File JavaDoc outputFile, DependencyTree dependencies) throws MojoExecutionException, IOException JavaDoc {
113         log.debug("Generating explicit version properties: " + outputFile);
114
115         // Generate explicit_versions for all our dependencies...
116
Properties JavaDoc props = new Properties JavaDoc();
117         
118         try {
119
120             Node root = dependencies.getRootNode();
121             
122             // Skip the root node
123
Iterator JavaDoc children = root.getChildren().iterator();
124             while (children.hasNext()) {
125                 Node child = (Node) children.next();
126                 appendExplicitVersionProperties(child, props);
127             }
128         }
129         catch (Exception JavaDoc e) {
130             throw new MojoExecutionException("Failed to determine project dependencies", e);
131         }
132         
133         BufferedOutputStream JavaDoc output = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outputFile));
134         props.store(output, null);
135         output.flush();
136         output.close();
137     }
138     
139     private void appendExplicitVersionProperties(final Node node, final Properties JavaDoc props) {
140         assert node != null;
141         assert props != null;
142         
143         Artifact artifact = node.getArtifact();
144         if ("test".equals(artifact.getScope())) {
145             if (log.isDebugEnabled()) {
146                 log.debug("Skipping artifact with scope test: " + artifact);
147             }
148             return;
149         }
150         
151         String JavaDoc name = artifact.getGroupId() + "/" + artifact.getArtifactId() + "//" + artifact.getType();
152         String JavaDoc value = artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getType();
153         
154         if (log.isDebugEnabled()) {
155             log.debug("Setting " + name + "=" + value);
156         }
157         props.setProperty(name, value);
158         
159         if (!node.getChildren().isEmpty()) {
160             Iterator JavaDoc children = node.getChildren().iterator();
161             
162             while (children.hasNext()) {
163                 Node child = (Node) children.next();
164                 appendExplicitVersionProperties(child, props);
165             }
166         }
167     }
168
169     protected static File JavaDoc getArchiveFile(final File JavaDoc basedir, final String JavaDoc finalName, String JavaDoc classifier) {
170         if (classifier == null) {
171             classifier = "";
172         }
173         else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) {
174             classifier = "-" + classifier;
175         }
176
177         return new File JavaDoc(basedir, finalName + classifier + ".car");
178     }
179
180     //
181
// Geronimo/Maven Artifact Interop
182
//
183

184     protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(final org.apache.maven.artifact.Artifact artifact) {
185         assert artifact != null;
186
187         return new org.apache.geronimo.kernel.repository.Artifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType());
188     }
189
190     protected org.apache.maven.artifact.Artifact geronimoToMavenArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) throws MojoExecutionException {
191         assert artifact != null;
192
193         ArtifactItem item = new ArtifactItem();
194         item.setGroupId(artifact.getGroupId());
195         item.setArtifactId(artifact.getArtifactId());
196         item.setVersion(artifact.getVersion().toString());
197         item.setType(artifact.getType());
198
199         return createArtifact(item);
200     }
201
202     /**
203      * Determine if the given artifact is a Geronimo module.
204      *
205      * @param artifact The artifact to check; must not be null.
206      * @return True if the artifact is a Geronimo module.
207      */

208     protected boolean isModuleArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) {
209         assert artifact != null;
210
211         return "car".equals(artifact.getType());
212     }
213
214     protected class ArtifactLookupImpl
215         implements Maven2RepositoryAdapter.ArtifactLookup
216     {
217
218         private final Map JavaDoc resolvedArtifacts;
219
220         public ArtifactLookupImpl(Map JavaDoc resolvedArtifacts) {
221             this.resolvedArtifacts = resolvedArtifacts;
222         }
223
224         public File JavaDoc getBasedir() {
225             String JavaDoc path = getArtifactRepository().getBasedir();
226             return new File JavaDoc(path);
227         }
228
229         private boolean isProjectArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) {
230             MavenProject project = getProject();
231
232             return artifact.getGroupId().equals(project.getGroupId()) &&
233                    artifact.getArtifactId().equals(project.getArtifactId());
234         }
235
236         public File JavaDoc getLocation(final org.apache.geronimo.kernel.repository.Artifact artifact) {
237             assert artifact != null;
238
239             boolean debug = log.isDebugEnabled();
240
241             Artifact mavenArtifact = (Artifact)resolvedArtifacts.get(artifact);
242
243             // If not cached, then make a new artifact
244
if (mavenArtifact == null) {
245                 mavenArtifact = getArtifactFactory().createArtifact(
246                         artifact.getGroupId(),
247                         artifact.getArtifactId(),
248                         artifact.getVersion().toString(),
249                         null,
250                         artifact.getType()
251                 );
252             }
253
254             // Do not attempt to resolve an artifact that is the same as the project
255
if (isProjectArtifact(artifact)) {
256                 if (debug) {
257                     log.debug("Skipping resolution of project artifact: " + artifact);
258                 }
259
260                 //
261
// HACK: Still have to return something, otherwise some CAR packaging will fail...
262
// no idea what is using this file, or if the files does exist if that will be
263
// used instead of any details we are currently building
264
//
265
return new File JavaDoc(getBasedir(), getArtifactRepository().pathOf(mavenArtifact));
266             }
267
268             File JavaDoc file;
269             try {
270                 if (!mavenArtifact.isResolved()) {
271                     if (debug) {
272                         log.debug("Resolving artifact: " + mavenArtifact);
273                     }
274                     mavenArtifact = resolveArtifact(mavenArtifact);
275
276                     // Cache the resolved artifact
277
resolvedArtifacts.put(artifact, mavenArtifact);
278                 }
279
280                 //
281
// HACK: Construct the real local filename from the path and resolved artifact file.
282
// Probably a better way to do this with the Maven API directly, but this is the
283
// best I can do for now.
284
//
285
String JavaDoc path = getArtifactRepository().pathOf(mavenArtifact);
286                 file = new File JavaDoc(getBasedir(), path);
287                 file = new File JavaDoc(mavenArtifact.getFile().getParentFile(), file.getName());
288             }
289             catch (MojoExecutionException e) {
290                 throw new RuntimeException JavaDoc("Failed to resolve: " + mavenArtifact, e);
291             }
292
293             return file;
294         }
295     }
296     
297 }
298
Popular Tags