[MPATCH-1] Support deletion of empty files after the patches have been applied
Submitted by: Napoleon Esmundo C. Ramirez
Reviewed by: Benjamin Bentmann

o Added IT

git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@642023 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/projects/remove-empty-files/goals.txt b/src/it/projects/remove-empty-files/goals.txt
new file mode 100644
index 0000000..dfc317d
--- /dev/null
+++ b/src/it/projects/remove-empty-files/goals.txt
@@ -0,0 +1,2 @@
+clean
+initialize
diff --git a/src/it/projects/remove-empty-files/pom.xml b/src/it/projects/remove-empty-files/pom.xml
new file mode 100644
index 0000000..d911a74
--- /dev/null
+++ b/src/it/projects/remove-empty-files/pom.xml
@@ -0,0 +1,57 @@
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  
+  <parent>
+    <groupId>org.apache.maven.plugins.patch.it</groupId>
+    <artifactId>it-project-parent</artifactId>
+    <version>1</version>
+  </parent>
+  
+  <artifactId>remove-empty-files</artifactId>
+  <name>remove-empty-files</name>
+  
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-patch-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>apply-patch-0</id>
+            <phase>initialize</phase>
+            <configuration>
+              <targetDirectory>${basedir}</targetDirectory>
+              <removeEmptyFiles>true</removeEmptyFiles>
+              <patches>
+                <patch>first.patch</patch>
+                <patch>second.patch</patch>
+              </patches>
+            </configuration>
+            <goals>
+              <goal>apply</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/src/it/projects/remove-empty-files/src/main/patches/first.patch b/src/it/projects/remove-empty-files/src/main/patches/first.patch
new file mode 100644
index 0000000..3dbd0bc
--- /dev/null
+++ b/src/it/projects/remove-empty-files/src/main/patches/first.patch
@@ -0,0 +1,25 @@
+#
+#  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.
+#
+Index: target/test.txt
+===================================================================
+--- target/test.txt	(revision 0)
++++ target/test.txt	(revision 0)
+@@ -0,0 +1 @@
++This is a test.
+
diff --git a/src/it/projects/remove-empty-files/src/main/patches/second.patch b/src/it/projects/remove-empty-files/src/main/patches/second.patch
new file mode 100644
index 0000000..7deff88
--- /dev/null
+++ b/src/it/projects/remove-empty-files/src/main/patches/second.patch
@@ -0,0 +1,25 @@
+#
+#  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.
+#
+Index: target/test.txt
+===================================================================
+--- target/test.txt	(revision 0)
++++ target/test.txt	(revision 0)
+@@ -1,1 +0,0 @@
+-This is a test.
+
diff --git a/src/it/projects/remove-empty-files/verify.bsh b/src/it/projects/remove-empty-files/verify.bsh
new file mode 100644
index 0000000..2c62468
--- /dev/null
+++ b/src/it/projects/remove-empty-files/verify.bsh
@@ -0,0 +1,38 @@
+/*
+    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.
+*/
+
+import java.io.*;
+import java.util.*;
+
+try
+{
+    File testFile = new File( basedir, "target/test.txt" );
+    if ( testFile.exists() )
+    {
+        System.err.println( "Test file was not deleted: " + testFile );
+        return false;
+    }
+}
+catch( Throwable t )
+{
+    t.printStackTrace();
+    return false;
+}
+
+return true;
diff --git a/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java b/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java
index f697516..08a337c 100644
--- a/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java
+++ b/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java
@@ -239,6 +239,15 @@
     private File patchDirectory;
 
     /**
+     * When set to <code>true</code>, the empty files resulting from the patching process are removed. Empty ancestor
+     * directories are removed as well.
+     * 
+     * @parameter default-value="false"
+     * @since 1.1
+     */
+    private boolean removeEmptyFiles;
+
+    /**
      * Apply the patches. Give preference to patchFile over patchSourceDir/patches, and preference to originalFile over
      * workDir.
      */
@@ -586,6 +595,11 @@
             cli.createArg().setValue( "-b" );
         }
 
+        if ( removeEmptyFiles )
+        {
+            cli.createArg().setValue( "-E" );
+        }
+
         cli.createArg().setValue( "<" );
         cli.createArg().setFile( patchFile );