View Javadoc
1   /*
2    * Copyright (c) 2011-2025 Yegor Bugayenko
3    *
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions
8    * are met: 1) Redistributions of source code must retain the above
9    * copyright notice, this list of conditions and the following
10   * disclaimer. 2) Redistributions in binary form must reproduce the above
11   * copyright notice, this list of conditions and the following
12   * disclaimer in the documentation and/or other materials provided
13   * with the distribution. 3) Neither the name of the Qulice.com nor
14   * the names of its contributors may be used to endorse or promote
15   * products derived from this software without specific prior written
16   * permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29   * OF THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package com.qulice.maven;
32  
33  import com.jcabi.log.Logger;
34  import java.util.Collection;
35  import java.util.LinkedList;
36  import org.apache.maven.execution.MavenSession;
37  import org.apache.maven.plugin.AbstractMojo;
38  import org.apache.maven.plugin.MavenPluginManager;
39  import org.apache.maven.plugin.MojoFailureException;
40  import org.apache.maven.plugins.annotations.Component;
41  import org.apache.maven.plugins.annotations.Parameter;
42  import org.apache.maven.project.MavenProject;
43  import org.codehaus.plexus.context.Context;
44  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
45  import org.slf4j.impl.StaticLoggerBinder;
46  
47  /**
48   * Abstract mojo.
49   *
50   * @since 0.3
51   */
52  public abstract class AbstractQuliceMojo extends AbstractMojo
53      implements Contextualizable {
54  
55      /**
56       * Environment to pass to validators.
57       */
58      private final DefaultMavenEnvironment environment =
59          new DefaultMavenEnvironment();
60  
61      /**
62       * Maven project, to be injected by Maven itself.
63       */
64      @Parameter(defaultValue = "${project}", readonly = true)
65      private MavenProject project;
66  
67      /**
68       * Maven session, to be injected by Maven itself.
69       */
70      @Parameter(defaultValue = "${session}", readonly = true)
71      private MavenSession sess;
72  
73      /**
74       * Maven plugin manager, to be injected by Maven itself.
75       */
76      @Component
77      private MavenPluginManager manager;
78  
79      /**
80       * Shall we skip execution?
81       */
82      @Parameter(property = "qulice.skip", defaultValue = "false")
83      private boolean skip;
84  
85      /**
86       * List of regular expressions to exclude.
87       */
88      @Parameter(property = "qulice.excludes")
89      private final Collection<String> excludes = new LinkedList<>();
90  
91      /**
92       * List of xpath queries to validate pom.xml.
93       * @checkstyle IndentationCheck (5 lines)
94       */
95      @Parameter(
96          property = "qulice.asserts",
97          required = false
98      )
99      private final Collection<String> asserts = new LinkedList<>();
100 
101     /**
102      * The source encoding.
103      *
104      * @parameter expression="${project.build.sourceEncoding}" required="true"
105      */
106     @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
107     private String charset;
108 
109     /**
110      * Set Maven Project (used mostly for unit testing).
111      * @param proj The project to set
112      */
113     public final void setProject(final MavenProject proj) {
114         this.project = proj;
115     }
116 
117     /**
118      * Set skip option (mostly for unit testing).
119      * @param skp The "skip" option
120      */
121     public final void setSkip(final boolean skp) {
122         this.skip = skp;
123     }
124 
125     /**
126      * Set asserts option.
127      * @param asser Asserts to use.
128      */
129     public final void setAsserts(final Collection<String> asser) {
130         this.asserts.clear();
131         this.asserts.addAll(asser);
132     }
133 
134     /**
135      * Set excludes.
136      * @param exprs Expressions
137      */
138     public final void setExcludes(final Collection<String> exprs) {
139         this.excludes.clear();
140         this.excludes.addAll(exprs);
141     }
142 
143     /**
144      * Set source code encoding.
145      * @param encoding Source code encoding
146      */
147     public void setEncoding(final String encoding) {
148         this.charset = encoding;
149     }
150 
151     @Override
152     public final void contextualize(final Context ctx) {
153         this.environment.setContext(ctx);
154     }
155 
156     @Override
157     public final void execute() throws MojoFailureException {
158         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
159         if (this.skip) {
160             this.getLog().info("Execution skipped");
161             return;
162         }
163         this.environment.setProject(this.project);
164         this.environment.setMojoExecutor(
165             new MojoExecutor(this.manager, this.sess)
166         );
167         this.environment.setExcludes(this.excludes);
168         this.environment.setAsser(this.asserts);
169         this.environment.setEncoding(this.charset);
170         final long start = System.nanoTime();
171         this.doExecute();
172         Logger.info(
173             this,
174             "Qulice quality check completed in %[nano]s",
175             System.nanoTime() - start
176         );
177     }
178 
179     /**
180      * Current maven session.
181      * @return Current maven session
182      */
183     public final MavenSession session() {
184         return this.sess;
185     }
186 
187     /**
188      * Do the real execution.
189      * @throws MojoFailureException If some failure inside
190      * @checkstyle NonStaticMethod (2 lines)
191      */
192     protected abstract void doExecute() throws MojoFailureException;
193 
194     /**
195      * Get the environment.
196      * @return The environment
197      */
198     protected final MavenEnvironment env() {
199         return this.environment;
200     }
201 
202 }