/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.geronimo.mavenplugins.car;

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;

import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.geronimo.genesis.MojoSupport;

import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Exclusion;

/**
 * Support for <em>packaging</em> Mojos.
 *
 * @version $Rev$ $Date$
 */
public abstract class AbstractCarMojo
    extends MojoSupport
{
    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * The basedir of the project.
     *
     * @parameter expression="${basedir}"
     * @required
     * @readonly
     */
    protected File basedir;

    /**
     * The maven project's helper.
     *
     * @component
     * @required
     * @readonly
     */
    protected MavenProjectHelper projectHelper;

    //
    // MojoSupport Hooks
    //

    protected MavenProject getProject() {
        return project;
    }

    /**
     * @component
     * @required
     * @readonly
     */
    private ArtifactFactory artifactFactory = null;

    protected ArtifactFactory getArtifactFactory() {
        return artifactFactory;
    }

    /**
     * @component
     * @required
     * @readonly
     */
    private ArtifactResolver artifactResolver = null;

    protected ArtifactResolver getArtifactResolver() {
        return artifactResolver;
    }

    /**
     * @parameter expression="${localRepository}"
     * @readonly
     * @required
     */
    protected ArtifactRepository artifactRepository = null;

    protected ArtifactRepository getArtifactRepository() {
        return artifactRepository;
    }

    //
    // Access to Project artifacts
    //

    protected Set getProjectArtifacts(final MavenProject project) {
        Set artifacts = new HashSet();

        Iterator dependencies = project.getDependencies().iterator();
        while (dependencies.hasNext()) {
            Dependency dep = (Dependency) dependencies.next();

            String groupId = dep.getGroupId();
            String artifactId = dep.getArtifactId();
            VersionRange versionRange = VersionRange.createFromVersion(dep.getVersion());
            String type = dep.getType();
            if (type == null) {
                type = "jar";
            }

            String classifier = dep.getClassifier();
            boolean optional = dep.isOptional();
            String scope = dep.getScope();
            if (scope == null) {
                scope = Artifact.SCOPE_COMPILE;
            }

            Artifact artifact = getArtifactFactory().createDependencyArtifact(
                groupId,
                artifactId,
                versionRange,
                type,
                classifier,
                scope,
                optional);

            if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) {
                artifact.setFile(new File(dep.getSystemPath()));
            }

            List exclusions = new ArrayList();
            for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) {
                Exclusion e = (Exclusion) j.next();
                exclusions.add(e.getGroupId() + ":" + e.getArtifactId());
            }

            ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);
            artifact.setDependencyFilter(newFilter);
            artifacts.add(artifact);
        }

        return artifacts;
    }

    protected Set getProjectArtifacts() {
        return getProjectArtifacts(project);
    }
    
    protected void generateExplicitVersionProperties(final File outputFile) throws IOException {
        log.debug("Generating explicit version properties: " + outputFile);

        // Generate explicit_versions for all our dependencies...
        Properties props = new Properties();
        Iterator iter = getProjectArtifacts().iterator();
        while (iter.hasNext()) {
            Artifact artifact = (Artifact)iter.next();
            String name = artifact.getGroupId() + "/" + artifact.getArtifactId() + "//" + artifact.getType();
            String value = artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getType();

            log.debug("Setting " + name + "=" + value);
            props.setProperty(name, value);
        }

        BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(outputFile));
        props.store(output, null);
        output.flush();
        output.close();
    }

    protected static File getArchiveFile(final File basedir, final String finalName, String classifier) {
        if (classifier == null) {
            classifier = "";
        }
        else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) {
            classifier = "-" + classifier;
        }

        return new File(basedir, finalName + classifier + ".car");
    }
}

@@ -989,115 +989,16 @@
 til.
-Set;%0Aimport java.util.List;%0Aimport java.util.Iterator;%0Aimport java.util.HashSet;%0Aimport java.util.ArrayList
+Iterator
 ;%0Aim
@@ -1074,16 +1074,71 @@
 pport;%0A%0A
+import org.apache.maven.plugin.MojoExecutionException;%0A
 import o
@@ -1231,146 +1231,8 @@
 er;%0A
-import org.apache.maven.artifact.resolver.filter.ArtifactFilter;%0Aimport org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;%0A
 impo
@@ -1291,66 +1291,8 @@
 er;%0A
-import org.apache.maven.artifact.versioning.VersionRange;%0A
 impo
@@ -1455,91 +1455,8 @@
 ory;
-%0Aimport org.apache.maven.model.Dependency;%0Aimport org.apache.maven.model.Exclusion;
 %0A%0A/*
@@ -2888,1875 +2888,8 @@
  %7D%0A%0A
-    //%0A    // Access to Project artifacts%0A    //%0A%0A    protected Set getProjectArtifacts(final MavenProject project) %7B%0A        Set artifacts = new HashSet();%0A%0A        Iterator dependencies = project.getDependencies().iterator();%0A        while (dependencies.hasNext()) %7B%0A            Dependency dep = (Dependency) dependencies.next();%0A%0A            String groupId = dep.getGroupId();%0A            String artifactId = dep.getArtifactId();%0A            VersionRange versionRange = VersionRange.createFromVersion(dep.getVersion());%0A            String type = dep.getType();%0A            if (type == null) %7B%0A                type = %22jar%22;%0A            %7D%0A%0A            String classifier = dep.getClassifier();%0A            boolean optional = dep.isOptional();%0A            String scope = dep.getScope();%0A            if (scope == null) %7B%0A                scope = Artifact.SCOPE_COMPILE;%0A            %7D%0A%0A            Artifact artifact = getArtifactFactory().createDependencyArtifact(%0A                groupId,%0A                artifactId,%0A                versionRange,%0A                type,%0A                classifier,%0A                scope,%0A                optional);%0A%0A            if (scope.equalsIgnoreCase(Artifact.SCOPE_SYSTEM)) %7B%0A                artifact.setFile(new File(dep.getSystemPath()));%0A            %7D%0A%0A            List exclusions = new ArrayList();%0A            for (Iterator j = dep.getExclusions().iterator(); j.hasNext();) %7B%0A                Exclusion e = (Exclusion) j.next();%0A                exclusions.add(e.getGroupId() + %22:%22 + e.getArtifactId());%0A            %7D%0A%0A            ArtifactFilter newFilter = new ExcludesArtifactFilter(exclusions);%0A            artifact.setDependencyFilter(newFilter);%0A            artifacts.add(artifact);%0A        %7D%0A%0A        return artifacts;%0A    %7D%0A%0A    protected Set getProjectArtifacts() %7B%0A        return getProjectArtifacts(project);%0A    %7D%0A    %0A
     
@@ -2966,16 +2966,40 @@
 ) throws
+ MojoExecutionException,
  IOExcep

Use getProjectArtifacts() from MojoSupport