View Javadoc

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.plugin.car;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.FileOutputStream;
25  import java.io.BufferedOutputStream;
26  
27  import java.util.Set;
28  import java.util.List;
29  import java.util.Iterator;
30  import java.util.HashSet;
31  import java.util.ArrayList;
32  import java.util.Properties;
33  
34  import org.apache.geronimo.genesis.MojoSupport;
35  
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.MavenProjectHelper;
38  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
39  import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
40  import org.apache.maven.artifact.resolver.ArtifactResolver;
41  import org.apache.maven.artifact.versioning.VersionRange;
42  import org.apache.maven.artifact.Artifact;
43  import org.apache.maven.artifact.repository.ArtifactRepository;
44  import org.apache.maven.artifact.factory.ArtifactFactory;
45  import org.apache.maven.model.Dependency;
46  import org.apache.maven.model.Exclusion;
47  
48  /**
49   * Support for <em>packaging</em> Mojos.
50   *
51   * @version $Rev: 451661 $ $Date: 2006-09-30 13:45:53 -0700 (Sat, 30 Sep 2006) $
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       * The basedir of the project.
67       *
68       * @parameter expression="${basedir}"
69       * @required
70       * @readonly
71       */
72      protected File basedir;
73  
74      /**
75       * The maven project's helper.
76       *
77       * @component
78       * @required
79       * @readonly
80       */
81      protected MavenProjectHelper projectHelper;
82  
83      //
84      // MojoSupport Hooks
85      //
86  
87      protected MavenProject getProject() {
88          return project;
89      }
90  
91      /**
92       * @component
93       * @required
94       * @readonly
95       */
96      private ArtifactFactory artifactFactory = null;
97  
98      protected ArtifactFactory getArtifactFactory() {
99          return artifactFactory;
100     }
101 
102     /**
103      * @component
104      * @required
105      * @readonly
106      */
107     private ArtifactResolver artifactResolver = null;
108 
109     protected ArtifactResolver getArtifactResolver() {
110         return artifactResolver;
111     }
112 
113     /**
114      * @parameter expression="${localRepository}"
115      * @readonly
116      * @required
117      */
118     protected ArtifactRepository artifactRepository = null;
119 
120     protected ArtifactRepository getArtifactRepository() {
121         return artifactRepository;
122     }
123 
124     //
125     // Access to Project artifacts
126     //
127 
128     protected Set getProjectArtifacts(final MavenProject project) {
129         Set artifacts = new HashSet();
130 
131         Iterator dependencies = project.getDependencies().iterator();
132         while (dependencies.hasNext()) {
133             Dependency dep = (Dependency) dependencies.next();
134 
135             String groupId = dep.getGroupId();
136             String artifactId = dep.getArtifactId();
137             VersionRange versionRange = VersionRange.createFromVersion(dep.getVersion());
138             String type = dep.getType();
139             if (type == null) {
140                 type = "jar";
141             }
142 
143             String classifier = dep.getClassifier();
144             boolean optional = dep.isOptional();
145             String scope = dep.getScope();
146             if (scope == null) {
147                 scope = Artifact.SCOPE_COMPILE;
148             }
149 
150             Artifact artifact = getArtifactFactory().createDependencyArtifact(
151                 groupId,
152                 artifactId,
153                 versionRange,
154                 type,
155                 classifier,
156                 scope,
157                 optional);
158 
159             if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
160                 artifact.setFile(new File(dep.getSystemPath()));
161             }
162 
163             List exclusions = new ArrayList();
164             for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
165                 Exclusion e = (Exclusion) j.next();
166                 exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
167             }
168 
169             ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
170             artifact.setDependencyFilter(newFilter);
171             artifacts.add(artifact);
172         }
173 
174         return artifacts;
175     }
176 
177     protected Set getProjectArtifacts() {
178         return getProjectArtifacts(project);
179     }
180     
181     protected void generateExplicitVersionProperties(final File outputFile) throws IOException {
182         log.debug("Generating explicit version properties: " + outputFile);
183 
184         // Generate explicit_versions for all our dependencies...
185         Properties props = new Properties();
186         Iterator iter = getProjectArtifacts().iterator();
187         while (iter.hasNext()) {
188             Artifact artifact = (Artifact)iter.next();
189             String name = artifact.getGroupId() + "/" + artifact.getArtifactId() + "//" + artifact.getType();
190             String value = artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getType();
191 
192             log.debug("Setting " + name + "=" + value);
193             props.setProperty(name, value);
194         }
195 
196         BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
197         props.store(output, null);
198         output.flush();
199         output.close();
200     }
201 
202     protected static File getArchiveFile(final File basedir, final String finalName, String classifier) {
203         if (classifier == null) {
204             classifier = "";
205         }
206         else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) {
207             classifier = "-" + classifier;
208         }
209 
210         return new File(basedir, finalName + classifier + ".car");
211     }
212 }