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