View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency;
20  
21  import java.util.List;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugin.logging.SystemStreamLog;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
30  import org.apache.maven.project.MavenProject;
31  import org.sonatype.plexus.build.incremental.BuildContext;
32  
33  /**
34   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
35   */
36  public abstract class AbstractDependencyMojo extends AbstractMojo {
37  
38      /**
39       * Contains the full list of projects in the reactor.
40       */
41      @Parameter(defaultValue = "${reactorProjects}", readonly = true)
42      protected List<MavenProject> reactorProjects;
43  
44      /**
45       * The Maven session.
46       */
47      protected final MavenSession session;
48  
49      /**
50       * If the plugin should be silent.
51       *
52       * @since 2.0
53       * @deprecated to be removed in 4.0; use -q command line option instead
54       */
55      @Deprecated
56      @Parameter(property = "silent", defaultValue = "false")
57      private boolean silent;
58  
59      /**
60       * Skip plugin execution completely.
61       *
62       * @since 2.7
63       */
64      @Parameter(property = "mdep.skip", defaultValue = "false")
65      private boolean skip;
66  
67      /**
68       * Skip plugin execution only during incremental builds (e.g. triggered from M2E).
69       *
70       * @see #skip
71       * @since 3.4.0
72       */
73      @Parameter(defaultValue = "false")
74      private boolean skipDuringIncrementalBuild;
75  
76      /**
77       * For IDE build support.
78       */
79      private final BuildContext buildContext;
80  
81      /**
82       * POM.
83       */
84      private final MavenProject project;
85  
86      protected AbstractDependencyMojo(MavenSession session, BuildContext buildContext, MavenProject project) {
87          this.session = session;
88          this.buildContext = buildContext;
89          this.project = project;
90      }
91  
92      // Mojo methods -----------------------------------------------------------
93  
94      /*
95       * @see org.apache.maven.plugin.Mojo#execute()
96       */
97      @Override
98      public final void execute() throws MojoExecutionException, MojoFailureException {
99          if (isSkip()) {
100             getLog().info("Skipping plugin execution");
101             return;
102         }
103 
104         doExecute();
105     }
106 
107     /**
108      * @throws MojoExecutionException {@link MojoExecutionException}
109      * @throws MojoFailureException {@link MojoFailureException}
110      */
111     protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
112 
113     /**
114      * @return returns the project
115      */
116     public MavenProject getProject() {
117         return this.project;
118     }
119 
120     /**
121      * @return {@link #skip}
122      */
123     public boolean isSkip() {
124         if (skipDuringIncrementalBuild && buildContext.isIncremental()) {
125             return true;
126         }
127         return skip;
128     }
129 
130     /**
131      * @param skip {@link #skip}
132      */
133     public void setSkip(boolean skip) {
134         this.skip = skip;
135     }
136 
137     /**
138      * @return {@link #silent}
139      * @deprecated to be removed in 4.0
140      */
141     @Deprecated
142     protected final boolean isSilent() {
143         return silent;
144     }
145 
146     /**
147      * @param silent {@link #silent}
148      * @deprecated to be removed in 4.0; no API replacement, use -q command line option instead
149      */
150     @Deprecated
151     public void setSilent(boolean silent) {
152         this.silent = silent;
153         if (silent) {
154             setLog(new DependencySilentLog());
155         } else if (getLog() instanceof DependencySilentLog) {
156             setLog(new SystemStreamLog());
157         }
158     }
159 }