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.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Execute;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.Parameter;
27  
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * Validate scm connection string.
33   *
34   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
35   * @author Olivier Lamy
36   */
37  @Mojo( name = "validate", requiresProject = false )
38  @Execute( phase = LifecyclePhase.VALIDATE )
39  public class ValidateMojo
40      extends AbstractScmMojo
41  {
42      /**
43       * The scm connection url.
44       */
45      @Parameter( property = "scmConnection", defaultValue = "${project.scm.connection}" )
46      private String scmConnection;
47  
48      /**
49       * The scm connection url for developers.
50       */
51      @Parameter( property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}" )
52      private String scmDeveloperConnection;
53  
54      /** {@inheritDoc} */
55      public void execute()
56          throws MojoExecutionException
57      {
58          super.execute();
59  
60          //check connectionUrl provided with cli
61          try
62          {
63              validateConnection( getConnectionUrl(), "connectionUrl" );
64          }
65          catch ( NullPointerException e )
66          {
67              // nothing to do. connectionUrl isn't defined
68          }
69  
70          //check scm connection
71          if ( scmConnection != null )
72          {
73              validateConnection( scmConnection, "project.scm.connection" );
74          }
75  
76          // Check scm developerConnection
77          if ( scmDeveloperConnection != null )
78          {
79              validateConnection( scmDeveloperConnection, "project.scm.developerConnection" );
80          }
81  
82      }
83  
84      private void validateConnection( String connectionString, String type )
85          throws MojoExecutionException
86      {
87          List<String> messages = getScmManager().validateScmRepository( connectionString );
88  
89          if ( !messages.isEmpty() )
90          {
91              getLog().error( "Validation of scm url connection (" + type + ") failed :" );
92  
93              Iterator<String> iter = messages.iterator();
94  
95              while ( iter.hasNext() )
96              {
97                  getLog().error( iter.next().toString() );
98              }
99  
100             getLog().error( "The invalid scm url connection: '" + connectionString + "'." );
101 
102             throw new MojoExecutionException( "Command failed. Bad Scm URL." );
103         }
104         else
105         {
106             getLog().info( type + " scm connection string is valid." );
107         }
108     }
109 }