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 java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Execute;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
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      /**
55       * <em>(Subversion specific)</em> Enables checking that "URL" field returned by svn info matches what is specified
56       * under the scm tag.
57       */
58      @Parameter( property = "scmCheckWorkingDirectoryUrl", defaultValue = "false" )
59      // Actually unused in the code here. Present for doc purpose,
60      // see org.apache.maven.scm.provider.svn.AbstractSvnScmProvider.CHECK_WORKING_DIRECTORY_URL
61      private boolean scmCheckWorkingDirectoryUrl;
62  
63      /** {@inheritDoc} */
64      public void execute()
65          throws MojoExecutionException
66      {
67          super.execute();
68  
69          //check connectionUrl provided with cli
70          try
71          {
72              validateConnection( getConnectionUrl(), "connectionUrl" );
73          }
74          catch ( NullPointerException e )
75          {
76              // nothing to do. connectionUrl isn't defined
77          }
78  
79          //check scm connection
80          if ( scmConnection != null )
81          {
82              validateConnection( scmConnection, "project.scm.connection" );
83          }
84  
85          // Check scm developerConnection
86          if ( scmDeveloperConnection != null )
87          {
88              validateConnection( scmDeveloperConnection, "project.scm.developerConnection" );
89          }
90  
91      }
92  
93      private void validateConnection( String connectionString, String type )
94          throws MojoExecutionException
95      {
96          List<String> messages = getScmManager().validateScmRepository( connectionString );
97  
98          if ( !messages.isEmpty() )
99          {
100             getLog().error( "Validation of scm url connection (" + type + ") failed :" );
101 
102             Iterator<String> iter = messages.iterator();
103 
104             while ( iter.hasNext() )
105             {
106                 getLog().error( iter.next().toString() );
107             }
108 
109             getLog().error( "The invalid scm url connection: '" + connectionString + "'." );
110 
111             throw new MojoExecutionException( "Command failed. Bad Scm URL." );
112         }
113         else
114         {
115             getLog().info( type + " scm connection string is valid." );
116         }
117     }
118 }