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.ArrayList;
9   import java.util.Collection;
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  
33      /**
34       * Maven project, to be injected by Maven itself.
35       */
36      @Parameter(defaultValue = "${project}", readonly = true)
37      private MavenProject project;
38  
39      /**
40       * Maven session, to be injected by Maven itself.
41       */
42      @Parameter(defaultValue = "${session}", readonly = true)
43      private MavenSession sess;
44  
45      /**
46       * Maven plugin manager, to be injected by Maven itself.
47       */
48      @Inject
49      private MavenPluginManager manager;
50  
51      /**
52       * Shall we skip execution?
53       */
54      @Parameter(property = "qulice.skip", defaultValue = "false")
55      private boolean skip;
56  
57      /**
58       * List of regular expressions to exclude.
59       */
60      @Parameter(property = "qulice.excludes")
61      private final Collection<String> excludes;
62  
63      /**
64       * List of xpath queries to validate pom.xml.
65       */
66      @Parameter(
67          property = "qulice.asserts",
68          required = false
69      )
70      private final Collection<String> asserts;
71  
72      /**
73       * The source encoding.
74       * @parameter expression="${project.build.sourceEncoding}" required="true"
75       */
76      @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
77      private String charset;
78  
79      /**
80       * Default constructor.
81       */
82      protected AbstractQuliceMojo() {
83          this.environment = new DefaultMavenEnvironment();
84          this.excludes = new ArrayList<>(0);
85          this.asserts = new ArrayList<>(0);
86      }
87  
88      /**
89       * Set Maven Project (used mostly for unit testing).
90       * @param proj The project to set
91       */
92      public final void setProject(final MavenProject proj) {
93          this.project = proj;
94      }
95  
96      /**
97       * Set skip option (mostly for unit testing).
98       * @param skp The "skip" option
99       */
100     public final void setSkip(final boolean skp) {
101         this.skip = skp;
102     }
103 
104     /**
105      * Set asserts option.
106      * @param val Asserts to use
107      */
108     public final void setAsserts(final Collection<String> val) {
109         this.asserts.clear();
110         this.asserts.addAll(val);
111     }
112 
113     /**
114      * Set excludes.
115      * @param exprs Expressions
116      */
117     public final void setExcludes(final Collection<String> exprs) {
118         this.excludes.clear();
119         this.excludes.addAll(exprs);
120     }
121 
122     /**
123      * Set source code encoding.
124      * @param encoding Source code encoding
125      */
126     public void setEncoding(final String encoding) {
127         this.charset = encoding;
128     }
129 
130     @Override
131     public final void contextualize(final Context ctx) {
132         this.environment.setContext(ctx);
133     }
134 
135     @Override
136     public final void execute() throws MojoFailureException {
137         StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
138         if (this.skip) {
139             this.getLog().info("Execution skipped");
140             return;
141         }
142         this.environment.setProject(this.project);
143         this.environment.setMojoExecutor(
144             new MojoExecutor(this.manager, this.sess)
145         );
146         this.environment.setExcludes(this.excludes);
147         this.environment.setAssertion(this.asserts);
148         this.environment.setEncoding(this.charset);
149         this.doExecute();
150         Logger.info(this, "Qulice quality check completed");
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      */
165     protected abstract void doExecute() throws MojoFailureException;
166 
167     /**
168      * Get the environment.
169      * @return The environment
170      */
171     protected final MavenEnvironment env() {
172         return this.environment;
173     }
174 }