1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.jarsigner;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugins.annotations.LifecyclePhase;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.shared.jarsigner.JarSigner;
29 import org.apache.maven.shared.jarsigner.JarSignerRequest;
30 import org.apache.maven.shared.jarsigner.JarSignerUtil;
31 import org.apache.maven.shared.jarsigner.JarSignerVerifyRequest;
32 import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
33 import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
34
35
36
37
38
39
40
41 @Mojo(name = "verify", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
42 public class JarsignerVerifyMojo extends AbstractJarsignerMojo {
43
44
45
46
47 @Parameter(property = "jarsigner.certs", defaultValue = "false")
48 private boolean certs;
49
50
51
52
53
54
55
56
57
58 @Parameter(property = "jarsigner.errorWhenNotSigned", defaultValue = "false")
59 private boolean errorWhenNotSigned;
60
61
62
63
64 @Override
65 protected JarSignerRequest createRequest(File archive) {
66 JarSignerVerifyRequest request = new JarSignerVerifyRequest();
67 request.setCerts(certs);
68 return request;
69 }
70
71 @Override
72 protected void preProcessArchive(File archive) throws MojoExecutionException {
73 super.preProcessArchive(archive);
74
75 if (errorWhenNotSigned) {
76
77
78 boolean archiveSigned;
79 try {
80 archiveSigned = JarSignerUtil.isArchiveSigned(archive);
81 } catch (IOException e) {
82 throw new MojoExecutionException(
83 "Failed to check if archive " + archive + " is signed: " + e.getMessage(), e);
84 }
85
86 if (!archiveSigned) {
87
88
89 throw new MojoExecutionException(getMessage("archiveNotSigned", archive));
90 }
91 }
92 }
93
94 @Override
95 protected void executeJarSigner(JarSigner jarSigner, JarSignerRequest request)
96 throws JavaToolException, MojoExecutionException {
97 JavaToolResult result = jarSigner.execute(request);
98 int resultCode = result.getExitCode();
99 if (resultCode != 0) {
100 throw new MojoExecutionException(
101 getMessage("failure", getCommandlineInfo(result.getCommandline()), resultCode));
102 }
103 }
104 }