001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import javax.inject.Inject;
022
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.maven.plugin.MojoExecutionException;
027import org.apache.maven.plugins.annotations.Execute;
028import org.apache.maven.plugins.annotations.LifecyclePhase;
029import org.apache.maven.plugins.annotations.Mojo;
030import org.apache.maven.plugins.annotations.Parameter;
031import org.apache.maven.project.MavenProject;
032import org.apache.maven.scm.manager.ScmManager;
033import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
034import org.apache.maven.settings.crypto.SettingsDecrypter;
035
036/**
037 * Validate scm connection string.
038 *
039 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
040 * @author Olivier Lamy
041 */
042@Mojo(name = "validate", requiresProject = false)
043@Execute(phase = LifecyclePhase.VALIDATE)
044public class ValidateMojo extends AbstractScmMojo {
045    /**
046     * The scm connection url.
047     */
048    @Parameter(property = "scmConnection", defaultValue = "${project.scm.connection}")
049    private String scmConnection;
050
051    @Parameter(defaultValue = "${project}", readonly = true)
052    private MavenProject project;
053
054    /**
055     * The scm connection url for developers.
056     */
057    @Parameter(property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}")
058    private String scmDeveloperConnection;
059
060    /**
061     * <em>(Subversion specific)</em> Enables checking that "URL" field returned by 'svn info' matches what is
062     * specified under the scm tag.
063     *
064     * @see AbstractSvnScmProvider#CURRENT_WORKING_DIRECTORY
065     */
066    @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
067    private boolean scmCheckWorkingDirectoryUrl;
068
069    @Inject
070    public ValidateMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
071        super(manager, settingsDecrypter);
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    public void execute() throws MojoExecutionException {
078        super.execute();
079
080        // check connectionUrl provided with cli
081        try {
082            validateConnection(getConnectionUrl(), "connectionUrl");
083        } catch (NullPointerException e) {
084            // nothing to do. connectionUrl isn't defined
085        }
086
087        // check scm connection
088        if (scmConnection != null) {
089            validateConnection(scmConnection, "project.scm.connection");
090        }
091
092        // Check scm developerConnection
093        if (scmDeveloperConnection != null) {
094            validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
095        }
096    }
097
098    private void validateConnection(String connectionString, String type) throws MojoExecutionException {
099        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}