View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.jcabi.log.Logger;
8   import java.util.Collection;
9   import java.util.LinkedList;
10  import javax.inject.Inject;
11  import org.apache.maven.execution.MavenSession;
12  import org.apache.maven.plugin.AbstractMojo;
13  import org.apache.maven.plugin.MavenPluginManager;
14  import org.apache.maven.plugin.MojoFailureException;
15  import org.apache.maven.plugins.annotations.Parameter;
16  import org.apache.maven.project.MavenProject;
17  import org.codehaus.plexus.context.Context;
18  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
19  import org.slf4j.impl.StaticLoggerBinder;
20  
21  /**
22   * Abstract mojo.
23   *
24   * @since 0.3
25   */
26  public abstract class AbstractQuliceMojo extends AbstractMojo
27      implements Contextualizable {
28  
29      /**
30       * Environment to pass to validators.
31       */
32      private final DefaultMavenEnvironment environment =
33          new DefaultMavenEnvironment();
34  
35      /**
36       * Maven project, to be injected by Maven itself.
37       */
38      @Parameter(defaultValue = "${project}", readonly = true)
39      private MavenProject project;
40  
41      /**
42       * Maven session, to be injected by Maven itself.
43       */
44      @Parameter(defaultValue = "${session}", readonly = true)
45      private MavenSession sess;
46  
47      /**
48       * Maven plugin manager, to be injected by Maven itself.
49       */
50      @Inject
51      private MavenPluginManager manager;
52  
53      /**
54       * Shall we skip execution?
55       */
56      @Parameter(property = "qulice.skip", defaultValue = "false")
57      private boolean skip;
58  
59      /**
60       * List of regular expressions to exclude.
61       */
62      @Parameter(property = "qulice.excludes")
63      private final Collection<String> excludes = new LinkedList<>();
64  
65      /**
66       * List of xpath queries to validate pom.xml.
67       * @checkstyle IndentationCheck (5 lines)
68       */
69      @Parameter(
70          property = "qulice.asserts",
71          required = false
72      )
73      private final Collection<String> asserts = new LinkedList<>();
74  
75      /**
76       * The source encoding.
77       *
78       * @parameter expression="${project.build.sourceEncoding}" required="true"
79       */
80      @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
81      private String charset;
82  
83      /**
84       * Set Maven Project (used mostly for unit testing).
85       * @param proj The project to set
86       */
87      public final void setProject(final MavenProject proj) {
88          this.project = proj;
89      }
90  
91      /**
92       * Set skip option (mostly for unit testing).
93       * @param skp The "skip" option
94       */
95      public final void setSkip(final boolean skp) {
96          this.skip = skp;
97      }
98  
99      /**
100      * Set asserts option.
101      * @param val Asserts to use.
102      */
103     public final void setAsserts(final Collection<String> val) {
104         this.asserts.clear();
105         this.asserts.addAll(val);
106     }
107 
108     /**
109      * Set excludes.
110      * @param exprs Expressions
111      */
112     public final void setExcludes(final Collection<String> exprs) {
113         this.excludes.clear();
114         this.excludes.addAll(exprs);
115     }
116 
117     /**
118      * Set source code encoding.
119      * @param encoding Source code encoding
120      */
121     public void setEncoding(final String encoding) {
122         this.charset = encoding;
123     }
124 
125     @Override
126     public final void contextualize(final Context ctx) {
127         this.environment.setContext(ctx);
128     }
129 
130     @Override
131     public final void execute() throws MojoFailureException {
132         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
133         if (this.skip) {
134             this.getLog().info("Execution skipped");
135             return;
136         }
137         this.environment.setProject(this.project);
138         this.environment.setMojoExecutor(
139             new MojoExecutor(this.manager, this.sess)
140         );
141         this.environment.setExcludes(this.excludes);
142         this.environment.setAssertion(this.asserts);
143         this.environment.setEncoding(this.charset);
144         final long start = System.nanoTime();
145         this.doExecute();
146         Logger.info(
147             this,
148             "Qulice quality check completed in %[nano]s",
149             System.nanoTime() - start
150         );
151     }
152 
153     /**
154      * Current maven session.
155      * @return Current maven session
156      */
157     public final MavenSession session() {
158         return this.sess;
159     }
160 
161     /**
162      * Do the real execution.
163      * @throws MojoFailureException If some failure inside
164      * @checkstyle NonStaticMethod (2 lines)
165      */
166     protected abstract void doExecute() throws MojoFailureException;
167 
168     /**
169      * Get the environment.
170      * @return The environment
171      */
172     protected final MavenEnvironment env() {
173         return this.environment;
174     }
175 
176 }