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     * @see AbstractSvnScmProvider#CURRENT_WORKING_DIRECTORY
064     */
065    @Parameter(property = "scmCheckWorkingDirectoryUrl", defaultValue = "false")
066    private boolean scmCheckWorkingDirectoryUrl;
067
068    @Inject
069    public ValidateMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
070        super(manager, settingsDecrypter);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    public void execute() throws MojoExecutionException {
077        super.execute();
078
079        // check connectionUrl provided with cli
080        try {
081            validateConnection(getConnectionUrl(), "connectionUrl");
082        } catch (NullPointerException e) {
083            // nothing to do. connectionUrl isn't defined
084        }
085
086        // check scm connection
087        if (scmConnection != null) {
088            validateConnection(scmConnection, "project.scm.connection");
089        }
090
091        // Check scm developerConnection
092        if (scmDeveloperConnection != null) {
093            validateConnection(scmDeveloperConnection, "project.scm.developerConnection");
094        }
095    }
096
097    private void validateConnection(String connectionString, String type) throws MojoExecutionException {
098        if (scmCheckWorkingDirectoryUrl) {
099            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}