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.io.File;
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.execution.MavenSession;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.apache.maven.plugins.annotations.Component;
31  import org.apache.maven.plugins.annotations.Parameter;
32  import org.apache.maven.plugins.dependency.utils.DependencySilentLog;
33  import org.apache.maven.project.DefaultProjectBuildingRequest;
34  import org.apache.maven.project.MavenProject;
35  import org.apache.maven.project.ProjectBuildingRequest;
36  import org.codehaus.plexus.util.FileUtils;
37  import org.sonatype.plexus.build.incremental.BuildContext;
38  
39  /**
40   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
41   */
42  public abstract class AbstractDependencyMojo extends AbstractMojo {
43  
44      /**
45       * For IDE build support
46       */
47      @Component
48      private BuildContext buildContext;
49  
50      /**
51       * Skip plugin execution only during incremental builds (e.g. triggered from M2E).
52       *
53       * @since 3.4.0
54       * @see #skip
55       */
56      @Parameter(defaultValue = "false")
57      private boolean skipDuringIncrementalBuild;
58  
59      /**
60       * POM
61       */
62      @Parameter(defaultValue = "${project}", readonly = true, required = true)
63      private MavenProject project;
64  
65      /**
66       * Remote repositories which will be searched for artifacts.
67       */
68      @Parameter(defaultValue = "${project.remoteArtifactRepositories}", readonly = true, required = true)
69      private List<ArtifactRepository> remoteRepositories;
70  
71      /**
72       * Remote repositories which will be searched for plugins.
73       */
74      @Parameter(defaultValue = "${project.pluginArtifactRepositories}", readonly = true, required = true)
75      private List<ArtifactRepository> remotePluginRepositories;
76  
77      /**
78       * Contains the full list of projects in the reactor.
79       */
80      @Parameter(defaultValue = "${reactorProjects}", readonly = true)
81      protected List<MavenProject> reactorProjects;
82  
83      /**
84       * The Maven session
85       */
86      @Parameter(defaultValue = "${session}", readonly = true, required = true)
87      protected MavenSession session;
88  
89      /**
90       * If the plugin should be silent.
91       *
92       * @since 2.0
93       */
94      @Parameter(property = "silent", defaultValue = "false")
95      private boolean silent;
96  
97      /**
98       * Output absolute filename for resolved artifacts
99       *
100      * @since 2.0
101      */
102     @Parameter(property = "outputAbsoluteArtifactFilename", defaultValue = "false")
103     protected boolean outputAbsoluteArtifactFilename;
104 
105     /**
106      * Skip plugin execution completely.
107      *
108      * @since 2.7
109      */
110     @Parameter(property = "mdep.skip", defaultValue = "false")
111     private boolean skip;
112 
113     // Mojo methods -----------------------------------------------------------
114 
115     /*
116      * @see org.apache.maven.plugin.Mojo#execute()
117      */
118     @Override
119     public final void execute() throws MojoExecutionException, MojoFailureException {
120         if (isSkip()) {
121             getLog().info("Skipping plugin execution");
122             return;
123         }
124 
125         doExecute();
126     }
127 
128     /**
129      * @throws MojoExecutionException {@link MojoExecutionException}
130      * @throws MojoFailureException {@link MojoFailureException}
131      */
132     protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
133 
134     /**
135      * Does the actual copy of the file and logging.
136      *
137      * @param artifact represents the file to copy.
138      * @param destFile file name of destination file.
139      * @throws MojoExecutionException with a message if an error occurs.
140      */
141     protected void copyFile(File artifact, File destFile) throws MojoExecutionException {
142         try {
143             getLog().info("Copying "
144                     + (this.outputAbsoluteArtifactFilename ? artifact.getAbsolutePath() : artifact.getName()) + " to "
145                     + destFile);
146 
147             if (artifact.isDirectory()) {
148                 // usual case is a future jar packaging, but there are special cases: classifier and other packaging
149                 throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, "
150                         + "copy should be executed after packaging: see MDEP-187.");
151             }
152 
153             FileUtils.copyFile(artifact, destFile);
154             buildContext.refresh(destFile);
155         } catch (IOException e) {
156             throw new MojoExecutionException("Error copying artifact from " + artifact + " to " + destFile, e);
157         }
158     }
159 
160     /**
161      * @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
162      *         repositories, used to resolve artifacts.
163      */
164     public ProjectBuildingRequest newResolveArtifactProjectBuildingRequest() {
165         return newProjectBuildingRequest(remoteRepositories);
166     }
167 
168     /**
169      * @return Returns a new ProjectBuildingRequest populated from the current session and the current project remote
170      *         repositories, used to resolve plugins.
171      */
172     protected ProjectBuildingRequest newResolvePluginProjectBuildingRequest() {
173         return newProjectBuildingRequest(remotePluginRepositories);
174     }
175 
176     private ProjectBuildingRequest newProjectBuildingRequest(List<ArtifactRepository> repositories) {
177         ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
178 
179         buildingRequest.setRemoteRepositories(repositories);
180 
181         return buildingRequest;
182     }
183 
184     /**
185      * @return Returns the project.
186      */
187     public MavenProject getProject() {
188         return this.project;
189     }
190 
191     /**
192      * @return {@link #skip}
193      */
194     public boolean isSkip() {
195         if (skipDuringIncrementalBuild && buildContext.isIncremental()) {
196             return true;
197         }
198         return skip;
199     }
200 
201     /**
202      * @param skip {@link #skip}
203      */
204     public void setSkip(boolean skip) {
205         this.skip = skip;
206     }
207 
208     /**
209      * @return {@link #silent}
210      */
211     protected final boolean isSilent() {
212         return silent;
213     }
214 
215     /**
216      * @param silent {@link #silent}
217      */
218     public void setSilent(boolean silent) {
219         this.silent = silent;
220         if (silent) {
221             setLog(new DependencySilentLog());
222         }
223     }
224 }