1 package org.apache.maven.plugin.gpg;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.InputStream;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.codehaus.plexus.util.Os;
28 import org.codehaus.plexus.util.StringUtils;
29 import org.codehaus.plexus.util.cli.CommandLineException;
30 import org.codehaus.plexus.util.cli.CommandLineUtils;
31 import org.codehaus.plexus.util.cli.Commandline;
32 import org.codehaus.plexus.util.cli.DefaultConsumer;
33
34
35
36
37 public class GpgSigner
38 extends AbstractGpgSigner
39 {
40 private String executable;
41
42 public GpgSigner( String executable )
43 {
44 this.executable = executable;
45 }
46
47
48
49
50 @Override
51 protected void generateSignatureForFile( File file, File signature )
52 throws MojoExecutionException
53 {
54
55
56
57
58 Commandline cmd = new Commandline();
59
60 if ( StringUtils.isNotEmpty( executable ) )
61 {
62 cmd.setExecutable( executable );
63 }
64 else
65 {
66 cmd.setExecutable( "gpg" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ) );
67 }
68
69 if ( args != null )
70 {
71 for ( String arg : args )
72 {
73 cmd.createArg().setValue( arg );
74 }
75 }
76
77 if ( homeDir != null )
78 {
79 cmd.createArg().setValue( "--homedir" );
80 cmd.createArg().setFile( homeDir );
81 }
82
83 if ( useAgent )
84 {
85 cmd.createArg().setValue( "--use-agent" );
86 }
87 else
88 {
89 cmd.createArg().setValue( "--no-use-agent" );
90 }
91
92 InputStream in = null;
93 if ( null != passphrase )
94 {
95
96 cmd.createArg().setValue( "--batch" );
97
98 cmd.createArg().setValue( "--passphrase-fd" );
99
100 cmd.createArg().setValue( "0" );
101
102
103 in = new ByteArrayInputStream( passphrase.getBytes() );
104 }
105
106 if ( null != keyname )
107 {
108 cmd.createArg().setValue( "--local-user" );
109
110 cmd.createArg().setValue( keyname );
111 }
112
113 cmd.createArg().setValue( "--armor" );
114
115 cmd.createArg().setValue( "--detach-sign" );
116
117 if ( !isInteractive )
118 {
119 cmd.createArg().setValue( "--no-tty" );
120 }
121
122 if ( !defaultKeyring )
123 {
124 cmd.createArg().setValue( "--no-default-keyring" );
125 }
126
127 if ( StringUtils.isNotEmpty( secretKeyring ) )
128 {
129 cmd.createArg().setValue( "--secret-keyring" );
130 cmd.createArg().setValue( secretKeyring );
131 }
132
133 if ( StringUtils.isNotEmpty( publicKeyring ) )
134 {
135 cmd.createArg().setValue( "--keyring" );
136 cmd.createArg().setValue( publicKeyring );
137 }
138
139 if ( "once".equalsIgnoreCase( lockMode ) )
140 {
141 cmd.createArg().setValue( "--lock-once" );
142 }
143 else if ( "multiple".equalsIgnoreCase( lockMode ) )
144 {
145 cmd.createArg().setValue( "--lock-multiple" );
146 }
147 else if ( "never".equalsIgnoreCase( lockMode ) )
148 {
149 cmd.createArg().setValue( "--lock-never" );
150 }
151
152 cmd.createArg().setValue( "--output" );
153 cmd.createArg().setFile( signature );
154
155 cmd.createArg().setFile( file );
156
157
158
159
160
161 try
162 {
163 int exitCode = CommandLineUtils.executeCommandLine( cmd, in, new DefaultConsumer(), new DefaultConsumer() );
164
165 if ( exitCode != 0 )
166 {
167 throw new MojoExecutionException( "Exit code: " + exitCode );
168 }
169 }
170 catch ( CommandLineException e )
171 {
172 throw new MojoExecutionException( "Unable to execute gpg command", e );
173 }
174 }
175
176 }