1 package org.codehaus.mojo.keytool;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.apache.maven.execution.MavenSession;
20 import org.apache.maven.plugin.MojoExecutionException;
21 import org.apache.maven.plugins.annotations.Component;
22 import org.apache.maven.plugins.annotations.Parameter;
23 import org.apache.maven.shared.utils.cli.Commandline;
24 import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
25 import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
26 import org.apache.maven.toolchain.Toolchain;
27 import org.apache.maven.toolchain.ToolchainManager;
28
29 import java.io.File;
30
31
32
33
34
35
36
37
38 public abstract class AbstractKeyToolRequestMojo<R extends KeyToolRequest>
39 extends AbstractKeyToolMojo
40 {
41
42
43
44
45
46
47
48
49
50
51
52 @Parameter
53 private String[] arguments;
54
55
56
57
58 @Parameter( defaultValue = "${basedir}", required = true, alias = "workingdir" )
59 private File workingDirectory;
60
61
62
63
64
65
66 @Component( role = KeyTool.class )
67 private KeyTool keyTool;
68
69
70
71
72
73
74 @Component
75 private ToolchainManager toolchainManager;
76
77
78
79
80
81
82
83 @Component
84 private MavenSession session;
85
86
87
88
89 private final Class<R> requestType;
90
91
92
93
94
95
96 protected AbstractKeyToolRequestMojo( Class<R> requestType )
97 {
98 this.requestType = requestType;
99 }
100
101
102
103
104 public void execute()
105 throws MojoExecutionException
106 {
107
108 if ( isSkip() )
109 {
110 getLog().info( getMessage( "disabled" ) );
111 }
112 else
113 {
114
115
116 Toolchain toolchain = getToolchain();
117
118 if ( toolchain != null )
119 {
120 getLog().info( "Toolchain in keytool-maven-plugin: " + toolchain );
121 keyTool.setToolchain(toolchain);
122 }
123
124
125 R request = createKeytoolRequest();
126
127 try
128 {
129 JavaToolResult result = keyTool.execute( request );
130
131 consumeResult( result );
132 }
133 catch ( JavaToolException e )
134 {
135 throw new MojoExecutionException( getMessage( "commandLineException", e.getMessage() ), e );
136 }
137 }
138 }
139
140
141
142
143
144
145
146 protected R createKeytoolRequest()
147 {
148 R request;
149 try
150 {
151 request = requestType.getConstructor().newInstance();
152 }
153 catch ( Exception e )
154 {
155
156 throw new RuntimeException( "Failed to create keytool request ", e );
157 }
158
159
160 request.setVerbose( isVerbose() );
161 request.setWorkingDirectory( this.workingDirectory );
162 request.setArguments( this.arguments );
163 return request;
164 }
165
166
167
168
169
170
171
172
173 protected String getCommandlineInfo( final Commandline commandLine )
174 {
175 if ( commandLine == null )
176 {
177 throw new NullPointerException( "commandLine" );
178 }
179 return commandLine.toString();
180 }
181
182
183
184
185
186
187
188 protected final void consumeResult( JavaToolResult result )
189 throws MojoExecutionException
190 {
191 Commandline commandLine = result.getCommandline();
192
193 int resultCode = result.getExitCode();
194
195 if ( resultCode != 0 )
196 {
197 throw new MojoExecutionException( getMessage( "failure", getCommandlineInfo( commandLine ), resultCode ) );
198 }
199 }
200
201
202
203
204
205
206
207
208 private Toolchain getToolchain()
209 {
210 Toolchain tc = null;
211 if ( toolchainManager != null )
212 {
213 tc = toolchainManager.getToolchainFromBuildContext( "jdk", session );
214 }
215
216 return tc;
217 }
218 }