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.artifact.repository.ArtifactRepository;
24  import org.apache.maven.execution.MavenSession;
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
31  import org.apache.maven.project.DefaultProjectBuildingRequest;
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.project.ProjectBuildingRequest;
34  import org.sonatype.plexus.build.incremental.BuildContext;
35  
36  /**
37   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
38   */
39  public abstract class AbstractDependencyMojo extends AbstractMojo {
40  
41      /**
42       * For IDE build support
43       */
44      @Component
45      private BuildContext buildContext;
46  
47      /**
48       * Skip plugin execution only during incremental builds (e.g. triggered from M2E).
49       *
50       * @since 3.4.0
51       * @see #skip
52       */
53      @Parameter(defaultValue = "false")
54      private boolean skipDuringIncrementalBuild;
55  
56      /**
57       * POM
58       */
59      @Component
60      private MavenProject project;
61  
62      /**
63       * Remote repositories which will be searched for artifacts.
64       */
65      @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
66      private List<ArtifactRepository> remoteRepositories;
67  
68      /**
69       * Remote repositories which will be searched for plugins.
70       */
71      @Parameter(defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true)
72      private List<ArtifactRepository> remotePluginRepositories;
73  
74      /**
75       * Contains the full list of projects in the reactor.
76       */
77      @Parameter(defaultValue = "${reactorProjects}", readonly = true)
78      protected List<MavenProject> reactorProjects;
79  
80      /**
81       * The Maven session
82       */
83      @Component
84      protected MavenSession session;
85  
86      /**
87       * If the plugin should be silent.
88       *
89       * @since 2.0
90       */
91      @Parameter(property = "silent", defaultValue = "false")
92      private boolean silent;
93  
94      /**
95       * Skip plugin execution completely.
96       *
97       * @since 2.7
98       */
99      @Parameter(property = "mdep.skip", defaultValue = "false")
100     private boolean skip;
101 
102     // Mojo methods -----------------------------------------------------------
103 
104     /*
105      * @see org.apache.maven.plugin.Mojo#execute()
106      */
107     @Override
108     public final void execute() throws MojoExecutionException, MojoFailureException {
109         if (isSkip()) {
110             getLog().info("Skipping plugin execution");
111             return;
112         }
113 
114         doExecute();
115     }
116 
117     /**
118      * @throws MojoExecutionException {@link MojoExecutionException}
119      * @throws MojoFailureException {@link MojoFailureException}
120      */
121     protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
122 
123     /**
124      * @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
125      *         repositories, used to resolve artifacts.
126      */
127     public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest() {
128         return newProjectBuildingRequest(remoteRepositories);
129     }
130 
131     /**
132      * @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
133      *         repositories, used to resolve plugins.
134      */
135     protected ProjectBuildingRequest newResolvePluginProjectBuildingRequest() {
136         return newProjectBuildingRequest(remotePluginRepositories);
137     }
138 
139     private ProjectBuildingRequest newProjectBuildingRequest(List<ArtifactRepository> repositories) {
140         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
141 
142         buildingRequest.setRemoteRepositories(repositories);
143 
144         return buildingRequest;
145     }
146 
147     /**
148      * @return Returns the project.
149      */
150     public MavenProject getProject() {
151         return this.project;
152     }
153 
154     /**
155      * @return {@link #skip}
156      */
157     public boolean isSkip() {
158         if (skipDuringIncrementalBuild && buildContext.isIncremental()) {
159             return true;
160         }
161         return skip;
162     }
163 
164     /**
165      * @param skip {@link #skip}
166      */
167     public void setSkip(boolean skip) {
168         this.skip = skip;
169     }
170 
171     /**
172      * @return {@link #silent}
173      */
174     protected final boolean isSilent() {
175         return silent;
176     }
177 
178     /**
179      * @param silent {@link #silent}
180      */
181     public void setSilent(boolean silent) {
182         this.silent = silent;
183         if (silent) {
184             setLog(new DependencySilentLog());
185         }
186     }
187 }