001    /*
002     *  Copyright 2006 The Apache Software Foundation
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    
017    package org.apache.geronimo.plugin.car;
018    
019    import org.apache.geronimo.plugin.MojoSupport;
020    
021    import org.apache.maven.project.MavenProject;
022    import org.apache.maven.project.MavenProjectHelper;
023    import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
024    import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
025    import org.apache.maven.artifact.resolver.ArtifactResolutionException;
026    import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
027    import org.apache.maven.artifact.resolver.ArtifactResolver;
028    import org.apache.maven.artifact.versioning.VersionRange;
029    import org.apache.maven.artifact.Artifact;
030    import org.apache.maven.artifact.repository.ArtifactRepository;
031    import org.apache.maven.artifact.factory.ArtifactFactory;
032    import org.apache.maven.model.Dependency;
033    import org.apache.maven.model.Exclusion;
034    import org.apache.maven.plugin.MojoExecutionException;
035    
036    import java.io.File;
037    import java.io.IOException;
038    import java.io.FileOutputStream;
039    import java.io.BufferedOutputStream;
040    
041    import java.util.Set;
042    import java.util.List;
043    import java.util.Iterator;
044    import java.util.HashSet;
045    import java.util.ArrayList;
046    import java.util.Properties;
047    
048    /**
049     * Support for <em>packaging</em> Mojos.
050     *
051     * @version $Id: AbstractCarMojo.java 431523 2006-08-15 04:02:48Z jdillon $
052     */
053    public abstract class AbstractCarMojo
054        extends MojoSupport
055    {
056        /**
057         * The maven project.
058         *
059         * @parameter expression="${project}"
060         * @required
061         * @readonly
062         */
063        protected MavenProject project;
064    
065        /**
066         * Used to look up Artifacts in the remote repository.
067         *
068         * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
069         * @required
070         * @readonly
071         */
072        protected ArtifactFactory factory;
073    
074        /**
075         * Used to look up Artifacts in the remote repository.
076         *
077         * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
078         * @required
079         * @readonly
080         */
081        protected ArtifactResolver resolver;
082    
083        /**
084         * Location of the local repository.
085         *
086         * @parameter expression="${localRepository}"
087         * @readonly
088         * @required
089         */
090        protected ArtifactRepository local;
091    
092        /**
093         * List of Remote Repositories used by the resolver.
094         *
095         * @parameter expression="${project.remoteArtifactRepositories}"
096         * @readonly
097         * @required
098         */
099        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    }