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.repository.ArtifactRepository;
25 import org.apache.maven.artifact.resolver.ArtifactResolver;
26 import org.apache.maven.model.Dependency;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29
30 import java.io.File;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.util.List;
34 import java.util.Properties;
35
36
37
38
39
40
41
42
43
44 public class ResolveMojo
45 extends AbstractMojo
46 {
47
48
49
50
51
52
53
54
55 private ArtifactRepository localRepository;
56
57
58
59
60
61
62
63
64 private List remoteRepositories;
65
66
67
68
69
70
71 private ArtifactResolver resolver;
72
73
74
75
76
77
78 private ArtifactFactory factory;
79
80
81
82
83
84
85 private Dependency[] dependencies;
86
87
88
89
90
91
92 private File propertiesFile;
93
94
95
96
97
98
99 public void execute()
100 throws MojoExecutionException
101 {
102 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving artifacts" );
103
104 Properties props = new Properties();
105
106 try
107 {
108 if ( dependencies != null )
109 {
110 for ( int i = 0; i < dependencies.length; i++ )
111 {
112 Dependency dependency = dependencies[i];
113
114 Artifact artifact =
115 factory.createArtifactWithClassifier( dependency.getGroupId(), dependency.getArtifactId(),
116 dependency.getVersion(), dependency.getType(),
117 dependency.getClassifier() );
118
119 getLog().info( "[MAVEN-CORE-IT-LOG] Resolving " + getId( artifact ) );
120
121 resolver.resolve( artifact, remoteRepositories, localRepository );
122
123 if ( artifact.getFile() != null )
124 {
125 props.setProperty( getId( artifact ), artifact.getFile().getPath() );
126 }
127
128 getLog().info( "[MAVEN-CORE-IT-LOG] " + artifact.getFile() );
129 }
130 }
131 }
132 catch ( Exception e )
133 {
134 throw new MojoExecutionException( "Failed to resolve artifacts: " + e.getMessage(), e );
135 }
136
137 if ( propertiesFile != null )
138 {
139 getLog().info( "[MAVEN-CORE-IT-LOG] Creating properties file " + propertiesFile );
140
141 try
142 {
143 propertiesFile.getParentFile().mkdirs();
144
145 FileOutputStream fos = new FileOutputStream( propertiesFile );
146 try
147 {
148 props.store( fos, "MAVEN-CORE-IT" );
149 }
150 finally
151 {
152 fos.close();
153 }
154 }
155 catch ( IOException e )
156 {
157 throw new MojoExecutionException( "Failed to create properties file: " + e.getMessage(), e );
158 }
159 }
160 }
161
162 private String getId( Artifact artifact )
163 {
164 artifact.isSnapshot();
165 return artifact.getId();
166 }
167
168 }