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     */
019    
020    package org.apache.geronimo.plugin.car;
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.io.FileOutputStream;
025    import java.io.BufferedOutputStream;
026    
027    import java.util.Set;
028    import java.util.List;
029    import java.util.Iterator;
030    import java.util.HashSet;
031    import java.util.ArrayList;
032    import java.util.Properties;
033    
034    import org.apache.geronimo.genesis.MojoSupport;
035    
036    import org.apache.maven.project.MavenProject;
037    import org.apache.maven.project.MavenProjectHelper;
038    import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
039    import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
040    import org.apache.maven.artifact.resolver.ArtifactResolver;
041    import org.apache.maven.artifact.versioning.VersionRange;
042    import org.apache.maven.artifact.Artifact;
043    import org.apache.maven.artifact.repository.ArtifactRepository;
044    import org.apache.maven.artifact.factory.ArtifactFactory;
045    import org.apache.maven.model.Dependency;
046    import org.apache.maven.model.Exclusion;
047    
048    /**
049     * Support for <em>packaging</em> Mojos.
050     *
051     * @version $Rev: 451661 $ $Date: 2006-09-30 13:45:53 -0700 (Sat, 30 Sep 2006) $
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         * The basedir of the project.
067         *
068         * @parameter expression="${basedir}"
069         * @required
070         * @readonly
071         */
072        protected File basedir;
073    
074        /**
075         * The maven project's helper.
076         *
077         * @component
078         * @required
079         * @readonly
080         */
081        protected MavenProjectHelper projectHelper;
082    
083        //
084        // MojoSupport Hooks
085        //
086    
087        protected MavenProject getProject() {
088            return project;
089        }
090    
091        /**
092         * @component
093         * @required
094         * @readonly
095         */
096        private ArtifactFactory artifactFactory = null;
097    
098        protected ArtifactFactory getArtifactFactory() {
099            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    }