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 | 40 | 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 | 46 | DependencyStatusSets dss = getDependencySets( this.failOnMissingClassifierArtifact ); |
79 | 43 | Set artifacts = dss.getResolvedDependencies(); |
80 | |
|
81 | 43 | if ( !useRepositoryLayout ) |
82 | |
{ |
83 | 42 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
84 | |
{ |
85 | 117 | copyArtifact( (Artifact) i.next(), this.stripVersion ); |
86 | |
} |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | |
try { |
91 | 1 | ArtifactRepository targetRepository = repositoryFactory.createDeploymentArtifactRepository( |
92 | |
"local", |
93 | |
outputDirectory.toURL().toExternalForm(), |
94 | |
new DefaultRepositoryLayout(), |
95 | |
false ); |
96 | 1 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
97 | |
{ |
98 | 9 | installArtifact( (Artifact) i.next(), targetRepository ); |
99 | |
} |
100 | |
} |
101 | 0 | catch ( MalformedURLException e ) |
102 | |
{ |
103 | 0 | throw new MojoExecutionException("Could not create outputDirectory repository", e); |
104 | 1 | } |
105 | |
} |
106 | |
|
107 | 43 | artifacts = dss.getSkippedDependencies(); |
108 | 43 | for ( Iterator i = artifacts.iterator(); i.hasNext(); ) |
109 | |
{ |
110 | 2 | Artifact artifact = (Artifact) i.next(); |
111 | 2 | getLog().info( artifact.getFile().getName() + " already exists in destination." ); |
112 | 2 | } |
113 | 43 | } |
114 | |
|
115 | |
private void installArtifact( Artifact artifact, ArtifactRepository targetRepository) |
116 | |
{ |
117 | |
try |
118 | |
{ |
119 | 9 | if ( "pom".equals( artifact.getType() ) ) |
120 | |
{ |
121 | 1 | installer.install( artifact.getFile(), artifact, targetRepository ); |
122 | 1 | installBaseSnapshot( artifact, targetRepository ); |
123 | |
} |
124 | |
else |
125 | |
{ |
126 | 8 | installer.install( artifact.getFile(), artifact, targetRepository ); |
127 | 8 | installBaseSnapshot( artifact, targetRepository ); |
128 | |
|
129 | 8 | if ( isCopyPom() ) |
130 | |
{ |
131 | 8 | Artifact pomArtifact = getResolvedPomArtifact( artifact ); |
132 | 8 | if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() ) |
133 | |
{ |
134 | 0 | installer.install( pomArtifact.getFile(), pomArtifact, targetRepository ); |
135 | 0 | installBaseSnapshot( pomArtifact, targetRepository ); |
136 | |
} |
137 | |
} |
138 | |
} |
139 | |
} |
140 | 0 | catch ( ArtifactInstallationException e ) |
141 | |
{ |
142 | 0 | getLog().info( e.getMessage() ); |
143 | 9 | } |
144 | 9 | } |
145 | |
|
146 | |
private void installBaseSnapshot( Artifact artifact, ArtifactRepository targetRepository ) |
147 | |
throws ArtifactInstallationException |
148 | |
{ |
149 | 9 | if ( artifact.isSnapshot() && !artifact.getBaseVersion().equals( artifact.getVersion() ) ) |
150 | |
{ |
151 | 2 | Artifact baseArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), |
152 | |
artifact.getBaseVersion(), artifact.getScope(), artifact.getType() ); |
153 | 2 | installer.install( artifact.getFile(), baseArtifact, targetRepository ); |
154 | |
} |
155 | 9 | } |
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 | 117 | String destFileName = DependencyUtil.getFormattedFileName( artifact, removeVersion ); |
179 | |
|
180 | |
File destDir; |
181 | 117 | destDir = DependencyUtil.getFormattedOutputDirectory( useSubDirectoryPerType, useSubDirectoryPerArtifact, |
182 | |
useRepositoryLayout, stripVersion, outputDirectory, |
183 | |
artifact ); |
184 | 117 | File destFile = new File( destDir, destFileName ); |
185 | |
|
186 | 117 | copyFile( artifact.getFile(), destFile ); |
187 | |
|
188 | 117 | if ( isCopyPom() ) |
189 | |
{ |
190 | |
|
191 | 117 | Artifact pomArtifact = getResolvedPomArtifact( artifact ); |
192 | |
|
193 | |
|
194 | 117 | if ( pomArtifact.getFile() != null && pomArtifact.getFile().exists() ) |
195 | |
{ |
196 | 22 | File pomDestFile = new File( destDir, DependencyUtil.getFormattedFileName( pomArtifact, removeVersion ) ); |
197 | 22 | copyFile( pomArtifact.getFile(), pomDestFile ); |
198 | |
} |
199 | |
} |
200 | 117 | } |
201 | |
|
202 | |
protected Artifact getResolvedPomArtifact( Artifact artifact ) { |
203 | 125 | Artifact pomArtifact = this.factory.createArtifact( artifact.getGroupId(), artifact.getArtifactId(), |
204 | |
artifact.getVersion(), "", "pom" ); |
205 | |
|
206 | |
try |
207 | |
{ |
208 | 125 | this.resolver.resolve( pomArtifact, this.remoteRepos, this.local ); |
209 | |
} |
210 | 103 | catch ( Exception e ) |
211 | |
{ |
212 | 103 | getLog().info( e.getMessage() ); |
213 | 22 | } |
214 | 125 | return pomArtifact; |
215 | |
} |
216 | |
|
217 | |
protected ArtifactsFilter getMarkedArtifactFilter() |
218 | |
{ |
219 | 47 | return new DestFileFilter( this.overWriteReleases, this.overWriteSnapshots, this.overWriteIfNewer, |
220 | |
this.useSubDirectoryPerArtifact, this.useSubDirectoryPerType, |
221 | |
this.useRepositoryLayout, this.stripVersion, this.outputDirectory ); |
222 | |
} |
223 | |
} |