1 package org.apache.maven.artifact.deployer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.maven.MavenException;
24 import org.apache.maven.project.Project;
25 import org.apache.maven.wagon.authentication.AuthenticationInfo;
26 import org.apache.maven.wagon.providers.ftp.FtpWagon;
27 import org.apache.maven.wagon.providers.ssh.AbstractSshWagon;
28 import org.apache.maven.wagon.providers.ssh.external.ScpExternalWagon;
29 import org.apache.maven.wagon.providers.ssh.jsch.ScpWagon;
30 import org.apache.maven.wagon.providers.ssh.jsch.SftpWagon;
31 import org.apache.maven.wagon.providers.ssh.knownhost.NullKnownHostProvider;
32 import org.apache.maven.wagon.proxy.ProxyInfo;
33 import org.apache.maven.wagon.repository.Repository;
34 import org.apache.maven.wagon.repository.RepositoryPermissions;
35
36 /**
37 * Perform mapping between project's properties and attributes of Wagon Repository class.
38 *
39 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
40 * @version $Id: RepositoryBuilder.java 532339 2007-04-25 12:28:56Z ltheussl $
41 */
42 public class RepositoryBuilder
43 {
44 private static final Log LOG = LogFactory.getLog( RepositoryBuilder.class );
45
46 public static Repository getRepository( final Project project, final String id )
47 throws MavenException
48 {
49 final String url = (String) project.getContext().getVariable( "maven.repo." + id );
50 if ( url == null )
51 {
52 throw new MavenException( "URL not specified in property: maven.repo." + id );
53 }
54
55 final Repository repository = new Repository( id, url );
56
57 String dir = (String) project.getContext().getVariable( "maven.repo." + id + ".directory" );
58 if ( ( repository.getBasedir() != null ) && ( dir != null ) )
59 {
60 dir = dir.replace( '\\', '/' );
61 if ( !repository.getBasedir().endsWith( "/" ) && !dir.startsWith( "/" ) )
62 {
63 dir = "/" + dir;
64 }
65 dir = repository.getBasedir() + dir;
66 }
67
68 repository.setBasedir( dir );
69 if ( dir == null )
70 {
71 throw new MavenException( "Directory not specified in property: maven.repo." + id + ".directory" );
72 }
73
74 final String port = (String) project.getContext().getVariable( "maven.repo." + id + ".port" );
75 if ( port != null )
76 {
77 try
78 {
79 repository.setPort( Integer.valueOf( port ).intValue() );
80 }
81 catch ( final NumberFormatException e )
82 {
83 RepositoryBuilder.LOG.warn( "Invalid format for port: " + port );
84 }
85 }
86
87 final String remoteGroup = (String) project.getContext().getVariable( "maven.repo." + id + ".group" );
88 String remoteMode = (String) project.getContext().getVariable( "maven.repo." + id + ".mode" );
89 String remoteDirectoryMode = (String) project.getContext().getVariable( "maven.repo." + id + ".directory.mode" );
90
91 if ( remoteMode == null )
92 {
93 remoteMode = "g+w";
94 }
95
96 if ( remoteDirectoryMode == null )
97 {
98 remoteDirectoryMode = remoteMode;
99 }
100
101 final RepositoryPermissions permissions = new RepositoryPermissions();
102 permissions.setDirectoryMode( remoteDirectoryMode );
103 permissions.setFileMode( remoteMode );
104 permissions.setGroup( remoteGroup );
105 repository.setPermissions( permissions );
106
107 return repository;
108 }
109
110 public static void configureFtpWagon( final Project project, final String id, final FtpWagon wagon )
111 {
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 }
137
138 public static void configureSshWagon( final Project project, final String id, final AbstractSshWagon wagon )
139 {
140
141
142 final NullKnownHostProvider nkhp = new NullKnownHostProvider();
143 nkhp.setHostKeyChecking( "no" );
144 wagon.setKnownHostsProvider( nkhp );
145 }
146
147 public static void configureSftpWagon( final Project project, final String id, final SftpWagon wagon )
148 {
149 RepositoryBuilder.configureSshWagon( project, id, wagon );
150 }
151
152 public static void configureScpWagon( final Project project, final String id, final ScpWagon wagon )
153 {
154 RepositoryBuilder.configureSshWagon( project, id, wagon );
155 }
156
157 public static void configureSshExternalWagon( final Project project, final String id, final ScpExternalWagon wagon )
158 {
159 String scpExe = (String) project.getContext().getVariable( "maven.repo." + id + ".scp.executable" );
160 if ( scpExe == null )
161 {
162 scpExe = (String) project.getContext().getVariable( "maven.scp.executable" );
163 }
164
165 if ( scpExe != null )
166 {
167 wagon.setScpExecutable( scpExe );
168 }
169
170 String scpArgs = (String) project.getContext().getVariable( "maven.repo." + id + ".scp.args" );
171 if ( scpArgs == null )
172 {
173 scpArgs = (String) project.getContext().getVariable( "maven.scp.args" );
174 }
175
176 if ( scpArgs != null )
177 {
178 wagon.setScpArgs( scpArgs );
179 }
180
181 String sshExe = (String) project.getContext().getVariable( "maven.repo." + id + ".ssh.executable" );
182 if ( sshExe == null )
183 {
184 sshExe = (String) project.getContext().getVariable( "maven.ssh.executable" );
185 }
186
187 if ( sshExe != null )
188 {
189 wagon.setSshExecutable( sshExe );
190 }
191
192 String sshArgs = (String) project.getContext().getVariable( "maven.repo." + id + ".ssh.args" );
193 if ( sshArgs == null )
194 {
195 sshArgs = (String) project.getContext().getVariable( "maven.ssh.args" );
196 }
197
198 if ( sshArgs != null )
199 {
200 wagon.setSshArgs( sshArgs );
201 }
202 }
203
204 public static void getProxyInfo( final Project project )
205 {
206 final ProxyInfo proxyInfo = new ProxyInfo();
207
208 final String proxyHost = project.getContext().getProxyHost();
209 final String proxyUser = project.getContext().getProxyUserName();
210 final String proxyPassword = project.getContext().getProxyPassword();
211 final String proxyPort = project.getContext().getProxyPort();
212
213 if ( proxyPort != null )
214 {
215 try
216 {
217 proxyInfo.setPort( Integer.valueOf( proxyPort ).intValue() );
218 }
219 catch ( final NumberFormatException e )
220 {
221 RepositoryBuilder.LOG.warn( "Invalid format for port: " + proxyPort );
222 }
223 }
224
225 proxyInfo.setType( "http" );
226 proxyInfo.setHost( proxyHost );
227 proxyInfo.setNonProxyHosts( (String) project.getContext().getVariable( "maven.proxy.nonProxyHosts" ) );
228 proxyInfo.setNtlmDomain( (String) project.getContext().getVariable( "maven.proxy.ntlm.domain" ) );
229 proxyInfo.setNtlmHost( (String) project.getContext().getVariable( "maven.proxy.ntlm.host" ) );
230 proxyInfo.setUserName( proxyUser );
231 proxyInfo.setPassword( proxyPassword );
232 }
233
234 public static AuthenticationInfo getAuthenticationInfo( final Project project, final String id )
235 {
236 String username = (String) project.getContext().getVariable( "maven.repo." + id + ".username" );
237 final String password = (String) project.getContext().getVariable( "maven.repo." + id + ".password" );
238 final String passphrase = (String) project.getContext().getVariable( "maven.repo." + id + ".passphrase" );
239 final String privateKey = (String) project.getContext().getVariable( "maven.repo." + id + ".privatekey" );
240
241 if ( username == null )
242 {
243 username = (String) project.getContext().getVariable( "maven.username" );
244 }
245
246 final AuthenticationInfo authenticationInfo = new AuthenticationInfo();
247 authenticationInfo.setUserName( username );
248 authenticationInfo.setPassword( password );
249 authenticationInfo.setPrivateKey( privateKey );
250 authenticationInfo.setPassphrase( passphrase );
251
252 return authenticationInfo;
253 }
254 }