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