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