1 package org.apache.maven.plugin.eclipse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.maven.AbstractMavenComponent;
26 import org.apache.maven.project.Dependency;
27 import org.apache.maven.project.Project;
28 import org.apache.maven.util.HttpUtils;
29
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.util.Iterator;
33
34 /**
35 * A base class to download sources achives.
36 *
37 * @author <a href="snicoll@apache.org">Stephane Nicoll</a>
38 */
39 public abstract class AbstractSourcesDownloader
40 extends AbstractMavenComponent
41 {
42
43 protected static final String PROXY_LOGINHOST = "maven.proxy.ntlm.host";
44
45 protected static final String PROXY_LOGINDOMAIN = "maven.proxy.ntlm.domain";
46
47 private static final Log log = LogFactory.getLog( AbstractSourcesDownloader.class );
48
49 private Project project;
50
51 private String groupId;
52
53 private String artifactId;
54
55 private String version;
56
57 public void downloadSources()
58 throws Exception
59 {
60 if ( project == null )
61 {
62 throw new NullPointerException( "project should be set." );
63 }
64
65 if ( groupId == null )
66 {
67 throw new NullPointerException( "groupId should be set." );
68 }
69
70 if ( artifactId == null )
71 {
72 throw new NullPointerException( "artifactId should be set." );
73 }
74
75 if ( version == null )
76 {
77 throw new NullPointerException( "version should be set." );
78 }
79
80 final String dependencyId = groupId + ":" + artifactId;
81 Dependency dependency = project.getDependency( dependencyId );
82 if ( dependency == null )
83 {
84 log.warn( "Could not retrieve dependency object for[" + dependencyId + "] - skipping" );
85 return;
86 }
87
88 String relativePath = buildRelativePath();
89 File localFile = new File( project.getContext().getMavenRepoLocal(), relativePath );
90 if ( isSnapshot() )
91 {
92 getRemoteArtifact( localFile, relativePath, dependency );
93 }
94 else
95 {
96 if ( localFile.exists() )
97 {
98 log.debug( getSourceName() + " for[" + groupId + ":" + artifactId + ":" + version +
99 "] is available in the local repository." );
100 }
101 else
102 {
103
104 getRemoteArtifact( localFile, relativePath, dependency );
105 }
106 }
107 }
108
109 protected abstract String buildRelativePath();
110
111 protected abstract String getSourceName();
112
113
114
115
116 public Project getProject()
117 {
118 return project;
119 }
120
121 public void setProject( final Project project )
122 {
123 this.project = project;
124 }
125
126 public String getGroupId()
127 {
128 return groupId;
129 }
130
131 public void setGroupId( final String groupId )
132 {
133 this.groupId = groupId;
134 }
135
136 public String getArtifactId()
137 {
138 return artifactId;
139 }
140
141 public void setArtifactId( final String artifactId )
142 {
143 this.artifactId = artifactId;
144 }
145
146 public String getVersion()
147 {
148 return version;
149 }
150
151 public void setVersion( final String version )
152 {
153 this.version = version;
154 }
155
156 protected boolean isSnapshot()
157 {
158 return version.endsWith( "SNAPSHOT" );
159 }
160
161 /**
162 * Retrieve a <code>remoteFile</code> from the maven remote repositories
163 * and store it at <code>destinationFile</code>
164 *
165 * @param destinationFile the destination file in the local repository
166 * @param relativePath the relative path to the dependency
167 * @return true if the retrieval succeeds, false otherwise.
168 */
169 protected boolean getRemoteArtifact( File destinationFile, String relativePath, Dependency relatedDependency )
170 {
171
172
173
174
175 File directory = destinationFile.getParentFile();
176
177 if ( !directory.exists() )
178 {
179 directory.mkdirs();
180 }
181
182 log.info( "Attempting to download " + getSourceName() + " for " + relatedDependency.getArtifact() );
183
184 boolean artifactFound = false;
185
186 for ( Iterator i = getProject().getContext().getMavenRepoRemote().iterator(); i.hasNext(); )
187 {
188 String remoteRepo = (String) i.next();
189
190 if ( remoteRepo.endsWith( "/" ) )
191 {
192 remoteRepo = remoteRepo.substring( 0, remoteRepo.length() - 1 );
193 }
194
195
196
197 String url = remoteRepo + "/" + relativePath;
198 url = StringUtils.replace( url, "//", "/" );
199
200 if ( !url.startsWith( "file" ) )
201 {
202 if ( url.startsWith( "https" ) )
203 {
204 url = StringUtils.replace( url, "https:/", "https://" );
205 }
206 else
207 {
208 url = StringUtils.replace( url, "http:/", "http://" );
209 }
210 }
211 log.debug( "Trying to download " + getSourceName() + " at " + url );
212
213
214
215 try
216 {
217 String loginHost = (String) getProject().getContext().getVariable( PROXY_LOGINHOST );
218 String loginDomain = (String) getProject().getContext().getVariable( PROXY_LOGINDOMAIN );
219 HttpUtils.getFile( url, destinationFile, false, true, getProject().getContext().getProxyHost(),
220 getProject().getContext().getProxyPort(),
221 getProject().getContext().getProxyUserName(),
222 getProject().getContext().getProxyPassword(), loginHost, loginDomain, true );
223
224
225
226 artifactFound = true;
227
228 if ( !isSnapshot() )
229 {
230 break;
231 }
232 }
233 catch ( FileNotFoundException e )
234 {
235
236
237
238
239
240 log.debug( "File not found on one of the repos", e );
241 }
242 catch ( Exception e )
243 {
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 String[] parsedUrl = HttpUtils.parseUrl( url );
263 log.warn( "Error retrieving artifact from [" + parsedUrl[2] + "]: " + e );
264 if ( parsedUrl[0] != null )
265 {
266 log.debug( "Username was '" + parsedUrl[0] + "', password hidden" );
267 }
268 log.debug( "Error details", e );
269 }
270 }
271
272 return artifactFound;
273 }
274
275 }