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 org.apache.maven.execution.MavenSession;
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugin.MojoFailureException;
25  import org.apache.maven.plugin.logging.SystemStreamLog;
26  import org.apache.maven.plugins.annotations.Parameter;
27  import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
28  import org.apache.maven.project.MavenProject;
29  import org.sonatype.plexus.build.incremental.BuildContext;
30  
31  /**
32   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
33   */
34  public abstract class AbstractDependencyMojo extends AbstractMojo {
35  
36      /**
37       * The Maven session.
38       */
39      protected final MavenSession session;
40  
41      /**
42       * If the plugin should be silent.
43       *
44       * @since 2.0
45       * @deprecated to be removed in 4.0; use -q command line option instead
46       */
47      @Deprecated
48      @Parameter(property = "silent", defaultValue = "false")
49      private boolean silent;
50  
51      /**
52       * Skip plugin execution completely.
53       *
54       * @since 2.7
55       */
56      @Parameter(property = "mdep.skip", defaultValue = "false")
57      private boolean skip;
58  
59      /**
60       * Skip plugin execution only during incremental builds (e.g. triggered from M2E).
61       *
62       * @see #skip
63       * @since 3.4.0
64       */
65      @Parameter(defaultValue = "false")
66      private boolean skipDuringIncrementalBuild;
67  
68      /**
69       * For IDE build support.
70       */
71      private final BuildContext buildContext;
72  
73      /**
74       * POM.
75       */
76      private final MavenProject project;
77  
78      protected AbstractDependencyMojo(MavenSession session, BuildContext buildContext, MavenProject project) {
79          this.session = session;
80          this.buildContext = buildContext;
81          this.project = project;
82      }
83  
84      // Mojo methods -----------------------------------------------------------
85  
86      /*
87       * @see org.apache.maven.plugin.Mojo#execute()
88       */
89      @Override
90      public final void execute() throws MojoExecutionException, MojoFailureException {
91          if (isSkip()) {
92              getLog().info("Skipping plugin execution");
93              return;
94          }
95  
96          doExecute();
97      }
98  
99      /**
100      * @throws MojoExecutionException {@link MojoExecutionException}
101      * @throws MojoFailureException {@link MojoFailureException}
102      */
103     protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
104 
105     /**
106      * @return returns the project
107      */
108     public MavenProject getProject() {
109         return this.project;
110     }
111 
112     /**
113      * @return {@link #skip}
114      */
115     public boolean isSkip() {
116         if (skipDuringIncrementalBuild && buildContext.isIncremental()) {
117             return true;
118         }
119         return skip;
120     }
121 
122     /**
123      * @param skip {@link #skip}
124      */
125     public void setSkip(boolean skip) {
126         this.skip = skip;
127     }
128 
129     /**
130      * @return {@link #silent}
131      * @deprecated to be removed in 4.0
132      */
133     @Deprecated
134     protected final boolean isSilent() {
135         return silent;
136     }
137 
138     /**
139      * @param silent {@link #silent}
140      * @deprecated to be removed in 4.0; no API replacement, use -q command line option instead
141      */
142     @Deprecated
143     public void setSilent(boolean silent) {
144         this.silent = silent;
145         if (silent) {
146             setLog(new DependencySilentLog());
147         } else if (getLog() instanceof DependencySilentLog) {
148             setLog(new SystemStreamLog());
149         }
150     }
151 }