Author: kenney Date: Tue Apr 11 07:47:48 2006 New Revision: 393237 URL: http://svn.apache.org/viewcvs?rev=393237&view=rev Log: PR: MJAR-24 Submitted by: Jerome Lacoste Patch by: Richard Allen <[EMAIL PROTECTED]>, modified by Jerome and /me
Don't sign already signed jars; allow for in-place jar signing. Modifications to the patch: code formatting. Modified: maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java maven/plugins/trunk/maven-jar-plugin/src/site/apt/howto.apt maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java Modified: maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java?rev=393237&r1=393236&r2=393237&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java (original) +++ maven/plugins/trunk/maven-jar-plugin/src/main/java/org/apache/maven/plugin/jar/JarSignMojo.java Tue Apr 11 07:47:48 2006 @@ -82,7 +82,7 @@ /** * Path of the jar to sign. When specified, the finalName is ignored. * - * @parameter alias="jarpath" + * @parameter alias="jarpath" default-value="${project.build.directory}/${project.build.finalName}.${project.packaging}" */ private File jarPath; @@ -118,8 +118,9 @@ /** * See <a href="http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jarsigner.html#Options">options</a>. * - * @parameter expression="${signedjar}" default-value="${project.build.directory}/signed/${project.build.finalName}.jar" - * @required + * Not specifying this argument will sign the jar in-place (your original jar is going to be overwritten). + * + * @parameter expression="${signedjar}" */ private File signedjar; @@ -185,19 +186,41 @@ getLog().info( "Skipping JAR signing for file: " + getJarFile().getAbsolutePath() ); } + JarSignVerifyMojo verifyMojo = createJarSignVerifyMojo(); + + File signedJarFile = signedjar != null ? signedjar : getJarFile(); + + if ( signedJarFile.exists() ) + { + verifyMojo.setWorkingDir( workingDirectory ); + verifyMojo.setBasedir( basedir ); + verifyMojo.setJarPath( signedJarFile ); + verifyMojo.setVerbose( verbose ); + verifyMojo.setErrorWhenNotSigned( false ); + verifyMojo.execute(); + } + + if ( verifyMojo.isSigned() ) + { + getLog().info( "JAR " + getJarFile().getAbsoluteFile() + " is already signed. Skipping."); + return; + } + signJar(); - if ( verify ) + if ( this.verify ) { - JarSignVerifyMojo verify = new JarSignVerifyMojo(); - verify.setWorkingDir( workingDirectory ); - verify.setBasedir( basedir ); - verify.setJarPath( getJarFile() ); - verify.setVerbose( verbose ); - verify.execute(); + verifyMojo.setErrorWhenNotSigned( true ); + verifyMojo.execute(); } } + protected JarSignVerifyMojo createJarSignVerifyMojo() + { + return new JarSignVerifyMojo(); + } + + File getJarFile() { if ( jarPath != null ) @@ -227,7 +250,7 @@ addArgIfNotEmpty( arguments, "-keystore", this.keystore ); addArgIfNotEmpty( arguments, "-storepass", this.storepass ); addArgIfNotEmpty( arguments, "-keypass", this.keypass ); - addArgIfNotEmpty( arguments, "-signedjar", this.signedjar.getPath() ); + addArgIfNotEmpty( arguments, "-signedjar", this.signedjar ); addArgIfNotEmpty( arguments, "-storetype", this.type ); addArgIfNotEmpty( arguments, "-sigfile", this.sigfile ); @@ -244,9 +267,14 @@ createParentDirIfNecessary( signedjar ); + if ( signedjar == null ) + { + getLog().debug( "Signing JAR in-place (overwritting original JAR)." ); + } + getLog().debug( "Executing: " + commandLine ); - // jarsigner may ask for some input if the parameters are missing or incorrect. + // jarsigner may ask for some input if the parameters are missing or incorrect. // This should take care of it and make it fail gracefully final InputStream inputStream = new InputStream() { @@ -286,6 +314,12 @@ throw new MojoExecutionException( "command execution failed", e ); } + // signed in place, no need to attach + if ( signedjar == null ) + { + return; + } + if ( classifier != null ) { projectHelper.attachArtifact( project, "jar", classifier, signedjar ); @@ -465,7 +499,8 @@ // hiding for now - I don't think this is required to be seen /* - public void setFinalName( String finalName ) { + public void setFinalName( String finalName ) + { this.finalName = finalName; } */ Modified: maven/plugins/trunk/maven-jar-plugin/src/site/apt/howto.apt URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/site/apt/howto.apt?rev=393237&r1=393236&r2=393237&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/site/apt/howto.apt (original) +++ maven/plugins/trunk/maven-jar-plugin/src/site/apt/howto.apt Tue Apr 11 07:47:48 2006 @@ -92,6 +92,9 @@ </project> ------------------- + If you do not specify the signedjar file, your jar will be signed in-place. + If you do specify it, the program will attempt to create the resulting containing directory. + * How to use jar:sign specifying parameters on the command line ------------------- Modified: maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java URL: http://svn.apache.org/viewcvs/maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java?rev=393237&r1=393236&r2=393237&view=diff ============================================================================== --- maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java (original) +++ maven/plugins/trunk/maven-jar-plugin/src/test/java/org/apache/maven/plugin/jar/JarSignMojoTest.java Tue Apr 11 07:47:48 2006 @@ -57,6 +57,8 @@ public Map systemProperties = new HashMap(); + public JarSignVerifyMojo verifyMojo = new JarSignVerifyMojo(); + protected int executeCommandLine( Commandline commandLine, InputStream inputStream, StreamConsumer stream1, StreamConsumer stream2 ) throws CommandLineException @@ -69,6 +71,11 @@ return executeResult; } + protected JarSignVerifyMojo createJarSignVerifyMojo() + { + return verifyMojo; + } + protected String getSystemProperty( String key ) { return (String) systemProperties.get( key ); @@ -96,6 +103,8 @@ mockArtifact.setType( "jar" ); project.setArtifact( mockArtifact ); mojo.setProject( project ); + + new File(getNullJar()).delete(); } public void tearDown() @@ -117,6 +126,44 @@ } /** + * We shouldn't sign the jar twice. + * On the second run, we simulated a created and signed jar. + */ + public void testRunTwice() + throws MojoExecutionException, IOException + { + mojo.execute(); + + class MyJarSignVerifyMojo + extends JarSignVerifyMojo + { + int nbExecutions; + + public void execute() + throws MojoExecutionException + { + nbExecutions++; + } + + public boolean isSigned() + { + return true; + } + } + + mojo.verifyMojo = new MyJarSignVerifyMojo(); + + new File(getNullJar()).createNewFile(); + + mojo.execute(); + + String[] expectedArguments = {"-keystore", "/tmp/keystore", "-keypass", "secretpassword", "-signedjar", + "/tmp/signed/file-version.jar", getNullJar(), "alias"}; + + checkMojo( expectedArguments ); + } + + /** */ public void testRunFailure() { @@ -179,12 +226,7 @@ assertEquals( 1, mojo.commandLines.size() ); Commandline commandline = (Commandline) mojo.commandLines.get( 0 ); String[] arguments = commandline.getArguments(); - // isn't there an assertEquals for arrays? - /* - for (int i = 0; i < arguments.length; i++ ) { - System.out.println( arguments[ i ] ); - } - */ + assertEquals( "Differing number of arguments", expectedCommandLineArguments.length, arguments.length ); for ( int i = 0; i < arguments.length; i++ ) {