directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Felix Knecht <fel...@apache.org>
Subject Re: svn commit: r698158 - in /directory/studio-plugin/trunk: ./ src/main/java/org/apache/directory/studio/maven/plugins/
Date Tue, 23 Sep 2008 12:38:37 GMT
@Pierre-Arnaud

Can you see if it fits your needs?

mvn studio:eclipse or mvn studio:clean should do the job. I commented
the manifest manipulations in the code. If it works we can drop these
line of codes.
Checkout the studio-plugins latest trunk and build it using

mvn clean install -DskipTests

to get it into your local repository and patch for the studio/pom.xml to
use the snapshot of the plugin

felix@pcfelix ~/svn/apache/directory/studio/trunk $ svn diff pom.xml
Index: pom.xml
===================================================================
--- pom.xml     (revision 697695)
+++ pom.xml     (working copy)
@@ -1042,7 +1042,7 @@
         <plugin>
           <groupId>org.apache.directory.studio</groupId>
           <artifactId>maven-studio-plugin</artifactId>
-          <version>1.0.0</version>
+          <version>1.0.1-SNAPSHOT</version>
         </plugin>
         <plugin>
           <groupId>org.apache.maven.plugins</groupId>

Thanks and regards
Felix


felixk@apache.org schrieb:
> Author: felixk
> Date: Tue Sep 23 05:34:18 2008
> New Revision: 698158
>
> URL: http://svn.apache.org/viewvc?rev=698158&view=rev
> Log:
> Fork the maven goal eclipse:eclipse and add it to the studio:eclipse and studio:clean goal
>
> Modified:
>     directory/studio-plugin/trunk/pom.xml
>     directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/AbstractStudioMojo.java
>     directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java
>     directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java
>
> Modified: directory/studio-plugin/trunk/pom.xml
> URL: http://svn.apache.org/viewvc/directory/studio-plugin/trunk/pom.xml?rev=698158&r1=698157&r2=698158&view=diff
> ==============================================================================
> --- directory/studio-plugin/trunk/pom.xml (original)
> +++ directory/studio-plugin/trunk/pom.xml Tue Sep 23 05:34:18 2008
> @@ -90,6 +90,11 @@
>  
>    <dependencies>
>      <dependency>
> +      <groupId>org.apache.maven.release</groupId>
> +      <artifactId>maven-release-manager</artifactId>
> +      <version>1.0-alpha-4</version>
> +    </dependency>
> +    <dependency>
>        <groupId>commons-lang</groupId>
>        <artifactId>commons-lang</artifactId>
>        <version>2.4</version>
>
> Modified: directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/AbstractStudioMojo.java
> URL: http://svn.apache.org/viewvc/directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/AbstractStudioMojo.java?rev=698158&r1=698157&r2=698158&view=diff
> ==============================================================================
> --- directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/AbstractStudioMojo.java (original)
> +++ directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/AbstractStudioMojo.java Tue Sep 23 05:34:18 2008
> @@ -19,7 +19,6 @@
>   */
>  package org.apache.directory.studio.maven.plugins;
>  
> -
>  import java.io.File;
>  import java.io.IOException;
>  import java.util.ArrayList;
> @@ -41,542 +40,517 @@
>  import org.apache.maven.plugin.AbstractMojo;
>  import org.apache.maven.plugin.MojoExecutionException;
>  import org.apache.maven.project.MavenProject;
> +import org.apache.maven.shared.release.exec.CommandLineFactory;
> +import org.apache.maven.shared.release.exec.DefaultCommandLineFactory;
> +import org.apache.maven.shared.release.exec.ForkedMavenExecutor;
> +import org.apache.maven.shared.release.exec.MavenExecutorException;
> +import org.apache.maven.shared.release.exec.TeeOutputStream;
>  import org.codehaus.plexus.archiver.Archiver;
>  import org.codehaus.plexus.archiver.ArchiverException;
>  import org.codehaus.plexus.archiver.UnArchiver;
>  import org.codehaus.plexus.archiver.manager.ArchiverManager;
>  import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
> -
> +import org.codehaus.plexus.util.cli.CommandLineException;
> +import org.codehaus.plexus.util.cli.Commandline;
>  
>  /**
>   * The abstract studio mojo
>   * 
>   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
>   */
> -public abstract class AbstractStudioMojo extends AbstractMojo
> -{
> +public abstract class AbstractStudioMojo extends AbstractMojo {
>  
> -    /**
> -     * To look up Archiver/UnArchiver implementations
> -     * 
> -     * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
> -     * @required
> -     * @readonly
> -     */
> -    protected ArchiverManager archiverManager;
> -
> -    /**
> -     * Location of the file.
> -     * 
> -     * @parameter expression="${project.build.directory}"
> -     * @required
> -     * @readonly
> -     */
> -    protected String buildDirectory;
> -
> -    /**
> -     * Used to look up Artifacts in the remote repository.
> -     * 
> -     * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
> -     * @required
> -     * @readonly
> -     */
> -    protected org.apache.maven.artifact.factory.ArtifactFactory factory;
> -
> -    /**
> -     * Used to look up Artifacts in the remote repository.
> -     * 
> -     * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
> -     * @required
> -     * @readonly
> -     */
> -    protected org.apache.maven.artifact.resolver.ArtifactResolver resolver;
> -
> -    /**
> -     * Flag if execution shall be skipped. Defaults to true.
> -     * 
> -     * @parameter expression="true"
> -     * @required
> -     */
> -    protected boolean skip;
> -
> -    /**
> -     * Relativ Path to copy libraries to. Defaults to lib
> -     * 
> -     * @parameter expression="lib"
> -     * @required
> -     * @readonly
> -     */
> -    protected String libraryPath;
> -
> -    /**
> -     * Artifact collector, needed to resolve dependencies.
> -     * 
> -     * @component role="org.apache.maven.artifact.resolver.ArtifactCollector"
> -     * @required
> -     * @readonly
> -     */
> -    protected ArtifactCollector artifactCollector;
> -
> -    /**
> -     * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
> -     *            hint="maven"
> -     * @required
> -     * @readonly
> -     */
> -    protected ArtifactMetadataSource artifactMetadataSource;
> -
> -    /**
> -     * Location of the local repository.
> -     * 
> -     * @parameter expression="${localRepository}"
> -     * @readonly
> -     * @required
> -     */
> -    protected org.apache.maven.artifact.repository.ArtifactRepository local;
> -
> -    /**
> -     * List of Remote Repositories used by the resolver
> -     * 
> -     * @parameter expression="${project.remoteArtifactRepositories}"
> -     * @readonly
> -     * @required
> -     */
> -    protected List<ArtifactRepository> remoteRepos;
> -
> -    /**
> -     * POM
> -     * 
> -     * @parameter expression="${project}"
> -     * @readonly
> -     * @required
> -     */
> -    protected MavenProject project;
> -
> -    /**
> -     * Output absolute filename for resolved artifacts
> -     * 
> -     * @optional
> -     * @since 2.0
> -     * @parameter expression="${outputAbsoluteArtifactFilename}"
> -     *            default-value="false"
> -     */
> -    protected boolean outputAbsoluteArtifactFilename;
> -
> -
> -    /**
> -     * Unpack a file to a location
> -     * 
> -     * @param location
> -     *            The location to unpack the file to
> -     * @param file
> -     *            The file to unpack
> -     */
> -    protected void unpackToLocation( final File location, final File file ) throws Exception
> -    {
> -        try
> -        {
> -            getLog().info( "Unpacking " + file + " to\n                 " + location );
> -            location.mkdirs();
> -            UnArchiver unArchiver = archiverManager.getUnArchiver( file );
> -            unArchiver.setSourceFile( file );
> -            unArchiver.setDestDirectory( location );
> -            unArchiver.extract();
> -        }
> -        catch ( NoSuchArchiverException e )
> -        {
> -            throw new MojoExecutionException( "Unknown archiver type", e );
> -        }
> -        catch ( ArchiverException e )
> -        {
> -            e.printStackTrace();
> -            throw new MojoExecutionException( "Error unpacking file: " + file + " to: " + location + "\r\n"
> -                + e.toString(), e );
> -        }
> -    }
> -
> -
> -    /**
> -     * Pack a given location into a file
> -     * 
> -     * @param location
> -     *            A location to pack
> -     * @param file
> -     *            The file to pack the location into
> -     * @throws Exception
> -     *             If an error occurs
> -     */
> -    protected void packFromLocation( final File location, final File file ) throws MojoExecutionException
> -    {
> -        try
> -        {
> -            getLog().info( "Packing " + location + " to\n               " + file );
> -            Archiver archiver = archiverManager.getArchiver( file );
> -            archiver.setDestFile( file );
> -            archiver.setIncludeEmptyDirs( true );
> -            archiver.addDirectory( location );
> -            archiver.createArchive();
> -        }
> -        catch ( NoSuchArchiverException e )
> -        {
> -            throw new MojoExecutionException( "Unknown archiver type", e );
> -        }
> -        catch ( IOException e )
> -        {
> -            throw new MojoExecutionException( "Error creating archive", e );
> -        }
> -        catch ( ArchiverException e )
> -        {
> -            e.printStackTrace();
> -            throw new MojoExecutionException( "Error packing file: " + file + " to: " + location + "\r\n"
> -                + e.toString(), e );
> -        }
> -    }
> -
> -
> -    /**
> -     * Resolves the Artifact from the remote repository if nessessary. If no
> -     * version is specified, it will be retrieved from the dependency list or
> -     * from the DependencyManagement section of the pom.
> -     * 
> -     * @param artifactItem
> -     *            containing information about artifact from plugin
> -     *            configuration.
> -     * @return Artifact object representing the specified file.
> -     * 
> -     * @throws MojoExecutionException
> -     *             with a message if the version can't be found in
> -     *             DependencyManagement.
> -     */
> -    protected Artifact getArtifact( ArtifactItem artifactItem ) throws MojoExecutionException
> -    {
> -        Artifact artifact;
> -
> -        VersionRange vr;
> -        try
> -        {
> -            vr = VersionRange.createFromVersionSpec( artifactItem.getVersion() );
> -        }
> -        catch ( InvalidVersionSpecificationException e1 )
> -        {
> -            e1.printStackTrace();
> -            vr = VersionRange.createFromVersion( artifactItem.getVersion() );
> -        }
> -
> -        if ( StringUtils.isEmpty( artifactItem.getClassifier() ) )
> -        {
> -            artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
> -                artifactItem.getType(), null, Artifact.SCOPE_COMPILE );
> -        }
> -        else
> -        {
> -            artifact = factory.createDependencyArtifact( artifactItem.getGroupId(), artifactItem.getArtifactId(), vr,
> -                artifactItem.getType(), artifactItem.getClassifier(), Artifact.SCOPE_COMPILE );
> -        }
> -
> -        try
> -        {
> -            resolver.resolve( artifact, remoteRepos, local );
> -        }
> -        catch ( ArtifactResolutionException e )
> -        {
> -            throw new MojoExecutionException( "Unable to resolve artifact.", e );
> -        }
> -        catch ( ArtifactNotFoundException e )
> -        {
> -            throw new MojoExecutionException( "Unable to find artifact.", e );
> -        }
> -
> -        return artifact;
> -    }
> -
> -
> -    /**
> -     * Tries to find missing version from dependancy list and dependency
> -     * management. If found, the artifact is updated with the correct version.
> -     * 
> -     * It will first look for an exact match on
> -     * artifactId/groupId/classifier/type and if it doesn't find a match, it
> -     * will try again looking for artifactId and groupId only.
> -     * 
> -     * @param artifact
> -     *            representing configured file.
> -     * @throws MojoExecutionException
> -     */
> -    protected void fillMissingArtifactVersion( ArtifactItem artifact ) throws MojoExecutionException
> -    {
> -        if ( !findDependencyVersion( artifact, project.getDependencies(), false )
> -            && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
> -                .getDependencyManagement().getDependencies(), false ) )
> -            && !findDependencyVersion( artifact, project.getDependencies(), true )
> -            && ( project.getDependencyManagement() == null || !findDependencyVersion( artifact, project
> -                .getDependencyManagement().getDependencies(), true ) ) )
> -        {
> -            throw new MojoExecutionException( "Unable to find artifact version of " + artifact.getGroupId() + ":"
> -                + artifact.getArtifactId() + " in either dependency list or in project's dependency management." );
> -        }
> -    }
> -
> -
> -    /**
> -     * Tries to find missing version from a list of dependencies. If found, the
> -     * artifact is updated with the correct version.
> -     * 
> -     * @param artifact
> -     *            representing configured file.
> -     * @param list
> -     *            list of dependencies to search.
> -     * @param looseMatch
> -     *            only look at artifactId and groupId
> -     * @return the found dependency
> -     */
> -    protected boolean findDependencyVersion( ArtifactItem artifact, List<Dependency> list, boolean looseMatch )
> -    {
> -        boolean result = false;
> -
> -        for ( int i = 0; i < list.size(); i++ )
> -        {
> -            Dependency dependency = list.get( i );
> -            if ( StringUtils.equals( dependency.getArtifactId(), artifact.getArtifactId() )
> -                && StringUtils.equals( dependency.getGroupId(), artifact.getGroupId() )
> -                && ( looseMatch || StringUtils.equals( dependency.getClassifier(), artifact.getClassifier() ) )
> -                && ( looseMatch || StringUtils.equals( dependency.getType(), artifact.getType() ) ) )
> -            {
> -
> -                artifact.setVersion( dependency.getVersion() );
> -                result = true;
> -                break;
> -            }
> -        }
> -        return result;
> -    }
> -
> -
> -    /**
> -     * Complete the artifacts in the artifactItems list (e.g. complete with
> -     * version number)
> -     * 
> -     * @throws MojoExecutionException
> -     */
> -    protected void completeArtifactItems( List<ArtifactItem> artifactItems ) throws MojoExecutionException
> -    {
> -        try
> -        {
> -            // Get and complete artifacts
> -            for ( Iterator<ArtifactItem> artifactItem = artifactItems.iterator(); artifactItem.hasNext(); )
> -            {
> -                ArtifactItem item = artifactItem.next();
> -                // make sure we have a version.
> -                if ( StringUtils.isEmpty( item.getVersion() ) )
> -                {
> -                    fillMissingArtifactVersion( item );
> -                }
> -                item.setArtifact( this.getArtifact( item ) );
> -            }
> -        }
> -        catch ( Exception e )
> -        {
> -            throw new MojoExecutionException( "", e );
> -        }
> -    }
> -
> -
> -    /**
> -     * Delete a directory
> -     * 
> -     * @param path
> -     * @return
> -     */
> -    protected static boolean deleteDirectory( File path )
> -    {
> -        if ( path.exists() && path.isDirectory() )
> -        {
> -            for ( File file : path.listFiles() )
> -            {
> -                if ( file.isDirectory() )
> -                {
> -                    deleteDirectory( file );
> -                }
> -                else
> -                {
> -                    file.delete();
> -                }
> -            }
> -        }
> -        return ( path.delete() );
> -    }
> -
> -
> -    /**
> -     * Return a list of artifacts nonscoped "provided"
> -     * 
> -     * @return
> -     */
> -    protected List<Artifact> createArtifactList()
> -    {
> -        List<Artifact> list = new ArrayList<Artifact>();
> -        
> -        // Copying only artifacts with 'provided' scope
> -        for ( Iterator<Artifact> artifactItem = project.getArtifacts().iterator(); artifactItem.hasNext(); )
> -        {
> -            Artifact artifact = ( Artifact ) artifactItem.next();
> -            if ( !artifact.getScope().equalsIgnoreCase( "provided" ) )
> -            {
> -                list.add( artifact );
> -            }
> -        }
> -
> -        // Sorting list before returning it
> -        Collections.sort( list, new Comparator<Artifact>()
> -        {
> -            public int compare( Artifact o1, Artifact o2 )
> -            {
> -                String artifactId1 = o1.getArtifactId();
> -                String artifactId2 = o2.getArtifactId();
> -
> -                if ( ( artifactId1 != null ) && ( artifactId2 != null ) )
> -                {
> -                    return artifactId1.compareToIgnoreCase( artifactId2 );
> -                }
> -
> -                // Default
> -                return o1.toString().compareToIgnoreCase( o2.toString() );
> -            }
> -        } );
> -        
> -        return list;
> -    }
> -
> -
> -    /**
> -     * @return Returns the factory.
> -     */
> -    public org.apache.maven.artifact.factory.ArtifactFactory getFactory()
> -    {
> -        return this.factory;
> -    }
> -
> -
> -    /**
> -     * @param factory
> -     *            The factory to set.
> -     */
> -    public void setFactory( org.apache.maven.artifact.factory.ArtifactFactory factory )
> -    {
> -        this.factory = factory;
> -    }
> -
> -
> -    /**
> -     * @return Returns the project.
> -     */
> -    public MavenProject getProject()
> -    {
> -        return this.project;
> -    }
> -
> -
> -    /**
> -     * @return Returns the local.
> -     */
> -    public org.apache.maven.artifact.repository.ArtifactRepository getLocal()
> -    {
> -        return this.local;
> -    }
> -
> -
> -    /**
> -     * @param local
> -     *            The local to set.
> -     */
> -    public void setLocal( org.apache.maven.artifact.repository.ArtifactRepository local )
> -    {
> -        this.local = local;
> -    }
> -
> -
> -    /**
> -     * @return Returns the remoteRepos.
> -     */
> -    public List<ArtifactRepository> getRemoteRepos()
> -    {
> -        return this.remoteRepos;
> -    }
> -
> -
> -    /**
> -     * @param remoteRepos
> -     *            The remoteRepos to set.
> -     */
> -    public void setRemoteRepos( List<ArtifactRepository> remoteRepos )
> -    {
> -        this.remoteRepos = remoteRepos;
> -    }
> -
> -
> -    /**
> -     * @return Returns the resolver.
> -     */
> -    public org.apache.maven.artifact.resolver.ArtifactResolver getResolver()
> -    {
> -        return this.resolver;
> -    }
> -
> -
> -    /**
> -     * @param resolver
> -     *            The resolver to set.
> -     */
> -    public void setResolver( org.apache.maven.artifact.resolver.ArtifactResolver resolver )
> -    {
> -        this.resolver = resolver;
> -    }
> -
> -
> -    /**
> -     * @return Returns the artifactCollector.
> -     */
> -    public ArtifactCollector getArtifactCollector()
> -    {
> -        return this.artifactCollector;
> -    }
> -
> -
> -    /**
> -     * @param theArtifactCollector
> -     *            The artifactCollector to set.
> -     */
> -    public void setArtifactCollector( ArtifactCollector theArtifactCollector )
> -    {
> -        this.artifactCollector = theArtifactCollector;
> -    }
> -
> -
> -    /**
> -     * @return Returns the artifactMetadataSource.
> -     */
> -    public ArtifactMetadataSource getArtifactMetadataSource()
> -    {
> -        return this.artifactMetadataSource;
> -    }
> -
> -
> -    /**
> -     * @param theArtifactMetadataSource
> -     *            The artifactMetadataSource to set.
> -     */
> -    public void setArtifactMetadataSource( ArtifactMetadataSource theArtifactMetadataSource )
> -    {
> -        this.artifactMetadataSource = theArtifactMetadataSource;
> -    }
> -
> -
> -    /**
> -     * @param libraryPath
> -     *            the libraryPath to set
> -     */
> -    public void setLibraryPath( String pLibraryPath )
> -    {
> -        this.libraryPath = pLibraryPath;
> -    }
> +	/**
> +	 * To look up Archiver/UnArchiver implementations
> +	 * 
> +	 * @parameter expression="${component.org.codehaus.plexus.archiver.manager.ArchiverManager}"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected ArchiverManager archiverManager;
> +
> +	/**
> +	 * Location of the file.
> +	 * 
> +	 * @parameter expression="${project.build.directory}"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected String buildDirectory;
> +
> +	/**
> +	 * Used to look up Artifacts in the remote repository.
> +	 * 
> +	 * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected org.apache.maven.artifact.factory.ArtifactFactory factory;
> +
> +	/**
> +	 * Used to look up Artifacts in the remote repository.
> +	 * 
> +	 * @parameter expression="${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected org.apache.maven.artifact.resolver.ArtifactResolver resolver;
> +
> +	/**
> +	 * Flag if execution shall be skipped. Defaults to true.
> +	 * 
> +	 * @parameter expression="true"
> +	 * @required
> +	 */
> +	protected boolean skip;
> +
> +	/**
> +	 * Relativ Path to copy libraries to. Defaults to lib
> +	 * 
> +	 * @parameter expression="lib"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected String libraryPath;
> +
> +	/**
> +	 * Artifact collector, needed to resolve dependencies.
> +	 * 
> +	 * @component role="org.apache.maven.artifact.resolver.ArtifactCollector"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected ArtifactCollector artifactCollector;
> +
> +	/**
> +	 * @component role="org.apache.maven.artifact.metadata.ArtifactMetadataSource"
> +	 *            hint="maven"
> +	 * @required
> +	 * @readonly
> +	 */
> +	protected ArtifactMetadataSource artifactMetadataSource;
> +
> +	/**
> +	 * Location of the local repository.
> +	 * 
> +	 * @parameter expression="${localRepository}"
> +	 * @readonly
> +	 * @required
> +	 */
> +	protected org.apache.maven.artifact.repository.ArtifactRepository local;
> +
> +	/**
> +	 * List of Remote Repositories used by the resolver
> +	 * 
> +	 * @parameter expression="${project.remoteArtifactRepositories}"
> +	 * @readonly
> +	 * @required
> +	 */
> +	protected List<ArtifactRepository> remoteRepos;
> +
> +	/**
> +	 * POM
> +	 * 
> +	 * @parameter expression="${project}"
> +	 * @readonly
> +	 * @required
> +	 */
> +	protected MavenProject project;
> +
> +	/**
> +	 * Output absolute filename for resolved artifacts
> +	 * 
> +	 * @optional
> +	 * @since 2.0
> +	 * @parameter expression="${outputAbsoluteArtifactFilename}"
> +	 *            default-value="false"
> +	 */
> +	protected boolean outputAbsoluteArtifactFilename;
> +
> +	/**
> +	 * Unpack a file to a location
> +	 * 
> +	 * @param location
> +	 *            The location to unpack the file to
> +	 * @param file
> +	 *            The file to unpack
> +	 */
> +	protected void unpackToLocation(final File location, final File file)
> +			throws Exception {
> +		try {
> +			getLog().info(
> +					"Unpacking " + file + " to\n                 " + location);
> +			location.mkdirs();
> +			UnArchiver unArchiver = archiverManager.getUnArchiver(file);
> +			unArchiver.setSourceFile(file);
> +			unArchiver.setDestDirectory(location);
> +			unArchiver.extract();
> +		} catch (NoSuchArchiverException e) {
> +			throw new MojoExecutionException("Unknown archiver type", e);
> +		} catch (ArchiverException e) {
> +			e.printStackTrace();
> +			throw new MojoExecutionException("Error unpacking file: " + file
> +					+ " to: " + location + "\r\n" + e.toString(), e);
> +		}
> +	}
> +
> +	/**
> +	 * Pack a given location into a file
> +	 * 
> +	 * @param location
> +	 *            A location to pack
> +	 * @param file
> +	 *            The file to pack the location into
> +	 * @throws Exception
> +	 *             If an error occurs
> +	 */
> +	protected void packFromLocation(final File location, final File file)
> +			throws MojoExecutionException {
> +		try {
> +			getLog()
> +					.info("Packing " + location + " to\n               " + file);
> +			Archiver archiver = archiverManager.getArchiver(file);
> +			archiver.setDestFile(file);
> +			archiver.setIncludeEmptyDirs(true);
> +			archiver.addDirectory(location);
> +			archiver.createArchive();
> +		} catch (NoSuchArchiverException e) {
> +			throw new MojoExecutionException("Unknown archiver type", e);
> +		} catch (IOException e) {
> +			throw new MojoExecutionException("Error creating archive", e);
> +		} catch (ArchiverException e) {
> +			e.printStackTrace();
> +			throw new MojoExecutionException("Error packing file: " + file
> +					+ " to: " + location + "\r\n" + e.toString(), e);
> +		}
> +	}
> +
> +	/**
> +	 * Fork a maven goal
> +	 * 
> +	 * @param goal
> +	 *            the goal
> +	 * @return the result
> +	 * @throws MavenExecutorException
> +	 * @throws CommandLineException
> +	 */
> +	protected int forkMvnGoal(final String goal) throws MavenExecutorException,
> +			CommandLineException {
> +		CommandLineFactory commandLineFactory = new DefaultCommandLineFactory();
> +		Commandline cl = commandLineFactory.createCommandLine("mvn");
> +
> +		cl.setWorkingDirectory(project.getBasedir().getAbsolutePath());
> +
> +		cl.addEnvironment("MAVEN_TERMINATE_CMD", "on");
> +		cl.createArg().setValue(goal);
> +		cl.createArg().setValue("--no-plugin-updates");
> +		cl.createArg().setValue("--batch-mode");
> +		TeeOutputStream stdOut = new TeeOutputStream(System.out);
> +		TeeOutputStream stdErr = new TeeOutputStream(System.err);
> +		return ForkedMavenExecutor.executeCommandLine(cl, System.in, stdOut,
> +				stdErr);
> +
> +	}
> +
> +	/**
> +	 * Resolves the Artifact from the remote repository if nessessary. If no
> +	 * version is specified, it will be retrieved from the dependency list or
> +	 * from the DependencyManagement section of the pom.
> +	 * 
> +	 * @param artifactItem
> +	 *            containing information about artifact from plugin
> +	 *            configuration.
> +	 * @return Artifact object representing the specified file.
> +	 * 
> +	 * @throws MojoExecutionException
> +	 *             with a message if the version can't be found in
> +	 *             DependencyManagement.
> +	 */
> +	protected Artifact getArtifact(ArtifactItem artifactItem)
> +			throws MojoExecutionException {
> +		Artifact artifact;
> +
> +		VersionRange vr;
> +		try {
> +			vr = VersionRange.createFromVersionSpec(artifactItem.getVersion());
> +		} catch (InvalidVersionSpecificationException e1) {
> +			e1.printStackTrace();
> +			vr = VersionRange.createFromVersion(artifactItem.getVersion());
> +		}
> +
> +		if (StringUtils.isEmpty(artifactItem.getClassifier())) {
> +			artifact = factory.createDependencyArtifact(artifactItem
> +					.getGroupId(), artifactItem.getArtifactId(), vr,
> +					artifactItem.getType(), null, Artifact.SCOPE_COMPILE);
> +		} else {
> +			artifact = factory.createDependencyArtifact(artifactItem
> +					.getGroupId(), artifactItem.getArtifactId(), vr,
> +					artifactItem.getType(), artifactItem.getClassifier(),
> +					Artifact.SCOPE_COMPILE);
> +		}
> +
> +		try {
> +			resolver.resolve(artifact, remoteRepos, local);
> +		} catch (ArtifactResolutionException e) {
> +			throw new MojoExecutionException("Unable to resolve artifact.", e);
> +		} catch (ArtifactNotFoundException e) {
> +			throw new MojoExecutionException("Unable to find artifact.", e);
> +		}
> +
> +		return artifact;
> +	}
> +
> +	/**
> +	 * Tries to find missing version from dependancy list and dependency
> +	 * management. If found, the artifact is updated with the correct version.
> +	 * 
> +	 * It will first look for an exact match on
> +	 * artifactId/groupId/classifier/type and if it doesn't find a match, it
> +	 * will try again looking for artifactId and groupId only.
> +	 * 
> +	 * @param artifact
> +	 *            representing configured file.
> +	 * @throws MojoExecutionException
> +	 */
> +	protected void fillMissingArtifactVersion(ArtifactItem artifact)
> +			throws MojoExecutionException {
> +		if (!findDependencyVersion(artifact, project.getDependencies(), false)
> +				&& (project.getDependencyManagement() == null || !findDependencyVersion(
> +						artifact, project.getDependencyManagement()
> +								.getDependencies(), false))
> +				&& !findDependencyVersion(artifact, project.getDependencies(),
> +						true)
> +				&& (project.getDependencyManagement() == null || !findDependencyVersion(
> +						artifact, project.getDependencyManagement()
> +								.getDependencies(), true))) {
> +			throw new MojoExecutionException(
> +					"Unable to find artifact version of "
> +							+ artifact.getGroupId()
> +							+ ":"
> +							+ artifact.getArtifactId()
> +							+ " in either dependency list or in project's dependency management.");
> +		}
> +	}
> +
> +	/**
> +	 * Tries to find missing version from a list of dependencies. If found, the
> +	 * artifact is updated with the correct version.
> +	 * 
> +	 * @param artifact
> +	 *            representing configured file.
> +	 * @param list
> +	 *            list of dependencies to search.
> +	 * @param looseMatch
> +	 *            only look at artifactId and groupId
> +	 * @return the found dependency
> +	 */
> +	protected boolean findDependencyVersion(ArtifactItem artifact,
> +			List<Dependency> list, boolean looseMatch) {
> +		boolean result = false;
> +
> +		for (int i = 0; i < list.size(); i++) {
> +			Dependency dependency = list.get(i);
> +			if (StringUtils.equals(dependency.getArtifactId(), artifact
> +					.getArtifactId())
> +					&& StringUtils.equals(dependency.getGroupId(), artifact
> +							.getGroupId())
> +					&& (looseMatch || StringUtils.equals(dependency
> +							.getClassifier(), artifact.getClassifier()))
> +					&& (looseMatch || StringUtils.equals(dependency.getType(),
> +							artifact.getType()))) {
> +
> +				artifact.setVersion(dependency.getVersion());
> +				result = true;
> +				break;
> +			}
> +		}
> +		return result;
> +	}
> +
> +	/**
> +	 * Complete the artifacts in the artifactItems list (e.g. complete with
> +	 * version number)
> +	 * 
> +	 * @throws MojoExecutionException
> +	 */
> +	protected void completeArtifactItems(List<ArtifactItem> artifactItems)
> +			throws MojoExecutionException {
> +		try {
> +			// Get and complete artifacts
> +			for (Iterator<ArtifactItem> artifactItem = artifactItems.iterator(); artifactItem
> +					.hasNext();) {
> +				ArtifactItem item = artifactItem.next();
> +				// make sure we have a version.
> +				if (StringUtils.isEmpty(item.getVersion())) {
> +					fillMissingArtifactVersion(item);
> +				}
> +				item.setArtifact(this.getArtifact(item));
> +			}
> +		} catch (Exception e) {
> +			throw new MojoExecutionException("", e);
> +		}
> +	}
> +
> +	/**
> +	 * Delete a directory
> +	 * 
> +	 * @param path
> +	 * @return
> +	 */
> +	protected static boolean deleteDirectory(File path) {
> +		if (path.exists() && path.isDirectory()) {
> +			for (File file : path.listFiles()) {
> +				if (file.isDirectory()) {
> +					deleteDirectory(file);
> +				} else {
> +					file.delete();
> +				}
> +			}
> +		}
> +		return (path.delete());
> +	}
> +
> +	/**
> +	 * Return a list of artifacts nonscoped "provided"
> +	 * 
> +	 * @return
> +	 */
> +	protected List<Artifact> createArtifactList() {
> +		List<Artifact> list = new ArrayList<Artifact>();
> +
> +		// Copying only artifacts with 'provided' scope
> +		for (Iterator<Artifact> artifactItem = project.getArtifacts()
> +				.iterator(); artifactItem.hasNext();) {
> +			Artifact artifact = (Artifact) artifactItem.next();
> +			if (!artifact.getScope().equalsIgnoreCase("provided")) {
> +				list.add(artifact);
> +			}
> +		}
> +
> +		// Sorting list before returning it
> +		Collections.sort(list, new Comparator<Artifact>() {
> +			public int compare(Artifact o1, Artifact o2) {
> +				String artifactId1 = o1.getArtifactId();
> +				String artifactId2 = o2.getArtifactId();
> +
> +				if ((artifactId1 != null) && (artifactId2 != null)) {
> +					return artifactId1.compareToIgnoreCase(artifactId2);
> +				}
> +
> +				// Default
> +				return o1.toString().compareToIgnoreCase(o2.toString());
> +			}
> +		});
> +
> +		return list;
> +	}
> +
> +	/**
> +	 * @return Returns the factory.
> +	 */
> +	public org.apache.maven.artifact.factory.ArtifactFactory getFactory() {
> +		return this.factory;
> +	}
> +
> +	/**
> +	 * @param factory
> +	 *            The factory to set.
> +	 */
> +	public void setFactory(
> +			org.apache.maven.artifact.factory.ArtifactFactory factory) {
> +		this.factory = factory;
> +	}
> +
> +	/**
> +	 * @return Returns the project.
> +	 */
> +	public MavenProject getProject() {
> +		return this.project;
> +	}
> +
> +	/**
> +	 * @return Returns the local.
> +	 */
> +	public org.apache.maven.artifact.repository.ArtifactRepository getLocal() {
> +		return this.local;
> +	}
> +
> +	/**
> +	 * @param local
> +	 *            The local to set.
> +	 */
> +	public void setLocal(
> +			org.apache.maven.artifact.repository.ArtifactRepository local) {
> +		this.local = local;
> +	}
> +
> +	/**
> +	 * @return Returns the remoteRepos.
> +	 */
> +	public List<ArtifactRepository> getRemoteRepos() {
> +		return this.remoteRepos;
> +	}
> +
> +	/**
> +	 * @param remoteRepos
> +	 *            The remoteRepos to set.
> +	 */
> +	public void setRemoteRepos(List<ArtifactRepository> remoteRepos) {
> +		this.remoteRepos = remoteRepos;
> +	}
> +
> +	/**
> +	 * @return Returns the resolver.
> +	 */
> +	public org.apache.maven.artifact.resolver.ArtifactResolver getResolver() {
> +		return this.resolver;
> +	}
> +
> +	/**
> +	 * @param resolver
> +	 *            The resolver to set.
> +	 */
> +	public void setResolver(
> +			org.apache.maven.artifact.resolver.ArtifactResolver resolver) {
> +		this.resolver = resolver;
> +	}
> +
> +	/**
> +	 * @return Returns the artifactCollector.
> +	 */
> +	public ArtifactCollector getArtifactCollector() {
> +		return this.artifactCollector;
> +	}
> +
> +	/**
> +	 * @param theArtifactCollector
> +	 *            The artifactCollector to set.
> +	 */
> +	public void setArtifactCollector(ArtifactCollector theArtifactCollector) {
> +		this.artifactCollector = theArtifactCollector;
> +	}
> +
> +	/**
> +	 * @return Returns the artifactMetadataSource.
> +	 */
> +	public ArtifactMetadataSource getArtifactMetadataSource() {
> +		return this.artifactMetadataSource;
> +	}
> +
> +	/**
> +	 * @param theArtifactMetadataSource
> +	 *            The artifactMetadataSource to set.
> +	 */
> +	public void setArtifactMetadataSource(
> +			ArtifactMetadataSource theArtifactMetadataSource) {
> +		this.artifactMetadataSource = theArtifactMetadataSource;
> +	}
> +
> +	/**
> +	 * @param libraryPath
> +	 *            the libraryPath to set
> +	 */
> +	public void setLibraryPath(String pLibraryPath) {
> +		this.libraryPath = pLibraryPath;
> +	}
>  }
> \ No newline at end of file
>
> Modified: directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java
> URL: http://svn.apache.org/viewvc/directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java?rev=698158&r1=698157&r2=698158&view=diff
> ==============================================================================
> --- directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java (original)
> +++ directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioCleanMojo.java Tue Sep 23 05:34:18 2008
> @@ -19,12 +19,10 @@
>   */
>  package org.apache.directory.studio.maven.plugins;
>  
> -
>  import java.io.File;
>  
>  import org.apache.maven.plugin.MojoExecutionException;
>  
> -
>  /**
>   * Clean stuff generated by studio:eclipse
>   * 
> @@ -35,29 +33,32 @@
>   * 
>   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
>   */
> -public class StudioCleanMojo extends AbstractStudioMojo
> -{
> +public class StudioCleanMojo extends AbstractStudioMojo {
> +
> +	private final String MAVEN_ECLIPSE_XML = "maven-eclipse.xml";
> +	private final String EXTERNAL_TOOL_BUILDERS_DIR = ".externalToolBuilders";
>  
> -    private final String MAVEN_ECLIPSE_XML = "maven-eclipse.xml";
> -    private final String EXTERNAL_TOOL_BUILDERS_DIR = ".externalToolBuilders";
> +	public void execute() throws MojoExecutionException {
>  
> -    public void execute() throws MojoExecutionException
> -    {
> -        try
> -        {
> -            File file = new File( project.getBasedir(), MAVEN_ECLIPSE_XML );
> -            getLog().info( "Deleting " + file );
> -            file.delete();
> -            file = new File( project.getBasedir(), EXTERNAL_TOOL_BUILDERS_DIR );
> -            getLog().info( "Deleting " + file );
> -            deleteDirectory( file );
> -            file = new File( project.getBasedir(), libraryPath );
> -            getLog().info( "Deleting " + file );
> -            deleteDirectory( file );
> -        }
> -        catch ( Exception e )
> -        {
> -            getLog().error( e );
> -        }
> -    }
> +		if (project.isExecutionRoot()) {
> +			try {
> +				forkMvnGoal("eclipse:clean");
> +			} catch (Exception e) {
> +				throw new MojoExecutionException(e.getMessage());
> +			}
> +		}
> +		try {
> +			File file = new File(project.getBasedir(), MAVEN_ECLIPSE_XML);
> +			getLog().info("Deleting " + file);
> +			file.delete();
> +			file = new File(project.getBasedir(), EXTERNAL_TOOL_BUILDERS_DIR);
> +			getLog().info("Deleting " + file);
> +			deleteDirectory(file);
> +			file = new File(project.getBasedir(), libraryPath);
> +			getLog().info("Deleting " + file);
> +			deleteDirectory(file);
> +		} catch (Exception e) {
> +			getLog().error(e);
> +		}
> +	}
>  }
>
> Modified: directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java
> URL: http://svn.apache.org/viewvc/directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java?rev=698158&r1=698157&r2=698158&view=diff
> ==============================================================================
> --- directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java (original)
> +++ directory/studio-plugin/trunk/src/main/java/org/apache/directory/studio/maven/plugins/StudioEclipseMojo.java Tue Sep 23 05:34:18 2008
> @@ -19,7 +19,6 @@
>   */
>  package org.apache.directory.studio.maven.plugins;
>  
> -
>  import java.io.BufferedReader;
>  import java.io.File;
>  import java.io.FileInputStream;
> @@ -43,9 +42,8 @@
>  import org.codehaus.plexus.util.xml.Xpp3DomWriter;
>  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
>  
> -
>  /**
> - * Prepares for eclipse: 
> + * Prepares for eclipse:
>   * <p>
>   * <ul>
>   * <li>Copy artifacts nonscoped "provided" to ${basedir}/lib</li>
> @@ -65,300 +63,280 @@
>   * 
>   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
>   */
> -public class StudioEclipseMojo extends AbstractStudioMojo
> -{
> +public class StudioEclipseMojo extends AbstractStudioMojo {
>  
> -    /**
> -     * Constant used for newline.
> -     */
> -    private static final String NEWLINE = "\n";
> -
> -    /**
> -     * Bundle-ClassPath: updated with the list of dependencies.
> -     */
> -    public final static String ENTRY_BUNDLE_CLASSPATH = "Bundle-ClassPath:";
> -
> -    /**
> -     * Bundle symbolic name: updated with the artifact id.
> -     */
> -    public final static String ENTRY_BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName:";
> -
> -    /**
> -     * Bundle version: updated with the project version.
> -     */
> -    public final static String ENTRY_BUNDLE_VERSION = "Bundle-Version:";
> -
> -
> -    public void execute() throws MojoExecutionException
> -    {
> -        if ( !skip )
> -        {
> -            try
> -            {
> -                // Create list of used artifacts
> -                final List<Artifact> artifactList = createArtifactList();
> -
> -                // copy Artifacts
> -                copyArtifacts( artifactList );
> -
> -                // Update Bundle-Classpath in MANIFEST.MF
> -                updateManifest( artifactList );
> -
> -                // Update .classpath
> -                updateDotClasspath( artifactList );
> -
> -                updateDotProject();
> -                removeMavenEclipseXml();
> -                removeDotExternalToolBuilders();
> -
> -            }
> -            catch ( FileNotFoundException e )
> -            {
> -                getLog().error(
> -                    "Please run eclipse:eclipse first to create .classpath, e.g. mvn eclipse:eclipse studio:eclipse.",
> -                    e );
> -            }
> -            catch ( Exception e )
> -            {
> -                getLog().error( e );
> -            }
> -        }
> -    }
> -
> -
> -    /**
> -     * Copy artifacts to ${basedir}/lib
> -     * 
> -     * @param list
> -     * @throws IOException
> -     */
> -    private void copyArtifacts( final List<Artifact> list ) throws IOException
> -    {
> -        // Only proceed when we have artifacts to process
> -        if ( !list.isEmpty() )
> -        {
> -            final File copyDir = new File( project.getBasedir(), libraryPath );
> -
> -            if ( !copyDir.exists() )
> -                copyDir.mkdirs();
> -
> -            for ( Artifact artifact : list )
> -            {
> -                if ( !artifact.getScope().equalsIgnoreCase( "test" ) )
> -                {
> -                    final File destFile = new File( copyDir, artifact.getFile().getName() );
> -                    FileUtils.copyFile( artifact.getFile(), destFile );
> -                    getLog().info( "Copying " + artifact.getFile() + " to " + destFile );
> -                }
> -            }
> -        }
> -    }
> -
> -
> -    /**
> -     * Updates the Bundle-ClassPath entry in the manifest file
> -     * 
> -     * @param list
> -     * @throws FileNotFoundException
> -     * @throws IOException
> -     */
> -    private void updateManifest( final List<Artifact> list ) throws FileNotFoundException, IOException
> -    {
> -        final Maven2OsgiConverter maven2OsgiConverter = new DefaultMaven2OsgiConverter();
> -        final File manifestFile = new File( project.getBasedir(), "META-INF/MANIFEST.MF" );
> -        getLog().info( "Update Bundle-Classpath in " + manifestFile );
> -
> -        // Build Bundle-ClassPath entry
> -        final StringBuilder bundleClasspath = new StringBuilder( " ." );
> -        for ( Artifact artifact : list )
> -        {
> -            if ( !artifact.getScope().equalsIgnoreCase( "test" ) )
> -            {
> -                bundleClasspath.append( "," ).append( NEWLINE ).append( " " ).append( libraryPath ).append( '/' )
> -                    .append( artifact.getFile().getName() );
> -            }
> -        }
> -
> -        boolean inBundleClasspathEntry = false;
> -
> -        // Read existing MANIFEST.MF and add existing entries
> -        // to StringBuilder exept Bundle-ClassPath entry
> -        StringBuilder manifestSb = new StringBuilder();
> -        BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream( manifestFile ), "UTF-8" ) );
> -        String line;
> -        while ( ( line = in.readLine() ) != null )
> -        {
> -            if ( inBundleClasspathEntry && line.indexOf( ":" ) > -1 )
> -            {
> -                inBundleClasspathEntry = false;
> -            }
> -            else if ( inBundleClasspathEntry )
> -            {
> -                continue;
> -            }
> -
> -            String name = line.substring( 0, line.indexOf( ":" ) + 1 );
> -
> -            if ( !name.equalsIgnoreCase( ENTRY_BUNDLE_CLASSPATH ) )
> -            {
> -                if ( name.equalsIgnoreCase( ENTRY_BUNDLE_SYMBOLICNAME ) )
> -                {
> -                    // get OSGI Bundle Name
> -                    manifestSb.append( ENTRY_BUNDLE_SYMBOLICNAME );
> -                    manifestSb.append( " " );
> -                    manifestSb.append( maven2OsgiConverter.getBundleSymbolicName( project.getArtifact() ) );
> -                    manifestSb.append( ";singleton:=true" );
> -                    manifestSb.append( NEWLINE );
> -                }
> -                else if ( name.equalsIgnoreCase( ENTRY_BUNDLE_VERSION ) )
> -                {
> -                    // get OSGI Bundle Version
> -                    manifestSb.append( ENTRY_BUNDLE_VERSION );
> -                    manifestSb.append( " " );
> -                    manifestSb.append( maven2OsgiConverter.getVersion( project.getArtifact() ) );
> -                    manifestSb.append( NEWLINE );
> -                }
> -                else
> -                {
> -                    manifestSb.append( line ).append( NEWLINE );
> -                }
> -            }
> -            else
> -            {
> -                inBundleClasspathEntry = true;
> -            }
> -        }
> -
> -        // Add Bundle-ClassPath entry
> -        manifestSb.append( ENTRY_BUNDLE_CLASSPATH ).append( bundleClasspath ).append( NEWLINE );
> -
> -        // Write MANIFEST.MF
> -        Writer out = new OutputStreamWriter( new FileOutputStream( manifestFile ), "UTF-8" );
> -        out.write( manifestSb.toString() );
> -        out.flush();
> -        out.close();
> -    }
> -
> -
> -    /**
> -     * Adapt the ${basedir}/.classpath
> -     * 
> -     * @param list
> -     */
> -    private void updateDotClasspath( List<Artifact> list ) throws IOException, XmlPullParserException
> -    {
> -        getLog().info( "Update .classpath in " + project.getBasedir() );
> -        final InputStream is = new FileInputStream( new File( project.getBasedir(), ".classpath" ) );
> -        Xpp3Dom dom = Xpp3DomBuilder.build( is, "UTF-8" );
> -        int cnt = 0;
> -        for ( Xpp3Dom cpEntry : dom.getChildren( "classpathentry" ) )
> -        {
> -            if ( cpEntry.getAttribute( "kind" ).equals( "lib" ) )
> -            {
> -                dom.removeChild( cnt );
> -                cnt--;
> -            }
> -            cnt++;
> -        }
> -
> -        Xpp3Dom entry;
> -        for ( Artifact artifact : list )
> -        {
> -            if ( artifact.getScope().equalsIgnoreCase( "test" ) )
> -            {
> -                entry = new Xpp3Dom( "classpathentry" );
> -                entry.setAttribute( "kind", "lib" );
> -                entry.setAttribute( "path", ( new StringBuilder() ).append( libraryPath ).append( '/' ).append(
> -                    artifact.getFile().getName() ).toString() );
> -            }
> -            else
> -            {
> -                entry = new Xpp3Dom( "classpathentry" );
> -                entry.setAttribute( "exported", "true" );
> -                entry.setAttribute( "kind", "lib" );
> -                entry.setAttribute( "path", ( new StringBuilder() ).append( libraryPath ).append( '/' ).append(
> -                    artifact.getFile().getName() ).toString() );
> -            }
> -            dom.addChild( entry );
> -        }
> -
> -        is.close();
> -        Writer w = new OutputStreamWriter( new FileOutputStream( new File( project.getBasedir(), ".classpath" ) ),
> -            "UTF-8" );
> -        org.codehaus.plexus.util.xml.XMLWriter writer = new PrettyPrintXMLWriter( w );
> -        Xpp3DomWriter.write( writer, dom );
> -        w.flush();
> -        w.close();
> -    }
> -
> -
> -    /**
> -     * Adapt the ${basedir}/.project
> -     * 
> -     * @param list
> -     */
> -    private void updateDotProject() throws IOException, XmlPullParserException
> -    {
> -        getLog().info( "Update .project in " + project.getBasedir() );
> -        InputStream is = new FileInputStream( new File( project.getBasedir(), ".project" ) );
> -        Xpp3Dom dom = Xpp3DomBuilder.build( is, "UTF-8" );
> -        int cnt = 0;
> -        for ( Xpp3Dom cpEntry : dom.getChild( "buildSpec" ).getChildren( "buildCommand" ) )
> -        {
> -            if ( cpEntry.getChild( "name" ).getValue().equals( "org.eclipse.ui.externaltools.ExternalToolBuilder" ) )
> -            {
> -                dom.getChild( "buildSpec" ).removeChild( cnt );
> -                cnt--;
> -            }
> -            cnt++;
> -        }
> -
> -        removeChildFromDom( dom, "linkedResources" );
> -        removeChildFromDom( dom, "projects" );
> -
> -        is.close();
> -        Writer w = new OutputStreamWriter( new FileOutputStream( new File( project.getBasedir(), ".project" ) ),
> -            "UTF-8" );
> -        org.codehaus.plexus.util.xml.XMLWriter writer = new PrettyPrintXMLWriter( w );
> -        Xpp3DomWriter.write( writer, dom );
> -        w.flush();
> -        w.close();
> -    }
> -
> -
> -    /**
> -     * remove ${basedir}/maven-eclipse.xml
> -     */
> -    void removeMavenEclipseXml()
> -    {
> -        File file = new File( project.getBasedir(), "maven-eclipse.xml" );
> -        if ( file.exists() )
> -            file.delete();
> -    }
> -
> -
> -    /**
> -     * Adapt the ${basedir}/.externalToolBuilders
> -     */
> -    void removeDotExternalToolBuilders()
> -    {
> -        File file = new File( project.getBasedir(), ".externalToolBuilders" );
> -        if ( file.exists() )
> -            deleteDirectory( file );
> -    }
> -
> -
> -    private void removeChildFromDom( Xpp3Dom dom, String childName )
> -    {
> -        int cnt = 0;
> -        for ( Xpp3Dom child : dom.getChildren() )
> -        {
> -            if ( child.getName().equals( childName ) )
> -            {
> -                dom.removeChild( cnt );
> -                cnt -= 1;
> -            }
> -            cnt += 1;
> -        }
> -    }
> +	/**
> +	 * Constant used for newline.
> +	 */
> +	private static final String NEWLINE = "\n";
> +
> +	/**
> +	 * Bundle-ClassPath: updated with the list of dependencies.
> +	 */
> +	public final static String ENTRY_BUNDLE_CLASSPATH = "Bundle-ClassPath:";
> +
> +	/**
> +	 * Bundle symbolic name: updated with the artifact id.
> +	 */
> +	public final static String ENTRY_BUNDLE_SYMBOLICNAME = "Bundle-SymbolicName:";
> +
> +	/**
> +	 * Bundle version: updated with the project version.
> +	 */
> +	public final static String ENTRY_BUNDLE_VERSION = "Bundle-Version:";
> +
> +	public void execute() throws MojoExecutionException {
> +		if (project.isExecutionRoot()) {
> +			try {
> +				forkMvnGoal("eclipse:eclipse");
> +			} catch (Exception e) {
> +				throw new MojoExecutionException(e.getMessage());
> +			}
> +		}
> +		if (!skip) {
> +			try {
> +				// Create list of used artifacts
> +				final List<Artifact> artifactList = createArtifactList();
> +
> +				// copy Artifacts
> +				copyArtifacts(artifactList);
> +
> +				// Update Bundle-Classpath in MANIFEST.MF
> +				// FIXME remove this if no longer needed
> +				// updateManifest(artifactList);
> +
> +				// Update .classpath
> +				updateDotClasspath(artifactList);
> +
> +				updateDotProject();
> +				removeMavenEclipseXml();
> +				removeDotExternalToolBuilders();
> +
> +			} catch (FileNotFoundException e) {
> +				getLog()
> +						.error(
> +								"Please run eclipse:eclipse first to create .classpath, e.g. mvn eclipse:eclipse studio:eclipse.",
> +								e);
> +			} catch (Exception e) {
> +				getLog().error(e);
> +			}
> +		}
> +	}
> +
> +	/**
> +	 * Copy artifacts to ${basedir}/lib
> +	 * 
> +	 * @param list
> +	 * @throws IOException
> +	 */
> +	private void copyArtifacts(final List<Artifact> list) throws IOException {
> +		// Only proceed when we have artifacts to process
> +		if (!list.isEmpty()) {
> +			final File copyDir = new File(project.getBasedir(), libraryPath);
> +
> +			if (!copyDir.exists())
> +				copyDir.mkdirs();
> +
> +			for (Artifact artifact : list) {
> +				if (!artifact.getScope().equalsIgnoreCase("test")) {
> +					final File destFile = new File(copyDir, artifact.getFile()
> +							.getName());
> +					FileUtils.copyFile(artifact.getFile(), destFile);
> +					getLog()
> +							.info(
> +									"Copying " + artifact.getFile() + " to "
> +											+ destFile);
> +				}
> +			}
> +		}
> +	}
> +
> +	/**
> +	 * Updates the Bundle-ClassPath entry in the manifest file
> +	 * 
> +	 * @param list
> +	 * @throws FileNotFoundException
> +	 * @throws IOException
> +	 */
> +	private void updateManifest(final List<Artifact> list)
> +			throws FileNotFoundException, IOException {
> +		final Maven2OsgiConverter maven2OsgiConverter = new DefaultMaven2OsgiConverter();
> +		final File manifestFile = new File(project.getBasedir(),
> +				"META-INF/MANIFEST.MF");
> +		getLog().info("Update Bundle-Classpath in " + manifestFile);
> +
> +		// Build Bundle-ClassPath entry
> +		final StringBuilder bundleClasspath = new StringBuilder(" .");
> +		for (Artifact artifact : list) {
> +			if (!artifact.getScope().equalsIgnoreCase("test")) {
> +				bundleClasspath.append(",").append(NEWLINE).append(" ").append(
> +						libraryPath).append('/').append(
> +						artifact.getFile().getName());
> +			}
> +		}
> +
> +		boolean inBundleClasspathEntry = false;
> +
> +		// Read existing MANIFEST.MF and add existing entries
> +		// to StringBuilder exept Bundle-ClassPath entry
> +		StringBuilder manifestSb = new StringBuilder();
> +		BufferedReader in = new BufferedReader(new InputStreamReader(
> +				new FileInputStream(manifestFile), "UTF-8"));
> +		String line;
> +		while ((line = in.readLine()) != null) {
> +			if (inBundleClasspathEntry && line.indexOf(":") > -1) {
> +				inBundleClasspathEntry = false;
> +			} else if (inBundleClasspathEntry) {
> +				continue;
> +			}
> +
> +			String name = line.substring(0, line.indexOf(":") + 1);
> +
> +			if (!name.equalsIgnoreCase(ENTRY_BUNDLE_CLASSPATH)) {
> +				if (name.equalsIgnoreCase(ENTRY_BUNDLE_SYMBOLICNAME)) {
> +					// get OSGI Bundle Name
> +					manifestSb.append(ENTRY_BUNDLE_SYMBOLICNAME);
> +					manifestSb.append(" ");
> +					manifestSb.append(maven2OsgiConverter
> +							.getBundleSymbolicName(project.getArtifact()));
> +					manifestSb.append(";singleton:=true");
> +					manifestSb.append(NEWLINE);
> +				} else if (name.equalsIgnoreCase(ENTRY_BUNDLE_VERSION)) {
> +					// get OSGI Bundle Version
> +					manifestSb.append(ENTRY_BUNDLE_VERSION);
> +					manifestSb.append(" ");
> +					manifestSb.append(maven2OsgiConverter.getVersion(project
> +							.getArtifact()));
> +					manifestSb.append(NEWLINE);
> +				} else {
> +					manifestSb.append(line).append(NEWLINE);
> +				}
> +			} else {
> +				inBundleClasspathEntry = true;
> +			}
> +		}
> +
> +		// Add Bundle-ClassPath entry
> +		manifestSb.append(ENTRY_BUNDLE_CLASSPATH).append(bundleClasspath)
> +				.append(NEWLINE);
> +
> +		// Write MANIFEST.MF
> +		Writer out = new OutputStreamWriter(new FileOutputStream(manifestFile),
> +				"UTF-8");
> +		out.write(manifestSb.toString());
> +		out.flush();
> +		out.close();
> +	}
> +
> +	/**
> +	 * Adapt the ${basedir}/.classpath
> +	 * 
> +	 * @param list
> +	 */
> +	private void updateDotClasspath(List<Artifact> list) throws IOException,
> +			XmlPullParserException {
> +		getLog().info("Update .classpath in " + project.getBasedir());
> +		final InputStream is = new FileInputStream(new File(project
> +				.getBasedir(), ".classpath"));
> +		Xpp3Dom dom = Xpp3DomBuilder.build(is, "UTF-8");
> +		int cnt = 0;
> +		for (Xpp3Dom cpEntry : dom.getChildren("classpathentry")) {
> +			if (cpEntry.getAttribute("kind").equals("lib")) {
> +				dom.removeChild(cnt);
> +				cnt--;
> +			}
> +			cnt++;
> +		}
> +
> +		Xpp3Dom entry;
> +		for (Artifact artifact : list) {
> +			if (artifact.getScope().equalsIgnoreCase("test")) {
> +				entry = new Xpp3Dom("classpathentry");
> +				entry.setAttribute("kind", "lib");
> +				entry.setAttribute("path", (new StringBuilder()).append(
> +						libraryPath).append('/').append(
> +						artifact.getFile().getName()).toString());
> +			} else {
> +				entry = new Xpp3Dom("classpathentry");
> +				entry.setAttribute("exported", "true");
> +				entry.setAttribute("kind", "lib");
> +				entry.setAttribute("path", (new StringBuilder()).append(
> +						libraryPath).append('/').append(
> +						artifact.getFile().getName()).toString());
> +			}
> +			dom.addChild(entry);
> +		}
> +
> +		is.close();
> +		Writer w = new OutputStreamWriter(new FileOutputStream(new File(project
> +				.getBasedir(), ".classpath")), "UTF-8");
> +		org.codehaus.plexus.util.xml.XMLWriter writer = new PrettyPrintXMLWriter(
> +				w);
> +		Xpp3DomWriter.write(writer, dom);
> +		w.flush();
> +		w.close();
> +	}
> +
> +	/**
> +	 * Adapt the ${basedir}/.project
> +	 * 
> +	 * @param list
> +	 */
> +	private void updateDotProject() throws IOException, XmlPullParserException {
> +		getLog().info("Update .project in " + project.getBasedir());
> +		InputStream is = new FileInputStream(new File(project.getBasedir(),
> +				".project"));
> +		Xpp3Dom dom = Xpp3DomBuilder.build(is, "UTF-8");
> +		int cnt = 0;
> +		for (Xpp3Dom cpEntry : dom.getChild("buildSpec").getChildren(
> +				"buildCommand")) {
> +			if (cpEntry.getChild("name").getValue().equals(
> +					"org.eclipse.ui.externaltools.ExternalToolBuilder")) {
> +				dom.getChild("buildSpec").removeChild(cnt);
> +				cnt--;
> +			}
> +			cnt++;
> +		}
> +
> +		removeChildFromDom(dom, "linkedResources");
> +		removeChildFromDom(dom, "projects");
> +
> +		is.close();
> +		Writer w = new OutputStreamWriter(new FileOutputStream(new File(project
> +				.getBasedir(), ".project")), "UTF-8");
> +		org.codehaus.plexus.util.xml.XMLWriter writer = new PrettyPrintXMLWriter(
> +				w);
> +		Xpp3DomWriter.write(writer, dom);
> +		w.flush();
> +		w.close();
> +	}
> +
> +	/**
> +	 * remove ${basedir}/maven-eclipse.xml
> +	 */
> +	void removeMavenEclipseXml() {
> +		File file = new File(project.getBasedir(), "maven-eclipse.xml");
> +		if (file.exists())
> +			file.delete();
> +	}
> +
> +	/**
> +	 * Adapt the ${basedir}/.externalToolBuilders
> +	 */
> +	void removeDotExternalToolBuilders() {
> +		File file = new File(project.getBasedir(), ".externalToolBuilders");
> +		if (file.exists())
> +			deleteDirectory(file);
> +	}
> +
> +	private void removeChildFromDom(Xpp3Dom dom, String childName) {
> +		int cnt = 0;
> +		for (Xpp3Dom child : dom.getChildren()) {
> +			if (child.getName().equals(childName)) {
> +				dom.removeChild(cnt);
> +				cnt -= 1;
> +			}
> +			cnt += 1;
> +		}
> +	}
>  }
>
>
>   


Mime
View raw message