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.fromConfiguration;
20  
21  import javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.List;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugin.MojoFailureException;
32  import org.apache.maven.plugins.annotations.LifecyclePhase;
33  import org.apache.maven.plugins.annotations.Mojo;
34  import org.apache.maven.plugins.annotations.Parameter;
35  import org.apache.maven.plugins.dependency.utils.CopyUtil;
36  import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
37  import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
38  import org.apache.maven.project.MavenProject;
39  import org.eclipse.aether.RepositorySystem;
40  import org.sonatype.plexus.build.incremental.BuildContext;
41  
42  /**
43   * Goal that copies a list of artifacts from the repository to defined locations.
44   *
45   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
46   * @since 1.0
47   */
48  @Mojo(name = "copy", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true)
49  public class CopyMojo extends AbstractFromConfigurationMojo {
50  
51      private final CopyUtil copyUtil;
52  
53      /**
54       * Strip artifact version during copy.
55       */
56      @Parameter(property = "mdep.stripVersion", defaultValue = "false")
57      private boolean stripVersion = false;
58  
59      /**
60       * Strip artifact classifier during copy.
61       */
62      @Parameter(property = "mdep.stripClassifier", defaultValue = "false")
63      private boolean stripClassifier = false;
64  
65      /**
66       * Prepend artifact groupId during copy.
67       *
68       * @since 2.7
69       */
70      @Parameter(property = "mdep.prependGroupId", defaultValue = "false")
71      private boolean prependGroupId = false;
72  
73      /**
74       * Use artifact baseVersion during copy.
75       *
76       * @since 2.7
77       */
78      @Parameter(property = "mdep.useBaseVersion", defaultValue = "false")
79      private boolean useBaseVersion = false;
80  
81      /**
82       * The artifact to copy from command line. A string of the form groupId:artifactId:version[:packaging[:classifier]].
83       * Use {@link #artifactItems} within the POM configuration.
84       */
85      @SuppressWarnings("unused") // marker-field, setArtifact(String) does the magic
86      @Parameter(property = "artifact")
87      private String artifact;
88  
89      @Inject
90      public CopyMojo(
91              MavenSession session,
92              BuildContext buildContext,
93              MavenProject project,
94              ArtifactHandlerManager artifactHandlerManager,
95              CopyUtil copyUtil,
96              RepositorySystem repositorySystem) {
97          super(session, buildContext, project, artifactHandlerManager, repositorySystem);
98          this.copyUtil = copyUtil;
99      }
100 
101     /**
102      * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
103      * copyArtifact.
104      *
105      * @throws MojoExecutionException with a message if an error occurs
106      * @see ArtifactItem
107      * @see #getArtifactItems
108      * @see #copyArtifact(ArtifactItem)
109      */
110     @Override
111     protected void doExecute() throws MojoExecutionException, MojoFailureException {
112         verifyRequirements();
113 
114         List<ArtifactItem> theArtifactItems = getProcessedArtifactItems(
115                 new ProcessArtifactItemsRequest(stripVersion, prependGroupId, useBaseVersion, stripClassifier));
116 
117         for (ArtifactItem artifactItem : theArtifactItems) {
118             if (artifactItem.isNeedsProcessing()) {
119                 copyArtifact(artifactItem);
120             } else {
121                 getLog().info(artifactItem + " already exists in " + artifactItem.getOutputDirectory());
122             }
123         }
124     }
125 
126     /**
127      * Resolves the artifact from the repository and copies it to the specified location.
128      *
129      * @param artifactItem containing the information about the artifact to copy
130      * @throws MojoExecutionException with a message if an error occurs
131      * @see CopyUtil#copyArtifactFile(Artifact, File)
132      */
133     protected void copyArtifact(ArtifactItem artifactItem) throws MojoExecutionException {
134         File destFile = new File(artifactItem.getOutputDirectory(), artifactItem.getDestFileName());
135         if (destFile.exists()) {
136             getLog().warn("Overwriting " + destFile);
137         }
138         try {
139             copyUtil.copyArtifactFile(artifactItem.getArtifact(), destFile);
140         } catch (IOException e) {
141             throw new MojoExecutionException(
142                     "Failed to copy artifact '" + artifactItem.getArtifact() + "' ("
143                             + artifactItem.getArtifact().getFile() + ") to " + destFile,
144                     e);
145         }
146     }
147 
148     @Override
149     protected ArtifactItemFilter getMarkedArtifactFilter(ArtifactItem item) {
150         return new DestFileFilter(
151                 this.isOverWriteReleases(),
152                 this.isOverWriteSnapshots(),
153                 this.isOverWriteIfNewer(),
154                 false,
155                 false,
156                 false,
157                 false,
158                 this.stripVersion,
159                 prependGroupId,
160                 useBaseVersion,
161                 item.getOutputDirectory());
162     }
163 
164     /**
165      * @return returns the stripVersion
166      */
167     public boolean isStripVersion() {
168         return this.stripVersion;
169     }
170 
171     /**
172      * @param stripVersion the stripVersion to set
173      */
174     public void setStripVersion(boolean stripVersion) {
175         this.stripVersion = stripVersion;
176     }
177 
178     /**
179      * @return returns the stripClassifier
180      */
181     public boolean isStripClassifier() {
182         return this.stripClassifier;
183     }
184 
185     /**
186      * @param stripClassifier the stripClassifier to set
187      */
188     public void setStripClassifier(boolean stripClassifier) {
189         this.stripClassifier = stripClassifier;
190     }
191 
192     /**
193      * @param useBaseVersion the useBaseVersion to set
194      */
195     public void setUseBaseVersion(boolean useBaseVersion) {
196         this.useBaseVersion = useBaseVersion;
197     }
198 }