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 java.util.Iterator;
022import java.util.List;
023
024import org.apache.maven.plugin.MojoExecutionException;
025import org.apache.maven.plugins.annotations.Execute;
026import org.apache.maven.plugins.annotations.LifecyclePhase;
027import org.apache.maven.plugins.annotations.Mojo;
028import org.apache.maven.plugins.annotations.Parameter;
029import org.apache.maven.project.MavenProject;
030import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
031
032/**
033 * Validate scm connection string.
034 *
035 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036 * @author Olivier Lamy
037 */
038@Mojo(name = "validate", requiresProject = false)
039@Execute(phase = LifecyclePhase.VALIDATE)
040public class ValidateMojo extends AbstractScmMojo {
041    /**
042     * The scm connection url.
043     */
044    @Parameter(property = "scmConnection", defaultValue = "${project.scm.connection}")
045    private String scmConnection;
046
047    @Parameter(defaultValue = "${project}", readonly = true)
048    private MavenProject project;
049
050    /**
051     * The scm connection url for developers.
052     */
053    @Parameter(property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}")
054    private String scmDeveloperConnection;
055
056    /**
057     * <em>(Subversion specific)</em> Enables checking that "URL" field returned by 'svn info' matches what is
058     * specified under the scm tag.
059     * @see AbstractSvnScmProvider#CURRENT_WORKING_DIRECTORY
060     */
061    @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
062    private boolean scmCheckWorkingDirectoryUrl;
063
064    /**
065     * {@inheritDoc}
066     */
067    public void execute() throws MojoExecutionException {
068        super.execute();
069
070        // check connectionUrl provided with cli
071        try {
072            validateConnection(getConnectionUrl(), "connectionUrl");
073        } catch (NullPointerException e) {
074            // nothing to do. connectionUrl isn't defined
075        }
076
077        // check scm connection
078        if (scmConnection != null) {
079            validateConnection(scmConnection, "project.scm.connection");
080        }
081
082        // Check scm developerConnection
083        if (scmDeveloperConnection != null) {
084            validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
085        }
086    }
087
088    private void validateConnection(String connectionString, String type) throws MojoExecutionException {
089        if (scmCheckWorkingDirectoryUrl) {
090            System.setProperty(
091                    AbstractSvnScmProvider.CURRENT_WORKING_DIRECTORY,
092                    project.getFile().getParentFile().getAbsolutePath());
093        }
094        List<String> messages = getScmManager().validateScmRepository(connectionString);
095
096        if (!messages.isEmpty()) {
097            getLog().error("Validation of scm url connection (" + type + ") failed :");
098
099            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}