[MJARSIGNER-8] Allow to exclude specific attachments from signing/verification
git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@817065 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/it/attachment-selection/excluded.jar b/src/it/attachment-selection/excluded.jar
new file mode 100644
index 0000000..f63952a
--- /dev/null
+++ b/src/it/attachment-selection/excluded.jar
Binary files differ
diff --git a/src/it/attachment-selection/pom.xml b/src/it/attachment-selection/pom.xml
new file mode 100644
index 0000000..a5bf781
--- /dev/null
+++ b/src/it/attachment-selection/pom.xml
@@ -0,0 +1,112 @@
+<?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.
+-->
+
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.jarsigner</groupId>
+ <artifactId>test</artifactId>
+ <version>1.0</version>
+ <packaging>jar</packaging>
+
+ <description>
+ Tests the selection of specific attachments.
+ </description>
+
+ <properties>
+ <maven.test.skip>true</maven.test.skip>
+ </properties>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <version>2.1</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-resources-plugin</artifactId>
+ <version>2.2</version>
+ </plugin>
+ <plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>build-helper-maven-plugin</artifactId>
+ <version>1.2</version>
+ <executions>
+ <execution>
+ <id>attach-artifacts</id>
+ <goals>
+ <goal>attach-artifact</goal>
+ </goals>
+ <configuration>
+ <artifacts>
+ <artifact>
+ <file>excluded.jar</file>
+ <type>jar</type>
+ <classifier>unsigned</classifier>
+ </artifact>
+ </artifacts>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.3.1</version>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jarsigner-plugin</artifactId>
+ <version>@project.version@</version>
+ <configuration>
+ <alias>test-01</alias>
+ <keypass>key-passwd</keypass>
+ <sigfile>TESTING</sigfile>
+ <excludeClassifiers>
+ <excludeClassifier>unsigned</excludeClassifier>
+ </excludeClassifiers>
+ </configuration>
+ <executions>
+ <execution>
+ <id>sign-jars</id>
+ <goals>
+ <goal>sign</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>verify-jars</id>
+ <goals>
+ <goal>verify</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+
+</project>
diff --git a/src/it/attachment-selection/src/main/resources/test.properties b/src/it/attachment-selection/src/main/resources/test.properties
new file mode 100644
index 0000000..dfb28b1
--- /dev/null
+++ b/src/it/attachment-selection/src/main/resources/test.properties
@@ -0,0 +1 @@
+key = value
\ No newline at end of file
diff --git a/src/it/attachment-selection/verify.bsh b/src/it/attachment-selection/verify.bsh
new file mode 100644
index 0000000..8a98f90
--- /dev/null
+++ b/src/it/attachment-selection/verify.bsh
@@ -0,0 +1,24 @@
+import java.io.*;
+import java.util.zip.*;
+
+File attachedJarFile = new File( basedir, "excluded.jar" );
+System.out.println( "Checking for existence of " + attachedJarFile );
+if ( !attachedJarFile.isFile() )
+{
+ throw new Exception( "missing " + attachedJarFile );
+}
+
+ZipFile attachedJar = new ZipFile( attachedJarFile );
+System.out.println( "Checking for absence of " + attachedJarFile.getName() + "!/META-INF/TESTING.SF" );
+if ( attachedJar.getEntry( "META-INF/TESTING.SF" ) != null )
+{
+ throw new Exception( "present " + attachedJarFile.getName() + "!/META-INF/TESTING.SF" );
+}
+System.out.println( "Checking for absence of " + attachedJarFile.getName() + "!/META-INF/TESTING.DSA" );
+if ( attachedJar.getEntry( "META-INF/TESTING.DSA" ) != null )
+{
+ throw new Exception( "present " + attachedJarFile.getName() + "!/META-INF/TESTING.DSA" );
+}
+attachedJar.close();
+
+return true;
diff --git a/src/it/type-detection/pom.xml b/src/it/type-detection/pom.xml
index f879469..4bdb4aa 100644
--- a/src/it/type-detection/pom.xml
+++ b/src/it/type-detection/pom.xml
@@ -28,7 +28,7 @@
<packaging>jar</packaging>
<description>
- Tests the detection of JAR files (we don't want to sign/verify other file formats).
+ Tests the detection of JAR/ZIP files (we don't want to sign/verify other file formats).
</description>
<properties>
diff --git a/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java b/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
index 335518c..1981fd4 100644
--- a/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
+++ b/src/main/java/org/apache/maven/plugins/jarsigner/AbstractJarsignerMojo.java
@@ -24,6 +24,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
@@ -142,6 +145,24 @@
private Boolean attachments;
/**
+ * A set of artifact classifiers describing the project attachments that should be processed. This parameter is only
+ * relevant if {@link #processAttachedArtifacts} is <code>true</code>. If empty, all attachments are included.
+ *
+ * @parameter
+ * @since 1.2
+ */
+ private String[] includeClassifiers;
+
+ /**
+ * A set of artifact classifiers describing the project attachments that should not be processed. This parameter is
+ * only relevant if {@link #processAttachedArtifacts} is <code>true</code>. If empty, no attachments are excluded.
+ *
+ * @parameter
+ * @since 1.2
+ */
+ private String[] excludeClassifiers;
+
+ /**
* The Maven project.
*
* @parameter default-value="${project}"
@@ -178,10 +199,32 @@
if ( processAttachedArtifacts && !Boolean.FALSE.equals( attachments ) )
{
+ Collection includes = new HashSet();
+ if ( includeClassifiers != null )
+ {
+ includes.addAll( Arrays.asList( includeClassifiers ) );
+ }
+
+ Collection excludes = new HashSet();
+ if ( excludeClassifiers != null )
+ {
+ excludes.addAll( Arrays.asList( excludeClassifiers ) );
+ }
+
for ( Iterator it = this.project.getAttachedArtifacts().iterator(); it.hasNext(); )
{
final Artifact artifact = (Artifact) it.next();
+ if ( !includes.isEmpty() && !includes.contains( artifact.getClassifier() ) )
+ {
+ continue;
+ }
+
+ if ( excludes.contains( artifact.getClassifier() ) )
+ {
+ continue;
+ }
+
processed += processArtifact( artifact ) ? 1 : 0;
}
}