View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Execute;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
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 extends AbstractScmMojo {
41      /**
42       * The scm connection url.
43       */
44      @Parameter(property = "scmConnection", defaultValue = "${project.scm.connection}")
45      private String scmConnection;
46  
47      @Parameter(defaultValue = "${project}", readonly = true)
48      private MavenProject project;
49  
50      /**
51       * The scm connection url for developers.
52       */
53      @Parameter(property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}")
54      private String scmDeveloperConnection;
55  
56      /**
57       * <em>(Subversion specific)</em> Enables checking that "URL" field returned by 'svn info' matches what is
58       * specified under the scm tag.
59       * @see AbstractSvnScmProvider#CURRENT_WORKING_DIRECTORY
60       */
61      @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
62      private boolean scmCheckWorkingDirectoryUrl;
63  
64      /**
65       * {@inheritDoc}
66       */
67      public void execute() throws MojoExecutionException {
68          super.execute();
69  
70          // check connectionUrl provided with cli
71          try {
72              validateConnection(getConnectionUrl(), "connectionUrl");
73          } catch (NullPointerException e) {
74              // nothing to do. connectionUrl isn't defined
75          }
76  
77          // check scm connection
78          if (scmConnection != null) {
79              validateConnection(scmConnection, "project.scm.connection");
80          }
81  
82          // Check scm developerConnection
83          if (scmDeveloperConnection != null) {
84              validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
85          }
86      }
87  
88      private void validateConnection(String connectionString, String type) throws MojoExecutionException {
89          if (scmCheckWorkingDirectoryUrl) {
90              System.setProperty(
91                      AbstractSvnScmProvider.CURRENT_WORKING_DIRECTORY,
92                      project.getFile().getParentFile().getAbsolutePath());
93          }
94          List<String> messages = getScmManager().validateScmRepository(connectionString);
95  
96          if (!messages.isEmpty()) {
97              getLog().error("Validation of scm url connection (" + type + ") failed :");
98  
99              Iterator<String> iter = messages.iterator();
100 
101             while (iter.hasNext()) {
102                 getLog().error(iter.next().toString());
103             }
104 
105             getLog().error("The invalid scm url connection: '" + connectionString + "'.");
106 
107             throw new MojoExecutionException("Command failed. Bad Scm URL.");
108         } else {
109             getLog().info(type + " scm connection string is valid.");
110         }
111     }
112 }