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