View Javadoc

1   package org.apache.maven.plugin.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 java.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter;
27  import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter;
28  
29  /**
30   * Goal that copies a list of artifacts from the repository to defined locations.
31   * 
32   * @goal copy
33   * @since 1.0
34   * @phase process-sources
35   * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
36   * @version $Id: CopyMojo.java 1085777 2011-03-26 18:13:19Z hboutemy $
37   */
38  public class CopyMojo
39      extends AbstractFromConfigurationMojo
40  {
41  
42      /**
43       * Strip artifact version during copy
44       * 
45       * @parameter expression="${mdep.stripVersion}" default-value="false"
46       * @parameter
47       */
48      private boolean stripVersion = false;
49  
50      /**
51       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
52       * copyArtifact.
53       * 
54       * @throws MojoExecutionException with a message if an error occurs.
55       * @see ArtifactItem
56       * @see #getArtifactItems
57       * @see #copyArtifact(ArtifactItem)
58       */
59      public void execute()
60          throws MojoExecutionException
61      {
62          if ( isSkip() )
63          {
64              return;
65          }
66  
67          List<ArtifactItem> theArtifactItems = getProcessedArtifactItems( this.stripVersion );
68          for ( ArtifactItem artifactItem : theArtifactItems )
69          {
70              if ( artifactItem.isNeedsProcessing() )
71              {
72                  copyArtifact( artifactItem );
73              }
74              else
75              {
76                  this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
77              }
78          }
79      }
80  
81      /**
82       * Resolves the artifact from the repository and copies it to the specified location.
83       * 
84       * @param artifactItem containing the information about the Artifact to copy.
85       * @throws MojoExecutionException with a message if an error occurs.
86       * @see DependencyUtil#copyFile(File, File, Log)
87       * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
88       */
89      protected void copyArtifact( ArtifactItem artifactItem )
90          throws MojoExecutionException
91      {
92          File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );
93  
94          copyFile( artifactItem.getArtifact().getFile(), destFile );
95      }
96  
97      protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
98      {
99          ArtifactItemFilter destinationNameOverrideFilter =
100             new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
101                                 false, false, false, false, this.stripVersion, item.getOutputDirectory() );
102         return destinationNameOverrideFilter;
103     }
104 
105     /**
106      * @return Returns the stripVersion.
107      */
108     public boolean isStripVersion()
109     {
110         return this.stripVersion;
111     }
112 
113     /**
114      * @param stripVersion The stripVersion to set.
115      */
116     public void setStripVersion( boolean stripVersion )
117     {
118         this.stripVersion = stripVersion;
119     }
120 
121 }