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