View Javadoc

1   package org.apache.maven.artifact.deployer;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
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         /* TODO: implement in FTP wagon
113          String passiveModeOn = (String) project.getContext().getVariable( "maven.repo." + id + ".passiveModeOn" );
114 
115          String compress = (String) project.getContext().getVariable( "maven.repo." + id + ".compress" );
116 
117          if ( passiveModeOn != null )
118          {
119          if ( "false".equalsIgnoreCase( passiveModeOn ) )
120          {
121          wagon.setPassiveModeOn( false );
122          }
123          }
124          if ( compress != null )
125          {
126          try
127          {
128          wagon.setCompress( new Boolean( compress ).booleanValue() );
129          }
130          catch ( Exception e )
131          {
132          throw new MavenException( "maven.repo." + id + ".compress should be a boolean" );
133          }
134          }
135          */
136     }
137 
138     public static void configureSshWagon( final Project project, final String id, final AbstractSshWagon wagon )
139     {
140         // DON'T check host key
141         // To do it we need to had more configuration settings (know_hosts file, ...)
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 }