1 | |
package org.apache.maven.artifact.ant; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.math.BigInteger; |
23 | |
import java.security.MessageDigest; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import org.apache.maven.model.Repository; |
29 | |
import org.apache.tools.ant.BuildException; |
30 | |
import org.apache.tools.ant.Project; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 1 | public abstract class AbstractArtifactWithRepositoryTask |
37 | |
extends AbstractArtifactTask |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
|
42 | 1 | private List remoteRepositories = new ArrayList(); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static RemoteRepository getDefaultRemoteRepository() |
49 | |
{ |
50 | |
|
51 | 0 | RemoteRepository remoteRepository = new RemoteRepository(); |
52 | 0 | remoteRepository.setId( "central" ); |
53 | 0 | remoteRepository.setUrl( "http://repo1.maven.org/maven2" ); |
54 | 0 | RepositoryPolicy snapshots = new RepositoryPolicy(); |
55 | 0 | snapshots.setEnabled( false ); |
56 | 0 | remoteRepository.addSnapshots( snapshots ); |
57 | 0 | return remoteRepository; |
58 | |
} |
59 | |
|
60 | |
private static String statusAsString( RepositoryPolicy policy ) |
61 | |
{ |
62 | 0 | return ( policy == null ) || policy.isEnabled() ? "enabled" : "disabled"; |
63 | |
} |
64 | |
|
65 | |
protected List createRemoteArtifactRepositories() |
66 | |
{ |
67 | 0 | return createRemoteArtifactRepositories( null ); |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
protected List createRemoteArtifactRepositories( List pomRepositories ) |
78 | |
{ |
79 | 0 | List remoteRepositories = new ArrayList(); |
80 | 0 | remoteRepositories.addAll( getRemoteRepositories() ); |
81 | |
|
82 | 0 | if ( getRemoteRepositories().isEmpty() ) |
83 | |
{ |
84 | 0 | remoteRepositories.add( getDefaultRemoteRepository() ); |
85 | |
} |
86 | |
|
87 | 0 | if ( pomRepositories != null ) |
88 | |
{ |
89 | 0 | for ( Iterator i = pomRepositories.iterator(); i.hasNext(); ) |
90 | |
{ |
91 | 0 | Repository pomRepository = (Repository) i.next(); |
92 | |
|
93 | 0 | remoteRepositories.add( createAntRemoteRepository( pomRepository ) ); |
94 | 0 | } |
95 | |
} |
96 | |
|
97 | 0 | log( "Using remote repositories:", Project.MSG_VERBOSE ); |
98 | 0 | List list = new ArrayList(); |
99 | 0 | for ( Iterator i = remoteRepositories.iterator(); i.hasNext(); ) |
100 | |
{ |
101 | 0 | RemoteRepository remoteRepository = (RemoteRepository) i.next(); |
102 | 0 | updateRepositoryWithSettings( remoteRepository ); |
103 | |
|
104 | 0 | StringBuffer msg = new StringBuffer(); |
105 | 0 | msg.append( " - id=" + remoteRepository.getId() ); |
106 | 0 | msg.append( ", url=" + remoteRepository.getUrl() ); |
107 | 0 | msg.append( ", releases=" + statusAsString( remoteRepository.getReleases() ) ); |
108 | 0 | msg.append( ", snapshots=" + statusAsString( remoteRepository.getSnapshots() ) ); |
109 | 0 | if ( remoteRepository.getAuthentication() != null ) |
110 | |
{ |
111 | 0 | msg.append( ", authentication=" + remoteRepository.getAuthentication().getUserName() ); |
112 | |
} |
113 | 0 | if ( remoteRepository.getProxy() != null ) |
114 | |
{ |
115 | 0 | msg.append( ", proxy=" + remoteRepository.getProxy().getHost() ); |
116 | |
} |
117 | 0 | getProject().log( msg.toString(), Project.MSG_VERBOSE ); |
118 | |
|
119 | 0 | list.add( createRemoteArtifactRepository( remoteRepository ) ); |
120 | 0 | } |
121 | 0 | return list; |
122 | |
} |
123 | |
|
124 | |
public List getRemoteRepositories() |
125 | |
{ |
126 | 0 | return remoteRepositories; |
127 | |
} |
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
public void addConfiguredRemoteRepository( RemoteRepository remoteRepository ) |
137 | |
{ |
138 | |
|
139 | 0 | if ( remoteRepository.getUrl() == null ) |
140 | |
{ |
141 | 0 | throw new BuildException( "Each remote repository must specify a url." ); |
142 | |
} |
143 | 0 | if ( remoteRepository.getId() == null || remoteRepository.getId().equals( remoteRepository.getUrl() ) ) |
144 | |
{ |
145 | 0 | log( "Each remote repository must specify a unique id. For backward-compatibility, " |
146 | |
+ "a default id will be used. In future releases, a missing repository id will raise an error.", |
147 | |
Project.MSG_WARN ); |
148 | 0 | remoteRepository.setId( generateDefaultRepositoryId( remoteRepository ) ); |
149 | |
} |
150 | 0 | remoteRepositories.add( remoteRepository ); |
151 | 0 | } |
152 | |
|
153 | 1 | public final String MD5_ALGO_NAME = "MD5"; |
154 | |
|
155 | 1 | public final String UTF_ENC_NAME = "UTF-8"; |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public String generateDefaultRepositoryId( RemoteRepository repository ) |
166 | |
{ |
167 | |
try |
168 | |
{ |
169 | 1 | MessageDigest md = MessageDigest.getInstance( MD5_ALGO_NAME ); |
170 | 1 | md.update( repository.getUrl().getBytes( UTF_ENC_NAME ) ); |
171 | 1 | BigInteger digest = new BigInteger( md.digest() ); |
172 | 1 | return digest.toString( 16 ); |
173 | |
} |
174 | 0 | catch ( Exception e ) |
175 | |
{ |
176 | 0 | log( "Unable to generate unique repository Id: " + e, Project.MSG_WARN ); |
177 | 0 | return "default"; |
178 | |
} |
179 | |
} |
180 | |
} |