1 package org.apache.maven.plugin.coreit;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.resolver.ArtifactResolver;
27 import org.apache.maven.model.Dependency;
28 import org.apache.maven.plugin.AbstractMojo;
29 import org.apache.maven.plugin.MojoExecutionException;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.util.Iterator;
35 import java.util.LinkedHashSet;
36 import java.util.List;
37 import java.util.Properties;
38 import java.util.Set;
39
40
41
42
43
44
45
46
47
48 public class ResolveTransitiveMojo
49 extends AbstractMojo
50 {
51
52
53
54
55
56
57
58
59 private ArtifactRepository localRepository;
60
61
62
63
64
65
66
67
68 private List remoteRepositories;
69
70
71
72
73
74
75 private ArtifactResolver resolver;
76
77
78
79
80
81
82 private ArtifactFactory factory;
83
84
85
86
87
88
89 private ArtifactMetadataSource metadataSource;
90
91
92
93
94
95
96 private Dependency[] dependencies;
97
98
99
100
101
102
103 private File propertiesFile;
104
105
106
107
108
109
110 public void execute()
111 throws MojoExecutionException
112 {
113 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving artifacts" );
114
115 ResolverThread thread = new ResolverThread();
116 thread.start();
117 while ( thread.isAlive() )
118 {
119 try
120 {
121 thread.join();
122 }
123 catch ( InterruptedException e )
124 {
125 e.printStackTrace();
126 }
127 }
128
129 if ( thread.error != null )
130 {
131 throw new MojoExecutionException( "Failed to resolve artifacts: " + thread.error.getMessage(), thread.error );
132 }
133
134 if ( propertiesFile != null )
135 {
136 getLog().info( "[MAVEN-CORE-IT-LOG] Creating properties file " + propertiesFile );
137
138 try
139 {
140 propertiesFile.getParentFile().mkdirs();
141
142 FileOutputStream fos = new FileOutputStream( propertiesFile );
143 try
144 {
145 thread.props.store( fos, "MAVEN-CORE-IT" );
146 }
147 finally
148 {
149 fos.close();
150 }
151 }
152 catch ( IOException e )
153 {
154 throw new MojoExecutionException( "Failed to create properties file: " + e.getMessage(), e );
155 }
156 }
157 }
158
159 private String getId( Artifact artifact )
160 {
161 artifact.isSnapshot();
162 return artifact.getId();
163 }
164
165 class ResolverThread
166 extends Thread
167 {
168
169 Properties props = new Properties();
170
171 Exception error;
172
173 public void run()
174 {
175 if ( dependencies != null )
176 {
177 try
178 {
179 Set artifacts = new LinkedHashSet();
180
181 for ( int i = 0; i < dependencies.length; i++ )
182 {
183 Dependency dependency = dependencies[i];
184
185 Artifact artifact =
186 factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
187 dependency.getVersion(), dependency.getType(),
188 dependency.getClassifier() );
189
190 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving " + ResolveTransitiveMojo.this.getId( artifact ) );
191
192 artifacts.add( artifact );
193 }
194
195 Artifact origin = factory.createArtifact( "it", "it", "0.1", null, "pom" );
196
197 artifacts =
198 resolver.resolveTransitively( artifacts, origin, remoteRepositories, localRepository,
199 metadataSource ).getArtifacts();
200
201 for ( Iterator it = artifacts.iterator(); it.hasNext(); )
202 {
203 Artifact artifact = (Artifact) it.next();
204
205 if ( artifact.getFile() != null )
206 {
207 props.setProperty( ResolveTransitiveMojo.this.getId( artifact ),
208 artifact.getFile().getPath() );
209 }
210
211 getLog().info( "[MAVEN-CORE-IT-LOG] " + artifact.getFile() );
212 }
213 }
214 catch ( Exception e )
215 {
216 error = e;
217 }
218 }
219 }
220 }
221
222 }