View Javadoc

1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.scm.ScmBranch;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFileSet;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.ScmRevision;
29  import org.apache.maven.scm.ScmTag;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.manager.ScmManager;
32  import org.apache.maven.scm.provider.ScmProviderRepository;
33  import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
34  import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
35  import org.apache.maven.scm.repository.ScmRepository;
36  import org.apache.maven.scm.repository.ScmRepositoryException;
37  import org.apache.maven.settings.Server;
38  import org.apache.maven.settings.Settings;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.util.Iterator;
44  import java.util.Map;
45  import java.util.Properties;
46  
47  /**
48   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
49   * @version $Id: AbstractScmMojo.java 654744 2008-05-09 09:59:03Z evenisse $
50   */
51  public abstract class AbstractScmMojo
52      extends AbstractMojo
53  {
54      /**
55       * The SCM connection URL.
56       *
57       * @parameter expression="${connectionUrl}" default-value="${project.scm.connection}"
58       */
59      private String connectionUrl;
60  
61      /**
62       * The SCM connection URL for developers.
63       *
64       * @parameter expression="${connectionUrl}" default-value="${project.scm.developerConnection}"
65       */
66      private String developerConnectionUrl;
67  
68      /**
69       * The type of connection to use (connection or developerConnection).
70       *
71       * @parameter expression="${connectionType}" default-value="connection"
72       */
73      private String connectionType;
74  
75      /**
76       * The working directory.
77       *
78       * @parameter expression="${workingDirectory}"
79       */
80      private File workingDirectory;
81  
82      /**
83       * The user name (used by svn, starteam and perforce protocol).
84       *
85       * @parameter expression="${username}"
86       */
87      private String username;
88  
89      /**
90       * The user password (used by svn, starteam and perforce protocol).
91       *
92       * @parameter expression="${password}"
93       */
94      private String password;
95  
96      /**
97       * The private key (used by java svn).
98       *
99       * @parameter expression="${privateKey}"
100      */
101     private String privateKey;
102 
103     /**
104      * The passphrase (used by java svn).
105      *
106      * @parameter expression="${passphrase}"
107      */
108     private String passphrase;
109 
110     /**
111      * The url of tags base directory (used by svn protocol). It is not
112      * necessary to set it if you use the standard svn layout
113      * (branches/tags/trunk).
114      *
115      * @parameter expression="${tagBase}"
116      */
117     private String tagBase;
118 
119     /**
120      * Comma separated list of includes file pattern.
121      *
122      * @parameter expression="${includes}"
123      */
124     private String includes;
125 
126     /**
127      * Comma separated list of excludes file pattern.
128      *
129      * @parameter expression="${excludes}"
130      */
131     private String excludes;
132 
133     /**
134      * @parameter expression="${component.org.apache.maven.scm.manager.ScmManager}"
135      * @required
136      * @readonly
137      */
138     private ScmManager manager;
139 
140     /**
141      * The base directory.
142      *
143      * @parameter expression="${basedir}"
144      * @required
145      */
146     private File basedir;
147 
148     /**
149      * @parameter expression="${settings}"
150      * @required
151      * @readonly
152      */
153     private Settings settings;
154 
155     /**
156      * List of System properties to pass to the JUnit tests.
157      *
158      * @parameter
159      */
160     private Properties systemProperties;
161 
162     /**
163      * List of provider implementations.
164      *
165      * @parameter
166      */
167     private Map providerImplementations;
168 
169     public void execute()
170         throws MojoExecutionException
171     {
172         if ( systemProperties != null )
173         {
174             // Add all system properties configured by the user
175             Iterator iter = systemProperties.keySet().iterator();
176 
177             while ( iter.hasNext() )
178             {
179                 String key = (String) iter.next();
180 
181                 String value = systemProperties.getProperty( key );
182 
183                 System.setProperty( key, value );
184             }
185         }
186 
187         if ( providerImplementations != null )
188         {
189             for ( Iterator i = providerImplementations.keySet().iterator(); i.hasNext(); )
190             {
191                 String providerType = (String) i.next();
192                 String providerImplementation = (String) providerImplementations.get( providerType );
193                 getLog().info( "Change the default '" + providerType + "' provider implementation to '" +
194                     providerImplementation + "'." );
195                 getScmManager().setScmProviderImplementation( providerType, providerImplementation );
196             }
197         }
198     }
199 
200     protected void setConnectionType( String connectionType )
201     {
202         this.connectionType = connectionType;
203     }
204 
205     public String getConnectionUrl()
206     {
207         boolean requireDeveloperConnection = !"connection".equals( connectionType.toLowerCase() );
208         if ( StringUtils.isNotEmpty( connectionUrl ) && !requireDeveloperConnection )
209         {
210             return connectionUrl;
211         }
212         else if ( StringUtils.isNotEmpty( developerConnectionUrl ) )
213         {
214             return developerConnectionUrl;
215         }
216         if ( requireDeveloperConnection )
217         {
218             throw new NullPointerException( "You need to define a developerConnectionUrl parameter" );
219         }
220         else
221         {
222             throw new NullPointerException( "You need to define a connectionUrl parameter" );
223         }
224     }
225 
226     public void setConnectionUrl( String connectionUrl )
227     {
228         this.connectionUrl = connectionUrl;
229     }
230 
231     public File getWorkingDirectory()
232     {
233         if ( workingDirectory == null )
234         {
235             return basedir;
236         }
237 
238         return workingDirectory;
239     }
240 
241     public void setWorkingDirectory( File workingDirectory )
242     {
243         this.workingDirectory = workingDirectory;
244     }
245 
246     public ScmManager getScmManager()
247     {
248         return manager;
249     }
250 
251     public ScmFileSet getFileSet()
252         throws IOException
253     {
254         if ( includes != null || excludes != null )
255         {
256             return new ScmFileSet( getWorkingDirectory(), includes, excludes );
257         }
258         else
259         {
260             return new ScmFileSet( getWorkingDirectory() );
261         }
262     }
263 
264     public ScmRepository getScmRepository()
265         throws ScmException
266     {
267         ScmRepository repository;
268 
269         try
270         {
271             repository = getScmManager().makeScmRepository( getConnectionUrl() );
272 
273             ScmProviderRepository providerRepo = repository.getProviderRepository();
274 
275             if ( !StringUtils.isEmpty( username ) )
276             {
277                 providerRepo.setUser( username );
278             }
279 
280             if ( !StringUtils.isEmpty( password ) )
281             {
282                 providerRepo.setPassword( password );
283             }
284 
285             if ( repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost )
286             {
287                 ScmProviderRepositoryWithHost repo = (ScmProviderRepositoryWithHost) repository.getProviderRepository();
288 
289                 loadInfosFromSettings( repo );
290 
291                 if ( !StringUtils.isEmpty( username ) )
292                 {
293                     repo.setUser( username );
294                 }
295 
296                 if ( !StringUtils.isEmpty( password ) )
297                 {
298                     repo.setPassword( password );
299                 }
300 
301                 if ( !StringUtils.isEmpty( privateKey ) )
302                 {
303                     repo.setPrivateKey( privateKey );
304                 }
305 
306                 if ( !StringUtils.isEmpty( passphrase ) )
307                 {
308                     repo.setPassphrase( passphrase );
309                 }
310             }
311 
312             if ( !StringUtils.isEmpty( tagBase ) && repository.getProvider().equals( "svn" ) )
313             {
314                 SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
315 
316                 svnRepo.setTagBase( tagBase );
317             }
318         }
319         catch ( ScmRepositoryException e )
320         {
321             if ( !e.getValidationMessages().isEmpty() )
322             {
323                 for ( Iterator i = e.getValidationMessages().iterator(); i.hasNext(); )
324                 {
325                     String message = (String) i.next();
326                     getLog().error( message );
327                 }
328             }
329 
330             throw new ScmException( "Can't load the scm provider.", e );
331         }
332         catch ( Exception e )
333         {
334             throw new ScmException( "Can't load the scm provider.", e );
335         }
336 
337         return repository;
338     }
339 
340     /**
341      * Load username password from settings if user has not set them in JVM properties
342      *
343      * @param repo
344      */
345     private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo )
346     {
347         if ( username == null || password == null )
348         {
349             String host = repo.getHost();
350 
351             int port = repo.getPort();
352 
353             if ( port > 0 )
354             {
355                 host += ":" + port;
356             }
357 
358             Server server = this.settings.getServer( host );
359 
360             if ( server != null )
361             {
362                 if ( username == null )
363                 {
364                     username = this.settings.getServer( host ).getUsername();
365                 }
366 
367                 if ( password == null )
368                 {
369                     password = this.settings.getServer( host ).getPassword();
370                 }
371 
372                 if ( privateKey == null )
373                 {
374                     privateKey = this.settings.getServer( host ).getPrivateKey();
375                 }
376 
377                 if ( passphrase == null )
378                 {
379                     passphrase = this.settings.getServer( host ).getPassphrase();
380                 }
381             }
382         }
383     }
384 
385     public void checkResult( ScmResult result )
386         throws MojoExecutionException
387     {
388         if ( !result.isSuccess() )
389         {
390             getLog().error( "Provider message:" );
391 
392             getLog().error( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
393 
394             getLog().error( "Command output:" );
395 
396             getLog().error( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
397 
398             throw new MojoExecutionException(
399                 "Command failed." + StringUtils.defaultString( result.getProviderMessage() ) );
400         }
401     }
402 
403     public String getIncludes()
404     {
405         return includes;
406     }
407 
408     public void setIncludes( String includes )
409     {
410         this.includes = includes;
411     }
412 
413     public String getExcludes()
414     {
415         return excludes;
416     }
417 
418     public void setExcludes( String excludes )
419     {
420         this.excludes = excludes;
421     }
422 
423     public ScmVersion getScmVersion( String versionType, String version )
424         throws MojoExecutionException
425     {
426         if ( StringUtils.isEmpty( versionType ) && StringUtils.isNotEmpty( version ) )
427         {
428             throw new MojoExecutionException( "You must specify the version type." );
429         }
430 
431         if ( StringUtils.isEmpty( version ) )
432         {
433             return null;
434         }
435 
436         if ( "branch".equals( versionType ) )
437         {
438             return new ScmBranch( version );
439         }
440 
441         if ( "tag".equals( versionType ) )
442         {
443             return new ScmTag( version );
444         }
445 
446         if ( "revision".equals( versionType ) )
447         {
448             return new ScmRevision( version );
449         }
450 
451         throw new MojoExecutionException( "Unknown '" + versionType + "' version type." );
452     }
453 }