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