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       * @see AbstractSvnScmProvider#CURRENT_WORKING_DIRECTORY
64       */
65      @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
66      private boolean scmCheckWorkingDirectoryUrl;
67  
68      @Inject
69      public ValidateMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
70          super(manager, settingsDecrypter);
71      }
72  
73      /**
74       * {@inheritDoc}
75       */
76      public void execute() throws MojoExecutionException {
77          super.execute();
78  
79          // check connectionUrl provided with cli
80          try {
81              validateConnection(getConnectionUrl(), "connectionUrl");
82          } catch (NullPointerException e) {
83              // nothing to do. connectionUrl isn't defined
84          }
85  
86          // check scm connection
87          if (scmConnection != null) {
88              validateConnection(scmConnection, "project.scm.connection");
89          }
90  
91          // Check scm developerConnection
92          if (scmDeveloperConnection != null) {
93              validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
94          }
95      }
96  
97      private void validateConnection(String connectionString, String type) throws MojoExecutionException {
98          if (scmCheckWorkingDirectoryUrl) {
99              System.setProperty(
100                     AbstractSvnScmProvider.CURRENT_WORKING_DIRECTORY,
101                     project.getFile().getParentFile().getAbsolutePath());
102         }
103         List<String> messages = getScmManager().validateScmRepository(connectionString);
104 
105         if (!messages.isEmpty()) {
106             getLog().error("Validation of scm url connection (" + type + ") failed :");
107 
108             Iterator<String> iter = messages.iterator();
109 
110             while (iter.hasNext()) {
111                 getLog().error(iter.next().toString());
112             }
113 
114             getLog().error("The invalid scm url connection: '" + connectionString + "'.");
115 
116             throw new MojoExecutionException("Command failed. Bad Scm URL.");
117         } else {
118             getLog().info(type + " scm connection string is valid.");
119         }
120     }
121 }