001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.geronimo.genesis.plugins.tools; 021 022 import java.util.List; 023 import java.util.Iterator; 024 import java.util.ArrayList; 025 026 import java.io.File; 027 import java.io.ByteArrayInputStream; 028 import java.io.InputStream; 029 030 import org.apache.maven.project.MavenProject; 031 import org.apache.maven.project.MavenProjectHelper; 032 import org.apache.maven.artifact.Artifact; 033 import org.apache.maven.plugin.MojoExecutionException; 034 import org.apache.commons.lang.SystemUtils; 035 import org.apache.geronimo.genesis.MojoSupport; 036 037 import org.codehaus.plexus.util.cli.Commandline; 038 import org.codehaus.plexus.util.cli.CommandLineUtils; 039 import org.codehaus.plexus.util.cli.DefaultConsumer; 040 import org.codehaus.plexus.util.cli.CommandLineException; 041 042 /** 043 * Sign project attached artifacts with GnuPG. 044 * 045 * @goal gpg-sign-attached 046 * @phase verify 047 * 048 * @version $Rev: 470156 $ $Date: 2006-11-01 17:11:16 -0800 (Wed, 01 Nov 2006) $ 049 */ 050 public class GpgSignAttachedMojo 051 extends MojoSupport 052 { 053 // 054 // TODO: Pull the passphrase from settings 055 // 056 057 /** 058 * The passphrase to use when signing. 059 * 060 * @parameter expression="${passphrase}" 061 * @required 062 */ 063 private String passphrase = null; 064 065 /** 066 * The maven project. 067 * 068 * @parameter expression="${project}" 069 * @required 070 * @readonly 071 */ 072 protected MavenProject project = null; 073 074 /** 075 * Maven ProjectHelper 076 * 077 * @component 078 * @required 079 * @readonly 080 */ 081 private MavenProjectHelper projectHelper = null; 082 083 // 084 // Mojo 085 // 086 087 protected void doExecute() throws Exception { 088 List artifacts = new ArrayList(); 089 artifacts.add(project.getArtifact()); 090 artifacts.addAll(project.getAttachedArtifacts()); 091 092 // 093 // TODO: Sign the POM file 094 // 095 096 if (log.isDebugEnabled()) { 097 log.info("Artifacts to be signed: " + artifacts); 098 } 099 100 // Sign attached artifacts 101 Iterator iter = artifacts.iterator(); 102 while (iter.hasNext()) { 103 Artifact artifact = (Artifact)iter.next(); 104 File file = artifact.getFile(); 105 106 if (file == null) { 107 log.info("No file to sign for artifact: " + artifact); 108 continue; 109 } 110 111 File signature = sign(file); 112 projectHelper.attachArtifact(project, artifact.getType() + ".asc", signature); 113 } 114 } 115 116 private File sign(final File file) throws Exception { 117 assert file != null; 118 119 log.info("Signing artifact file: " + file); 120 121 File signature = new File(file.getCanonicalPath() + ".asc"); 122 log.debug("Signature file: " + signature); 123 124 if (signature.exists()) { 125 log.debug("Signature file already exists, removing: " + signature); 126 signature.delete(); 127 } 128 129 Commandline cmd = new Commandline(); 130 cmd.setExecutable("gpg" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "")); 131 132 cmd.createArgument().setValue("--passphrase-fd"); 133 cmd.createArgument().setValue("0"); 134 cmd.createArgument().setValue("--armor"); 135 cmd.createArgument().setValue("--detach-sign"); 136 cmd.createArgument().setFile(file); 137 138 if (log.isDebugEnabled()) { 139 log.debug(Commandline.toString(cmd.getCommandline())); 140 } 141 142 // Prepare the input stream which will be used to pass the passphrase to the executable 143 InputStream in = new ByteArrayInputStream(passphrase.getBytes()); 144 145 try { 146 int exitCode = CommandLineUtils.executeCommandLine(cmd, in, new DefaultConsumer(), new DefaultConsumer()); 147 148 if (exitCode != 0) { 149 throw new MojoExecutionException("Exit code: " + exitCode); 150 } 151 } 152 catch (CommandLineException e) { 153 throw new MojoExecutionException("Unable to execute java command", e); 154 } 155 156 return signature; 157 } 158 }