View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2012-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.ssl.maven.plugin;
6   
7   import com.jcabi.log.Logger;
8   import java.io.File;
9   import java.io.IOException;
10  import org.apache.commons.codec.digest.DigestUtils;
11  import org.apache.maven.plugin.AbstractMojo;
12  import org.apache.maven.plugin.MojoFailureException;
13  import org.apache.maven.project.MavenProject;
14  import org.slf4j.impl.StaticLoggerBinder;
15  
16  /**
17   * Generate SSL keystore and configure in JVM.
18   *
19   * @since 0.5
20   * @goal keygen
21   * @phase initialize
22   */
23  public final class KeygenMojo extends AbstractMojo {
24  
25      /**
26       * Maven project.
27       * @parameter name="project" default-value="${project}"
28       * @readonly
29       * @required
30       */
31      private transient MavenProject project;
32  
33      /**
34       * Shall we skip execution?
35       * @parameter name="skip"
36       */
37      private transient boolean skip;
38  
39      /**
40       * Name of keystore.jks file.
41       * @parameter name="keystore"
42       * default-value="${project.build.directory}/keystore.jks"
43       */
44      private transient File keystore;
45  
46      /**
47       * Name of cacerts.jks file.
48       * @parameter name="cacerts"
49       * default-value="${project.build.directory}/cacerts.jks"
50       */
51      private transient File cacerts;
52  
53      /**
54       * Keystore instance.
55       */
56      private transient Keystore store;
57  
58      /**
59       * Cacerts instance.
60       */
61      private transient Cacerts truststore;
62  
63      /**
64       * Creates KeygenMojo.
65       */
66      public KeygenMojo() {
67          this(
68              null, new Keystore(DigestUtils.md5Hex(KeygenMojo.class.getName())),
69              null
70          );
71      }
72  
73      /**
74       * Creates KeygenMojo using custom KeystoreFactory.
75       * @param prj Maven project
76       * @param str Keystore instance
77       * @param crt Cacerts instance
78       */
79      public KeygenMojo(final MavenProject prj, final Keystore str,
80          final Cacerts crt) {
81          super();
82          this.project = prj;
83          this.store = str;
84          this.truststore = crt;
85      }
86  
87      /**
88       * Set skip option.
89       * @param skp Shall we skip execution?
90       */
91      public void setSkip(final boolean skp) {
92          this.skip = skp;
93      }
94  
95      @Override
96      public void execute() throws MojoFailureException {
97          StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
98          if (this.skip) {
99              Logger.info(this, "execution skipped because of 'skip' option");
100             return;
101         }
102         try {
103             if (this.truststore == null) {
104                 this.truststore = new Cacerts(this.cacerts);
105             }
106             if (!this.store.isActive()) {
107                 this.store.activate(this.keystore);
108                 this.truststore.imprt();
109             }
110         } catch (final IOException ex) {
111             throw new IllegalStateException(ex);
112         }
113         this.store.populate(this.project.getProperties());
114         this.truststore.populate(this.project.getProperties());
115         Logger.info(this, "Keystore is active: %s", this.store);
116     }
117 
118 }