<commit_before>package org.ops4j.pax.exam.mavenplugin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.DefaultArtifactCollector;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.execution.MavenSession;

/**
 * @author <NAME> (tonit)
 * @goal generate-paxexam-config
 * @phase generate-resources
 * @since Mar 17, 2009
 */
public class ExamPlugin extends AbstractMojo
{

    protected static final String SEPARATOR = "/";

    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * The file to generate
     *
     * @parameter default-value="${project.build.directory}/classes/META-INF/maven/paxexam-config.args"
     */

    private File outputFile;

    /**
     * pax runner arguments defined in <settings> tag in configuration of plugin.
     *
     * @parameter
     */

    private Map<String, String> settings;

    /**
     * @component
     */
    protected ArtifactMetadataSource artifactMetadataSource;

    /**
     * @component
     */
    protected ArtifactResolver resolver;

    protected ArtifactCollector collector = new DefaultArtifactCollector();

    /**
     * @component
     */
    protected ArtifactFactory factory;

    /**
     * @parameter expression="${project.remoteArtifactRepositories}"
     */
    private List<ArtifactRepository> remoteRepositories;

    /**
     * The Maven Session Object
     *
     * @parameter expression="${session}"
     * @required
     * @readonly
     */
    private MavenSession session;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        OutputStream out = null;
        try
        {
            outputFile.getParentFile().mkdirs();
            out = new FileOutputStream( outputFile );
            PrintStream printer = new PrintStream( out );

            List<Dependency> dependencies;
            dependencies = getProvisionableDependencies();

            writeHeader( printer );
            writeProvisioning( printer, dependencies );
            writeSettings( printer, settings );

            getLog().info( "PAX EXAM PLUGIN Created: " + outputFile );
        }
        catch( Exception e )
        {
            throw new MojoExecutionException(
                "Unable to create dependencies file: " + e, e
            );
        }
        finally
        {
            if( out != null )
            {
                try
                {
                    out.close();
                }
                catch( IOException e )
                {
                    getLog().info( "Failed to close: " + outputFile + ". Reason: " + e, e );
                }
            }
        }
    }

    private void writeHeader( PrintStream out )
    {
        out.println( "# Configurations generated by the Pax Exam Maven Plugin" );
        out.println( "# Generated at: " + new Date() );
        out.println();
        out.println( "# groupId = " + project.getGroupId() );
        out.println( "# artifactId = " + project.getArtifactId() );
        out.println( "# version = " + project.getVersion() );
        out.println( "# " +
                     project.getGroupId() + SEPARATOR + project.getArtifactId() + SEPARATOR + "version = "
                     + project.getVersion()
        );
        out.println();
    }

    private void writeSettings( PrintStream out, Map<String, String> settings )
    {
        out.println( "# Settings parsed from pom.xml in settings of plugin" );
        for( String key : settings.keySet() )
        {
            out.println( "--" + key + "=" + settings.get( key ) );
        }
        out.println();
    }

    /**
     * Dependency resolution inspired by servicemix depends-maven-plguin
     *
     * @return list of dependencies to be written to disk.
     */
    private List<Dependency> getProvisionableDependencies()
    {
        List<Dependency> dependencies = new ArrayList<Dependency>();
        for( Dependency d : getDependencies() )
        {
            if( d.getScope() != null && d.getScope().equalsIgnoreCase( "provided" ) )
            {
                dependencies.add( d );
            }
        }

        Collections.sort( dependencies, new Comparator<Dependency>()
        {
            public int compare( Dependency o1, Dependency o2 )
            {
                int result = o1.getGroupId().compareTo( o2.getGroupId() );
                if( result == 0 )
                {
                    result = o1.getArtifactId().compareTo( o2.getArtifactId() );
                    if( result == 0 )
                    {
                        result = o1.getType().compareTo( o2.getType() );
                        if( result == 0 )
                        {
                            if( o1.getClassifier() == null )
                            {
                                if( o2.getClassifier() != null )
                                {
                                    result = 1;
                                }
                            }
                            else
                            {
                                if( o2.getClassifier() != null )
                                {
                                    result = o1.getClassifier().compareTo( o2.getClassifier() );
                                }
                                else
                                {
                                    result = -1;
                                }
                            }
                            if( result == 0 )
                            {
                                result = o1.getVersion().compareTo( o2.getVersion() );
                            }
                        }
                    }
                }
                return result;
            }
        }
        );
        return dependencies;
    }

    @SuppressWarnings( "unchecked" )
    private List<Dependency> getDependencies()
    {
        return (List<Dependency>) project.getDependencies();
    }

    protected void writeProvisioning( PrintStream out, List<Dependency> dependencies )
        throws
        ArtifactResolutionException,
        ArtifactNotFoundException
    {
        out.println( "# provisioning" );
        out.println();

        for( Dependency dependency : dependencies )
        {
            Artifact artifact = factory.createDependencyArtifact(
                dependency.getGroupId(), dependency.getArtifactId(),
                VersionRange.createFromVersion( dependency.getVersion() ),
                dependency.getType(), dependency.getClassifier(), dependency.getScope()
            );
            resolver.resolve( artifact, remoteRepositories, session.getLocalRepository() );
            out.println( artifact.getFile().toURI().normalize().toString() );

            getLog().debug( "Dependency: " + dependency + " classifier: " + dependency.getClassifier() + " type: "
                            + dependency.getType()
            );
        }
        out.println();
    }

}

<commit_msg>PAXEXAM-36 made dependency scope configurable by this:           <dependencyScope>yourScope</dependencyScope> for plugin setting.<commit_after>package org.ops4j.pax.exam.mavenplugin;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactCollector;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.artifact.resolver.DefaultArtifactCollector;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

/**
 * @author <NAME> (tonit)
 * @goal generate-paxexam-config
 * @phase generate-resources
 * @since Mar 17, 2009
 */
public class ExamPlugin extends AbstractMojo
{

    protected static final String SEPARATOR = "/";

    /**
     * The maven project.
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * The file to generate
     *
     * @parameter default-value="${project.build.directory}/classes/META-INF/maven/paxexam-config.args"
     */

    private File outputFile;

    /**
     * @parameter default-value="provided"
     */
    private String dependencyScope;
    /**
     * pax runner arguments defined in <settings> tag in configuration of plugin.
     *
     * @parameter
     */

    private Map<String, String> settings;

    /**
     * @component
     */
    protected ArtifactMetadataSource artifactMetadataSource;

    /**
     * @component
     */
    protected ArtifactResolver resolver;

    protected ArtifactCollector collector = new DefaultArtifactCollector();

    /**
     * @component
     */
    protected ArtifactFactory factory;

    /**
     * @parameter expression="${project.remoteArtifactRepositories}"
     */
    private List<ArtifactRepository> remoteRepositories;

    /**
     * The Maven Session Object
     *
     * @parameter expression="${session}"
     * @required
     * @readonly
     */
    private MavenSession session;

    public void execute()
        throws MojoExecutionException, MojoFailureException
    {
        OutputStream out = null;
        try
        {
            outputFile.getParentFile().mkdirs();
            out = new FileOutputStream( outputFile );
            PrintStream printer = new PrintStream( out );

            List<Dependency> dependencies;
            dependencies = getProvisionableDependencies();

            writeHeader( printer );
            writeProvisioning( printer, dependencies );
            writeSettings( printer, settings );

            getLog().info( "PAX EXAM PLUGIN Created: " + outputFile );
        }
        catch( Exception e )
        {
            throw new MojoExecutionException(
                "Unable to create dependencies file: " + e, e
            );
        }
        finally
        {
            if( out != null )
            {
                try
                {
                    out.close();
                }
                catch( IOException e )
                {
                    getLog().info( "Failed to close: " + outputFile + ". Reason: " + e, e );
                }
            }
        }
    }

    private void writeHeader( PrintStream out )
    {
        out.println( "# Configurations generated by the Pax Exam Maven Plugin" );
        out.println( "# Generated at: " + new Date() );
        out.println();
        out.println( "# groupId = " + project.getGroupId() );
        out.println( "# artifactId = " + project.getArtifactId() );
        out.println( "# version = " + project.getVersion() );
        out.println( "# " +
                     project.getGroupId() + SEPARATOR + project.getArtifactId() + SEPARATOR + "version = "
                     + project.getVersion()
        );
        out.println();
    }

    private void writeSettings( PrintStream out, Map<String, String> settings )
    {
        out.println( "# Settings parsed from pom.xml in settings of plugin" );
        for( String key : settings.keySet() )
        {
            out.println( "--" + key + "=" + settings.get( key ) );
        }
        out.println();
    }

    /**
     * Dependency resolution inspired by servicemix depends-maven-plguin
     *
     * @return list of dependencies to be written to disk.
     */
    private List<Dependency> getProvisionableDependencies()
    {
        List<Dependency> dependencies = new ArrayList<Dependency>();
        getLog().info( "Adding dependencies in scope " + dependencyScope );
        for( Dependency d : getDependencies() )
        {
            if( d.getScope() != null && d.getScope().equalsIgnoreCase( dependencyScope ) )
            {
                dependencies.add( d );
            }
        }

        Collections.sort( dependencies, new Comparator<Dependency>()
        {
            public int compare( Dependency o1, Dependency o2 )
            {
                int result = o1.getGroupId().compareTo( o2.getGroupId() );
                if( result == 0 )
                {
                    result = o1.getArtifactId().compareTo( o2.getArtifactId() );
                    if( result == 0 )
                    {
                        result = o1.getType().compareTo( o2.getType() );
                        if( result == 0 )
                        {
                            if( o1.getClassifier() == null )
                            {
                                if( o2.getClassifier() != null )
                                {
                                    result = 1;
                                }
                            }
                            else
                            {
                                if( o2.getClassifier() != null )
                                {
                                    result = o1.getClassifier().compareTo( o2.getClassifier() );
                                }
                                else
                                {
                                    result = -1;
                                }
                            }
                            if( result == 0 )
                            {
                                result = o1.getVersion().compareTo( o2.getVersion() );
                            }
                        }
                    }
                }
                return result;
            }
        }
        );
        return dependencies;
    }

    @SuppressWarnings( "unchecked" )
    private List<Dependency> getDependencies()
    {
        return (List<Dependency>) project.getDependencies();
    }

    protected void writeProvisioning( PrintStream out, List<Dependency> dependencies )
        throws
        ArtifactResolutionException,
        ArtifactNotFoundException
    {
        out.println( "# provisioning" );
        out.println();

        for( Dependency dependency : dependencies )
        {
            Artifact artifact = factory.createDependencyArtifact(
                dependency.getGroupId(), dependency.getArtifactId(),
                VersionRange.createFromVersion( dependency.getVersion() ),
                dependency.getType(), dependency.getClassifier(), dependency.getScope()
            );
            resolver.resolve( artifact, remoteRepositories, session.getLocalRepository() );
            out.println( artifact.getFile().toURI().normalize().toString() );

            getLog().debug( "Dependency: " + dependency + " classifier: " + dependency.getClassifier() + " type: "
                            + dependency.getType()
            );
        }
        out.println();
    }

}


{'fraction_non_alphanumeric': 0.10289253713392267, 'fraction_numerical': 0.003487882614709243, 'mean_word_length': 1.6426187827745113, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4267523', 'n_tokens_mistral': 4316, 'n_tokens_neox': 4072, 'n_words': 857}