View Javadoc

1   /*
2    *  Copyright 2006 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package org.apache.geronimo.plugin.car;
18  
19  import org.apache.geronimo.plugin.MojoSupport;
20  
21  import org.apache.maven.project.MavenProject;
22  import org.apache.maven.project.MavenProjectHelper;
23  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
24  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
25  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
26  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
27  import org.apache.maven.artifact.resolver.ArtifactResolver;
28  import org.apache.maven.artifact.versioning.VersionRange;
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.factory.ArtifactFactory;
32  import org.apache.maven.model.Dependency;
33  import org.apache.maven.model.Exclusion;
34  import org.apache.maven.plugin.MojoExecutionException;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.FileOutputStream;
39  import java.io.BufferedOutputStream;
40  
41  import java.util.Set;
42  import java.util.List;
43  import java.util.Iterator;
44  import java.util.HashSet;
45  import java.util.ArrayList;
46  import java.util.Properties;
47  
48  /**
49   * Support for <em>packaging</em> Mojos.
50   *
51   * @version $Id: AbstractCarMojo.java 431523 2006-08-15 04:02:48Z jdillon $
52   */
53  public abstract class AbstractCarMojo
54      extends MojoSupport
55  {
56      /**
57       * The maven project.
58       *
59       * @parameter expression="${project}"
60       * @required
61       * @readonly
62       */
63      protected MavenProject project;
64  
65      /**
66       * Used to look up Artifacts in the remote repository.
67       *
68       * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
69       * @required
70       * @readonly
71       */
72      protected ArtifactFactory factory;
73  
74      /**
75       * Used to look up Artifacts in the remote repository.
76       *
77       * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
78       * @required
79       * @readonly
80       */
81      protected ArtifactResolver resolver;
82  
83      /**
84       * Location of the local repository.
85       *
86       * @parameter expression="${localRepository}"
87       * @readonly
88       * @required
89       */
90      protected ArtifactRepository local;
91  
92      /**
93       * List of Remote Repositories used by the resolver.
94       *
95       * @parameter expression="${project.remoteArtifactRepositories}"
96       * @readonly
97       * @required
98       */
99      protected java.util.List remoteRepos;
100 
101     /**
102      * The basedir of the project.
103      *
104      * @parameter expression="${basedir}"
105      * @required
106      * @readonly
107      */
108     protected File basedir;
109 
110     /**
111      * The maven project's helper.
112      *
113      * @parameter expression="${component.org.apache.maven.project.MavenProjectHelper}"
114      * @required
115      * @readonly
116      */
117     protected MavenProjectHelper projectHelper;
118 
119     /**
120      * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
121      * @required
122      * @readonly
123      */
124     protected ArtifactFactory artifactFactory;
125 
126     protected Set getProjectArtifacts(final MavenProject project) {
127         Set artifacts = new HashSet();
128 
129         Iterator dependencies = project.getDependencies().iterator();
130         while (dependencies.hasNext()) {
131             Dependency dep = (Dependency) dependencies.next();
132 
133             String groupId = dep.getGroupId();
134             String artifactId = dep.getArtifactId();
135             VersionRange versionRange = VersionRange.createFromVersion(dep.getVersion());
136             String type = dep.getType();
137             if (type == null) {
138                 type = "jar";
139             }
140 
141             String classifier = dep.getClassifier();
142             boolean optional = dep.isOptional();
143             String scope = dep.getScope();
144             if (scope == null) {
145                 scope = Artifact.SCOPE_COMPILE;
146             }
147 
148             Artifact artifact = artifactFactory.createDependencyArtifact(
149                 groupId,
150                 artifactId,
151                 versionRange,
152                 type,
153                 classifier,
154                 scope,
155                 optional);
156 
157             if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
158                 artifact.setFile(new File(dep.getSystemPath()));
159             }
160 
161             List exclusions = new ArrayList();
162             for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
163                 Exclusion e = (Exclusion) j.next();
164                 exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
165             }
166 
167             ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
168             artifact.setDependencyFilter(newFilter);
169             artifacts.add(artifact);
170         }
171 
172         return artifacts;
173     }
174 
175     protected Set getProjectArtifacts() {
176         return getProjectArtifacts(project);
177     }
178     
179     protected void generateExplicitVersionProperties(final File outputFile) throws IOException {
180         log.debug("Generating explicit version properties: " + outputFile);
181 
182         // Generate explicit_versions for all our dependencies...
183         Properties props = new Properties();
184         Iterator iter = getProjectArtifacts().iterator();
185         while (iter.hasNext()) {
186             Artifact artifact = (Artifact)iter.next();
187             String name = artifact.getGroupId() + "/" + artifact.getArtifactId() + "//" + artifact.getType();
188             String value = artifact.getVersion();
189 
190             log.debug("Setting " + name + "=" + value);
191             props.setProperty(name, value);
192         }
193 
194         BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
195         props.store(output, null);
196         output.flush();
197         output.close();
198     }
199 
200     protected static File getArchiveFile(final File basedir, final String finalName, String classifier) {
201         if (classifier == null) {
202             classifier = "";
203         }
204         else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) {
205             classifier = "-" + classifier;
206         }
207 
208         return new File(basedir, finalName + classifier + ".car");
209     }
210 
211     //
212     // NOTE: Bits below lifed from the maven-depndency-plugin
213     //
214 
215     //
216     // TODO: Replace with ArtifactItem and move to base-class
217     //
218 
219     /**
220      * Resolves the Artifact from the remote repository if nessessary. If no version is specified, it will
221      * be retrieved from the dependency list or from the DependencyManagement section of the pom.
222      */
223     protected Artifact getArtifact(final ArtifactItem item) throws MojoExecutionException {
224         Artifact artifact;
225 
226         if (item.getVersion() == null) {
227             fillMissingArtifactVersion(item);
228 
229             if (item.getVersion() == null) {
230                 throw new MojoExecutionException("Unable to find artifact version of " + item.getGroupId()
231                     + ":" + item.getArtifactId() + " in either dependency list or in project's dependency management.");
232             }
233 
234         }
235 
236         String classifier = item.getClassifier();
237         if (classifier == null || classifier.equals("")) {
238             artifact = factory.createArtifact(
239                     item.getGroupId(),
240                     item.getArtifactId(),
241                     item.getVersion(),
242                     Artifact.SCOPE_PROVIDED,
243                     item.getType());
244         }
245         else {
246             artifact = factory.createArtifactWithClassifier(
247                     item.getGroupId(),
248                     item.getArtifactId(),
249                     item.getVersion(),
250                     item.getType(),
251                     item.getClassifier());
252         }
253 
254         try {
255             resolver.resolve(artifact, remoteRepos, local);
256         }
257         catch (ArtifactResolutionException e) {
258             throw new MojoExecutionException("Unable to resolve artifact.", e);
259         }
260         catch (ArtifactNotFoundException e) {
261             throw new MojoExecutionException("Unable to find artifact.", e);
262         }
263 
264         return artifact;
265     }
266 
267     /**
268      * Tries to find missing version from dependancy list and dependency management.
269      * If found, the artifact is updated with the correct version.
270      */
271     private void fillMissingArtifactVersion(final ArtifactItem item) {
272         log.debug("Attempting to find missing version in " + item.getGroupId() + ":" + item.getArtifactId());
273 
274         List list = this.project.getDependencies();
275 
276         for (int i = 0; i < list.size(); ++i) {
277             Dependency dependency = (Dependency) list.get(i);
278 
279             if (dependency.getGroupId().equals(item.getGroupId())
280                 && dependency.getArtifactId().equals(item.getArtifactId())
281                 && dependency.getType().equals(item.getType()))
282             {
283                 log.debug("Found missing version: " + dependency.getVersion() + " in dependency list.");
284 
285                 item.setVersion(dependency.getVersion());
286 
287                 return;
288             }
289         }
290 
291         list = this.project.getDependencyManagement().getDependencies();
292 
293         for (int i = 0; i < list.size(); i++) {
294             Dependency dependency = (Dependency) list.get(i);
295 
296             if (dependency.getGroupId().equals(item.getGroupId())
297                 && dependency.getArtifactId().equals(item.getArtifactId())
298                 && dependency.getType().equals(item.getType()))
299             {
300                 log.debug("Found missing version: " + dependency.getVersion() + " in dependency management list");
301 
302                 item.setVersion(dependency.getVersion());
303             }
304         }
305     }
306 }