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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugin.MojoFailureException;
24  import org.apache.maven.plugin.dependency.utils.filters.ArtifactItemFilter;
25  import org.apache.maven.plugin.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 1400739 2012-10-21 23:05:22Z hboutemy $
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       * The artifact to copy from commandLine.
53       * Use {@link #artifactItems} within the pom-configuration.
54       */
55      @SuppressWarnings( "unused" ) //marker-field, setArtifact(String) does the magic
56      @Parameter( property = "artifact" )
57      private String artifact;
58  
59      /**
60       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
61       * copyArtifact.
62       *
63       * @throws MojoExecutionException with a message if an error occurs.
64       * @see ArtifactItem
65       * @see #getArtifactItems
66       * @see #copyArtifact(ArtifactItem)
67       */
68      public void execute()
69          throws MojoExecutionException, MojoFailureException
70      {
71          if ( isSkip() )
72          {
73              return;
74          }
75          
76          verifyRequirements();
77  
78          List<ArtifactItem> theArtifactItems = getProcessedArtifactItems( this.stripVersion );
79          for ( ArtifactItem artifactItem : theArtifactItems )
80          {
81              if ( artifactItem.isNeedsProcessing() )
82              {
83                  copyArtifact( artifactItem );
84              }
85              else
86              {
87                  this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
88              }
89          }
90      }
91  
92      /**
93       * Resolves the artifact from the repository and copies it to the specified location.
94       *
95       * @param artifactItem containing the information about the Artifact to copy.
96       * @throws MojoExecutionException with a message if an error occurs.
97       * @see DependencyUtil#copyFile(File, File, Log)
98       * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
99       */
100     protected void copyArtifact( ArtifactItem artifactItem )
101         throws MojoExecutionException
102     {
103         File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );
104 
105         copyFile( artifactItem.getArtifact().getFile(), destFile );
106     }
107 
108     protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
109     {
110         ArtifactItemFilter destinationNameOverrideFilter =
111             new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
112                                 false, false, false, false, this.stripVersion, item.getOutputDirectory() );
113         return destinationNameOverrideFilter;
114     }
115 
116     /**
117      * @return Returns the stripVersion.
118      */
119     public boolean isStripVersion()
120     {
121         return this.stripVersion;
122     }
123 
124     /**
125      * @param stripVersion The stripVersion to set.
126      */
127     public void setStripVersion( boolean stripVersion )
128     {
129         this.stripVersion = stripVersion;
130     }
131 
132 }