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