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  import org.apache.maven.project.MavenProject;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * Validate scm connection string.
34   *
35   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
36   * @author Olivier Lamy
37   */
38  @Mojo( name = "validate", requiresProject = false )
39  @Execute( phase = LifecyclePhase.VALIDATE )
40  public class ValidateMojo
41      extends AbstractScmMojo
42  {
43      /**
44       * The scm connection url.
45       */
46      @Parameter( property = "scmConnection", defaultValue = "${project.scm.connection}" )
47      private String scmConnection;
48  
49      @Parameter( defaultValue = "${project}", readonly = true )
50      private MavenProject project;
51  
52      /**
53       * The scm connection url for developers.
54       */
55      @Parameter( property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}" )
56      private String scmDeveloperConnection;
57  
58      /**
59       * <em>(Subversion specific)</em> Enables checking that "URL" field returned by svn info matches what is specified
60       * under the scm tag.
61       */
62      @Parameter( property = "scmCheckWorkingDirectoryUrl", defaultValue = "false" )
63      // Actually unused in the code here. Present for doc purpose,
64      // see org.apache.maven.scm.provider.svn.AbstractSvnScmProvider.CHECK_WORKING_DIRECTORY_URL
65      private boolean scmCheckWorkingDirectoryUrl;
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void execute()
71          throws MojoExecutionException
72      {
73          super.execute();
74  
75          //check connectionUrl provided with cli
76          try
77          {
78              validateConnection( getConnectionUrl(), "connectionUrl" );
79          }
80          catch ( NullPointerException e )
81          {
82              // nothing to do. connectionUrl isn't defined
83          }
84  
85          //check scm connection
86          if ( scmConnection != null )
87          {
88              validateConnection( scmConnection, "project.scm.connection" );
89          }
90  
91          // Check scm developerConnection
92          if ( scmDeveloperConnection != null )
93          {
94              validateConnection( scmDeveloperConnection, "project.scm.developerConnection" );
95          }
96  
97      }
98  
99      private void validateConnection( String connectionString, String type )
100         throws MojoExecutionException
101     {
102         if ( scmCheckWorkingDirectoryUrl )
103         {
104             System.setProperty( "scmCheckWorkingDirectoryUrl.currentWorkingDirectory",
105                                 project.getFile().getParentFile().getAbsolutePath() );
106         }
107         List<String> messages = getScmManager().validateScmRepository( connectionString );
108 
109         if ( !messages.isEmpty() )
110         {
111             getLog().error( "Validation of scm url connection (" + type + ") failed :" );
112 
113             Iterator<String> iter = messages.iterator();
114 
115             while ( iter.hasNext() )
116             {
117                 getLog().error( iter.next().toString() );
118             }
119 
120             getLog().error( "The invalid scm url connection: '" + connectionString + "'." );
121 
122             throw new MojoExecutionException( "Command failed. Bad Scm URL." );
123         }
124         else
125         {
126             getLog().info( type + " scm connection string is valid." );
127         }
128     }
129 }