/**
 * Copyright 2007 Google Inc.
 *
 * Licensed 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 com.tonicsystems.jarjar;

import java.io.File;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ExcludesArtifactFilter;
import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.Archiver;
import org.codehaus.plexus.archiver.manager.ArchiverManager;
import org.codehaus.plexus.util.FileUtils;

import com.tonicsystems.jarjar.util.StandaloneJarProcessor;

/**
 * TODO
 * 
 * @goal jarjar
 * @phase package
 * @requiresDependencyResolution test
 */
public class JarJarMojo
    extends AbstractMojo
{
    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * TODO
     * 
     * @parameter
     * @required
     */
    private List<PatternElement> rules;

    /**
     * TODO
     * 
     * @parameter default-value="${includes}"
     */
    private List<String> includes;

    /**
     * TODO
     * 
     * @parameter default-value="${excludes}"
     */
    private List<String> excludes;

    /**
     * TODO
     * 
     * @parameter default-value="${skipManifest}"
     */
    private boolean skipManifest;

    /**
     * @component
     */
    private ArchiverManager archiverManager;

    @SuppressWarnings( "unchecked" )
    public void execute()
        throws MojoExecutionException
    {
        final MainProcessor processor = new MainProcessor( rules, getLog().isDebugEnabled(), skipManifest );

        final File file = project.getArtifact().getFile();
        final File orig = new File( file.getParentFile(), "original-" + file.getName() );
        final File uber = new File( file.getParentFile(), "uber-" + file.getName() );

        final AndArtifactFilter filter = new AndArtifactFilter();
        filter.add( new IncludesArtifactFilter( includes ) );
        filter.add( new ExcludesArtifactFilter( excludes ) );

        try
        {
            final Archiver archiver = archiverManager.getArchiver( "jar" );

            archiver.setDestFile( uber );
            archiver.setIncludeEmptyDirs( false );
            archiver.addArchivedFileSet( file );

            for ( final Artifact a : (Set<Artifact>) project.getArtifacts() )
            {
                if ( "pom" != a.getType() && filter.include( a ) )
                {
                    archiver.addArchivedFileSet( a.getFile() );
                }
            }

            archiver.createArchive();

            FileUtils.rename( file, orig );
            StandaloneJarProcessor.run( uber, file, processor );
            processor.strip( file );

            uber.delete();
        }
        catch ( final Throwable e )
        {
            throw new MojoExecutionException( e.toString() );
        }
    }
}

@@ -1733,36 +1733,92 @@
 eter
- default-value=%22$%7Bincludes%7D%22
+%0A     */%0A    private File outputFile;%0A%0A    /**%0A     * TODO%0A     * %0A     * @parameter
 %0A   
@@ -1907,36 +1907,8 @@
 eter
- default-value=%22$%7Bexcludes%7D%22
 %0A   
@@ -1997,40 +1997,8 @@
 eter
- default-value=%22$%7BskipManifest%7D%22
 %0A   
@@ -2632,24 +2632,70 @@
 ctFilter();%0A
+        if ( null != includes )%0A        %7B%0A    
         filt
@@ -2740,24 +2740,80 @@
 cludes ) );%0A
+        %7D%0A        if ( null != excludes )%0A        %7B%0A    
         filt
@@ -2857,24 +2857,34 @@
 xcludes ) );
+%0A        %7D
 %0A%0A        tr
@@ -3231,32 +3231,8 @@
 if (
- %22pom%22 != a.getType() &&
  fil
@@ -3260,32 +3260,82 @@
               %7B%0A
+                    try%0A                    %7B%0A    
                 
@@ -3402,104 +3402,489 @@
     
-%7D%0A            %7D%0A%0A            archiver.createArchive();%0A%0A            FileUtils.rename( file, orig
+    %7D%0A                    catch ( final Throwable e )%0A                    %7B%0A                        getLog().info( %22Ignoring: %22 + a );%0A                        getLog().debug( e );%0A                    %7D%0A                %7D%0A            %7D%0A%0A            archiver.createArchive();%0A%0A            if ( null == outputFile )%0A            %7B%0A                FileUtils.rename( file, orig );%0A                outputFile = file;%0A            %7D%0A%0A            getLog().info( %22JarJar'ing to: %22 + outputFile
  );%0A
@@ -3929,17 +3929,23 @@
 ( uber, 
-f
+outputF
 ile, pro
@@ -3983,17 +3983,23 @@
 .strip( 
-f
+outputF
 ile );%0A%0A

Check for missing parameters and non-archive dependencies