View Javadoc
1   package org.apache.maven.plugins.dependency.fromConfiguration;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugins.dependency.utils.filters.ArtifactItemFilter;
25  import org.apache.maven.plugins.dependency.utils.filters.DestFileFilter;
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  
30  import java.io.File;
31  import java.util.List;
32  
33  /**
34   * Goal that copies a list of artifacts from the repository to defined locations.
35   *
36   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
37   * @version $Id: CopyMojo.java 1807877 2017-09-09 10:35:59Z khmarbaise $
38   * @since 1.0
39   */
40  @Mojo( name = "copy", defaultPhase = LifecyclePhase.PROCESS_SOURCES, requiresProject = false, threadSafe = true )
41  public class CopyMojo
42      extends AbstractFromConfigurationMojo
43  {
44  
45      /**
46       * Strip artifact version during copy
47       */
48      @Parameter( property = "mdep.stripVersion", defaultValue = "false" )
49      private boolean stripVersion = false;
50  
51      /**
52       * Strip artifact classifier during copy
53       */
54      @Parameter( property = "mdep.stripClassifier", defaultValue = "false" )
55      private boolean stripClassifier = false;
56  
57      /**
58       * Prepend artifact groupId during copy
59       * 
60       * @since 2.7
61       */
62      @Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
63      private boolean prependGroupId = false;
64  
65      /**
66       * Use artifact baseVersion during copy
67       * 
68       * @since 2.7
69       */
70      @Parameter( property = "mdep.useBaseVersion", defaultValue = "false" )
71      private boolean useBaseVersion = false;
72  
73      /**
74       * The artifact to copy from command line. A string of the form groupId:artifactId:version[:packaging[:classifier]].
75       * Use {@link #artifactItems} within the POM configuration.
76       */
77      @SuppressWarnings( "unused" ) // marker-field, setArtifact(String) does the magic
78      @Parameter( property = "artifact" )
79      private String artifact;
80  
81      /**
82       * <i>not used in this goal</i>
83       */
84      @Parameter
85      protected boolean useJvmChmod = true;
86  
87      /**
88       * <i>not used in this goal</i>
89       */
90      @Parameter
91      protected boolean ignorePermissions;
92  
93      /**
94       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
95       * copyArtifact.
96       *
97       * @throws MojoExecutionException with a message if an error occurs.
98       * @see ArtifactItem
99       * @see #getArtifactItems
100      * @see #copyArtifact(ArtifactItem)
101      */
102     @Override
103     protected void doExecute()
104         throws MojoExecutionException, MojoFailureException
105     {
106         verifyRequirements();
107 
108         List<ArtifactItem> theArtifactItems =
109             getProcessedArtifactItems( new ProcessArtifactItemsRequest( stripVersion, prependGroupId, useBaseVersion,
110                                                                         stripClassifier ) );
111         for ( ArtifactItem artifactItem : theArtifactItems )
112         {
113             if ( artifactItem.isNeedsProcessing() )
114             {
115                 copyArtifact( artifactItem );
116             }
117             else
118             {
119                 this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
120             }
121         }
122     }
123 
124     /**
125      * Resolves the artifact from the repository and copies it to the specified location.
126      *
127      * @param artifactItem containing the information about the Artifact to copy.
128      * @throws MojoExecutionException with a message if an error occurs.
129      * @see #copyFile(File, File)
130      */
131     protected void copyArtifact( ArtifactItem artifactItem )
132         throws MojoExecutionException
133     {
134         File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );
135 
136         copyFile( artifactItem.getArtifact().getFile(), destFile );
137     }
138 
139     @Override
140     protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
141     {
142         ArtifactItemFilter destinationNameOverrideFilter =
143             new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
144                                 false, false, false, false, this.stripVersion, prependGroupId, useBaseVersion,
145                                 item.getOutputDirectory() );
146         return destinationNameOverrideFilter;
147     }
148 
149     /**
150      * @return Returns the stripVersion.
151      */
152     public boolean isStripVersion()
153     {
154         return this.stripVersion;
155     }
156 
157     /**
158      * @param stripVersion The stripVersion to set.
159      */
160     public void setStripVersion( boolean stripVersion )
161     {
162         this.stripVersion = stripVersion;
163     }
164 
165     /**
166      * @return Returns the stripClassifier.
167      */
168     public boolean isStripClassifier()
169     {
170         return this.stripClassifier;
171     }
172 
173     /**
174      * @param stripClassifier The stripClassifier to set.
175      */
176     public void setStripClassifier( boolean stripClassifier )
177     {
178         this.stripClassifier = stripClassifier;
179     }
180 
181     /**
182      * @param useBaseVersion The useBaseVersion to set.
183      */
184     public void setUseBaseVersion( boolean useBaseVersion )
185     {
186         this.useBaseVersion = useBaseVersion;
187     }
188 }