1 package org.apache.maven.plugin.dependency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.net.MalformedURLException;
24 import java.util.Iterator;
25 import java.util.Set;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.artifact.installer.ArtifactInstallationException;
29 import org.apache.maven.artifact.installer.ArtifactInstaller;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
32 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.dependency.utils.DependencyStatusSets;
35 import org.apache.maven.plugin.dependency.utils.DependencyUtil;
36 import org.apache.maven.plugin.dependency.utils.filters.DestFileFilter;
37 import org.apache.maven.plugin.logging.Log;
38 import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
39
40
41
42
43
44
45
46
47
48
49
50
51 public class CopyDependenciesMojo
52 extends AbstractFromDependenciesMojo
53 {
54
55
56
57
58 protected ArtifactInstaller installer;
59
60
61
62
63 protected ArtifactRepositoryFactory repositoryFactory;
64
65
66
67
68
69
70
71
72
73
74
75 public void execute()
76 throws MojoExecutionException
77 {
78 DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact );
79 Set artifacts = dss.getResolvedDependencies();
80
81 if ( !useRepositoryLayout )
82 {
83 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
84 {
85 copyArtifact( (Artifact) i.next(), this.stripVersion );
86 }
87 }
88 else
89 {
90 try {
91 ArtifactRepository targetRepository = repositoryFactory.createDeploymentArtifactRepository(
92 "local",
93 outputDirectory.toURL().toExternalForm(),
94 new DefaultRepositoryLayout(),
95 false
96 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
97 {
98 installArtifact( (Artifact) i.next(), targetRepository );
99 }
100 }
101 catch ( MalformedURLException e )
102 {
103 throw new MojoExecutionException("Could not create outputDirectory repository", e);
104 }
105 }
106
107 artifacts = dss.getSkippedDependencies();
108 for ( Iterator i = artifacts.iterator(); i.hasNext(); )
109 {
110 Artifact artifact = (Artifact) i.next();
111 getLog().info( artifact.getFile().getName() + " already exists in destination." );
112 }
113 }
114
115 private void installArtifact( Artifact artifact, ArtifactRepository targetRepository)
116 {
117 try
118 {
119 if ( "pom".equals( artifact.getType() ) )
120 {
121 installer.install( artifact.getFile(), artifact, targetRepository );
122 installBaseSnapshot( artifact, targetRepository );
123 }
124 else
125 {
126 installer.install( artifact.getFile(), artifact, targetRepository );
127 installBaseSnapshot( artifact, targetRepository );
128
129 if ( isCopyPom() )
130 {
131 Artifact pomArtifact = getResolvedPomArtifact( artifact );
132 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
133 {
134 installer.install( pomArtifact.getFile(), pomArtifact, targetRepository );
135 installBaseSnapshot( pomArtifact, targetRepository );
136 }
137 }
138 }
139 }
140 catch ( ArtifactInstallationException e )
141 {
142 getLog().info( e.getMessage() );
143 }
144 }
145
146 private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository )
147 throws ArtifactInstallationException
148 {
149 if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) )
150 {
151 Artifact baseArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
152 artifact.getBaseVersion(), artifact.getScope(), artifact.getType() );
153 installer.install( artifact.getFile(), baseArtifact, targetRepository );
154 }
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 protected void copyArtifact( Artifact artifact, boolean removeVersion )
175 throws MojoExecutionException
176 {
177
178 String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion );
179
180 File destDir;
181 destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerType, useSubDirectoryPerArtifact,
182 useRepositoryLayout, stripVersion, outputDirectory,
183 artifact );
184 File destFile = new File( destDir, destFileName );
185
186 copyFile( artifact.getFile(), destFile );
187
188 if ( isCopyPom() )
189 {
190
191 Artifact pomArtifact = getResolvedPomArtifact( artifact );
192
193
194 if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() )
195 {
196 File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion ) );
197 copyFile( pomArtifact.getFile(), pomDestFile );
198 }
199 }
200 }
201
202 protected Artifact getResolvedPomArtifact( Artifact artifact ) {
203 Artifact pomArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(),
204 artifact.getVersion(), "", "pom" );
205
206 try
207 {
208 this.resolver.resolve( pomArtifact, this.remoteRepos, this.local );
209 }
210 catch ( Exception e )
211 {
212 getLog().info( e.getMessage() );
213 }
214 return pomArtifact;
215 }
216
217 protected ArtifactsFilter getMarkedArtifactFilter()
218 {
219 return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer,
220 this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType,
221 this.useRepositoryLayout, this.stripVersion, this.outputDirectory );
222 }
223 }