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 1451088 2013-02-28 04:22:41Z brianf $
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       * Prepend artifact groupId during copy
53       * @since 2.7
54       */
55      @Parameter( property = "mdep.prependGroupId", defaultValue = "false" )
56      private boolean prependGroupId = false;
57  
58      /**
59       * Use artifact baseVersion during copy
60       * @since 2.7
61       */
62      @Parameter( property = "mdep.useBaseVersion", defaultValue = "false" )
63      private boolean useBaseVersion = false;
64  
65      /**
66       * The artifact to copy from commandLine.
67       * Use {@link #artifactItems} within the pom-configuration.
68       */
69      @SuppressWarnings( "unused" ) //marker-field, setArtifact(String) does the magic
70      @Parameter( property = "artifact" )
71      private String artifact;
72  
73      /**
74       * Main entry into mojo. This method gets the ArtifactItems and iterates through each one passing it to
75       * copyArtifact.
76       *
77       * @throws MojoExecutionException with a message if an error occurs.
78       * @see ArtifactItem
79       * @see #getArtifactItems
80       * @see #copyArtifact(ArtifactItem)
81       */
82      protected void doExecute()
83          throws MojoExecutionException, MojoFailureException
84      {
85          verifyRequirements();
86  
87          List<ArtifactItem> theArtifactItems = getProcessedArtifactItems(
88              new ProcessArtifactItemsRequest( this.stripVersion, this.prependGroupId, this.useBaseVersion ) );
89          for ( ArtifactItem artifactItem : theArtifactItems )
90          {
91              if ( artifactItem.isNeedsProcessing() )
92              {
93                  copyArtifact( artifactItem );
94              }
95              else
96              {
97                  this.getLog().info( artifactItem + " already exists in " + artifactItem.getOutputDirectory() );
98              }
99          }
100     }
101 
102     /**
103      * Resolves the artifact from the repository and copies it to the specified location.
104      *
105      * @param artifactItem containing the information about the Artifact to copy.
106      * @throws MojoExecutionException with a message if an error occurs.
107      * @see DependencyUtil#copyFile(File, File, Log)
108      * @see DependencyUtil#getFormattedFileName(Artifact, boolean)
109      */
110     protected void copyArtifact( ArtifactItem artifactItem )
111         throws MojoExecutionException
112     {
113         File destFile = new File( artifactItem.getOutputDirectory(), artifactItem.getDestFileName() );
114 
115         copyFile( artifactItem.getArtifact().getFile(), destFile );
116     }
117 
118     protected ArtifactItemFilter getMarkedArtifactFilter( ArtifactItem item )
119     {
120         ArtifactItemFilter destinationNameOverrideFilter =
121             new DestFileFilter( this.isOverWriteReleases(), this.isOverWriteSnapshots(), this.isOverWriteIfNewer(),
122                                 false, false, false, false, this.stripVersion, item.getOutputDirectory() );
123         return destinationNameOverrideFilter;
124     }
125 
126     /**
127      * @return Returns the stripVersion.
128      */
129     public boolean isStripVersion()
130     {
131         return this.stripVersion;
132     }
133 
134     /**
135      * @param stripVersion The stripVersion to set.
136      */
137     public void setStripVersion( boolean stripVersion )
138     {
139         this.stripVersion = stripVersion;
140     }
141 
142     /**
143      * @param useBaseVersion The useBaseVersion to set.
144      */
145     public void setUseBaseVersion( boolean useBaseVersion )
146     {
147         this.useBaseVersion = useBaseVersion;
148     }
149 }