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