JCRVLT-321 Warn for files not being included in the package due to being outside of filter roots (closes #19)

git-svn-id: https://svn.apache.org/repos/asf/jackrabbit/commons/filevault-package-maven-plugin/trunk@1846090 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/VaultMojo.java b/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/VaultMojo.java
index b021af2..186edf7 100644
--- a/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/VaultMojo.java
+++ b/src/main/java/org/apache/jackrabbit/filevault/maven/packaging/VaultMojo.java
@@ -19,7 +19,9 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 import java.util.List;
@@ -94,6 +96,17 @@
                     "${project.build.outputDirectory}"
     )
     private File[] jcrRootSourceDirectory;
+    
+    /**
+     * Set to {@code true} to fail the build in case of files are being contained in the {@code jcrRootSourceDirectory} 
+     * which are not covered by the filter rules and therefore would not end up in the package.
+     */
+    @Parameter(
+            property = "vault.failOnUncoveredSourceFiles",
+            required = true,
+            defaultValue = "false"
+    )
+    private boolean failOnUncoveredSourceFiles;
 
     /**
      * The name of the generated package ZIP file without the ".zip" file
@@ -243,6 +256,16 @@
                         }
                     }
                 }
+                
+                Collection<File> uncoveredFiles = getUncoveredFiles(jcrSourceDirectory, prefix, contentPackageArchiver.getFiles().keySet());
+                if (!uncoveredFiles.isEmpty()) {
+                    for (File uncoveredFile : uncoveredFiles) {
+                        getLog().warn("File " + uncoveredFile + " not covered by a filter rule and therefore not contained in the resulting package");
+                    }
+                    if (failOnUncoveredSourceFiles) {
+                        throw new MojoFailureException("The following files are not covered by a filter rule: \n" + StringUtils.join(uncoveredFiles.iterator(), ",\n"));
+                    }
+                }
             }
 
             for (Map.Entry<String, File> entry : embeddedFiles.entrySet()) {
@@ -289,6 +312,28 @@
         }
     }
 
+    private Collection<File> getUncoveredFiles(final File sourceDirectory, final String prefix, final Collection<String> entryNames) throws IOException {
+        /*
+         *  similar method as in {@link org.codehaus.plexus.components.io.resources.PlexusIoFileResourceCollection#getResources();}
+         */
+        DirectoryScanner scanner = new DirectoryScanner();
+        scanner.setBasedir(sourceDirectory);
+        scanner.setExcludes(excludes);
+        scanner.addDefaultExcludes();
+        scanner.scan();
+        return getUncoveredFiles(sourceDirectory, scanner.getIncludedFiles(), prefix, entryNames);
+    }
+
+    private Collection<File> getUncoveredFiles(final File sourceDirectory, final String[] relativeSourceFileNames, final String prefix, final Collection<String> entryNames) {
+        Collection<File> uncoveredFiles = new ArrayList<>();
+        for (String relativeSourceFileName : relativeSourceFileNames) {
+            if (!entryNames.contains(JCR_ROOT + prefix + relativeSourceFileName)) {
+                uncoveredFiles.add(new File(sourceDirectory, relativeSourceFileName));
+            }
+        }
+        return uncoveredFiles;
+    }
+    
     private MavenArchiveConfiguration getMavenArchiveConfiguration(File manifestFile) throws IOException {
         if (archive == null) {
             archive = new MavenArchiveConfiguration();
diff --git a/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/it/FilterIT.java b/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/it/FilterIT.java
index 06e19da..175531c 100644
--- a/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/it/FilterIT.java
+++ b/src/test/java/org/apache/jackrabbit/filevault/maven/packaging/it/FilterIT.java
@@ -157,4 +157,9 @@
 
         assertTrue("Marker file still exists.", marker.exists());
     }
+    
+    @Test 
+    public void test_filter_not_covering_all_files() throws Exception {
+        verify("filter-not-covering-all-files", true);
+    }
 }
diff --git a/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/META-INF/vault/filter.xml b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/META-INF/vault/filter.xml
new file mode 100644
index 0000000..0562814
--- /dev/null
+++ b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/META-INF/vault/filter.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+<workspaceFilter version="1.0">
+    <filter root="/apps2/test2"/>
+</workspaceFilter>
diff --git a/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/jcr_root/apps/.content.xml b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/jcr_root/apps/.content.xml
new file mode 100755
index 0000000..dcf03f8
--- /dev/null
+++ b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/jcr_root/apps/.content.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+  -->
+
+<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
+    jcr:primaryType="sling:Folder"/>
diff --git a/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/pom.xml b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/pom.xml
new file mode 100644
index 0000000..4be889e
--- /dev/null
+++ b/src/test/resources/test-projects/filter-tests/filter-not-covering-all-files/pom.xml
@@ -0,0 +1,53 @@
+<!--
+  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>
+
+    <!-- ====================================================================== -->
+    <!-- P R O J E C T  D E S C R I P T I O N                                   -->
+    <!-- ====================================================================== -->
+    <groupId>org.apache.jackrabbit.filevault</groupId>
+    <artifactId>package-plugin-test-pkg</artifactId>
+    <version>1.0.0-SNAPSHOT</version>
+    <packaging>content-package</packaging>
+
+    <description>Test project that defines an explicit filter in META-INF/vault/filter.xml</description>
+    <build>
+        <resources>
+            <resource>
+                <directory>${basedir}/META-INF</directory>
+                <targetPath>${project.build.directory}/vault-work/META-INF</targetPath>
+            </resource>
+            <resource>
+                <directory>${basedir}/jcr_root</directory>
+                <targetPath>.</targetPath>
+            </resource>
+        </resources>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.jackrabbit</groupId>
+                <artifactId>filevault-package-maven-plugin</artifactId>
+                <version>${plugin.version}</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <filterSource>filter.xml</filterSource>
+                    <failOnUncoveredSourceFiles>true</failOnUncoveredSourceFiles>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>