View Javadoc
1   /*
2    * Copyright (c) 2011-2024 Qulice.com
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       * Location of License file. If it is an absolute file name you should
87       * prepend it with "file:" prefix. Otherwise it is treated like a resource
88       * name and will be found in classpath (if available).
89       */
90      @Parameter(property = "qulice.license", defaultValue = "LICENSE.txt")
91      private String license = "LICENSE.txt";
92  
93      /**
94       * List of regular expressions to exclude.
95       */
96      @Parameter(property = "qulice.excludes")
97      private final Collection<String> excludes = new LinkedList<>();
98  
99      /**
100      * List of xpath queries to validate pom.xml.
101      * @checkstyle IndentationCheck (5 lines)
102      */
103     @Parameter(
104         property = "qulice.asserts",
105         required = false
106     )
107     private final Collection<String> asserts = new LinkedList<>();
108 
109     /**
110      * The source encoding.
111      *
112      * @parameter expression="${project.build.sourceEncoding}" required="true"
113      */
114     @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
115     private String charset;
116 
117     /**
118      * Set Maven Project (used mostly for unit testing).
119      * @param proj The project to set
120      */
121     public final void setProject(final MavenProject proj) {
122         this.project = proj;
123     }
124 
125     /**
126      * Set skip option (mostly for unit testing).
127      * @param skp The "skip" option
128      */
129     public final void setSkip(final boolean skp) {
130         this.skip = skp;
131     }
132 
133     /**
134      * Set asserts option.
135      * @param asser Asserts to use.
136      */
137     public final void setAsserts(final Collection<String> asser) {
138         this.asserts.clear();
139         this.asserts.addAll(asser);
140     }
141 
142     /**
143      * Set license address.
144      * @param lcs The "license" option
145      */
146     public final void setLicense(final String lcs) {
147         this.license = lcs;
148     }
149 
150     /**
151      * Set excludes.
152      * @param exprs Expressions
153      */
154     public final void setExcludes(final Collection<String> exprs) {
155         this.excludes.clear();
156         this.excludes.addAll(exprs);
157     }
158 
159     /**
160      * Set source code encoding.
161      * @param encoding Source code encoding
162      */
163     public void setEncoding(final String encoding) {
164         this.charset = encoding;
165     }
166 
167     @Override
168     public final void contextualize(final Context ctx) {
169         this.environment.setContext(ctx);
170     }
171 
172     @Override
173     public final void execute() throws MojoFailureException {
174         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
175         if (this.skip) {
176             this.getLog().info("Execution skipped");
177             return;
178         }
179         this.environment.setProperty("license", this.license);
180         this.environment.setProject(this.project);
181         this.environment.setMojoExecutor(
182             new MojoExecutor(this.manager, this.sess)
183         );
184         this.environment.setExcludes(this.excludes);
185         this.environment.setAsser(this.asserts);
186         this.environment.setEncoding(this.charset);
187         final long start = System.nanoTime();
188         this.doExecute();
189         Logger.info(
190             this,
191             "Qulice quality check completed in %[nano]s",
192             System.nanoTime() - start
193         );
194     }
195 
196     /**
197      * Current maven session.
198      * @return Current maven session
199      */
200     public final MavenSession session() {
201         return this.sess;
202     }
203 
204     /**
205      * Do the real execution.
206      * @throws MojoFailureException If some failure inside
207      * @checkstyle NonStaticMethod (2 lines)
208      */
209     protected abstract void doExecute() throws MojoFailureException;
210 
211     /**
212      * Get the environment.
213      * @return The environment
214      */
215     protected final MavenEnvironment env() {
216         return this.environment;
217     }
218 
219 }