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 org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.plugins.annotations.Execute;
024import org.apache.maven.plugins.annotations.LifecyclePhase;
025import org.apache.maven.plugins.annotations.Mojo;
026import org.apache.maven.plugins.annotations.Parameter;
027
028import java.util.Iterator;
029import java.util.List;
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    /** {@inheritDoc} */
055    public void execute()
056        throws MojoExecutionException
057    {
058        super.execute();
059
060        //check connectionUrl provided with cli
061        try
062        {
063            validateConnection( getConnectionUrl(), "connectionUrl" );
064        }
065        catch ( NullPointerException e )
066        {
067            // nothing to do. connectionUrl isn't defined
068        }
069
070        //check scm connection
071        if ( scmConnection != null )
072        {
073            validateConnection( scmConnection, "project.scm.connection" );
074        }
075
076        // Check scm developerConnection
077        if ( scmDeveloperConnection != null )
078        {
079            validateConnection( scmDeveloperConnection, "project.scm.developerConnection" );
080        }
081
082    }
083
084    private void validateConnection( String connectionString, String type )
085        throws MojoExecutionException
086    {
087        List<String> messages = getScmManager().validateScmRepository( connectionString );
088
089        if ( !messages.isEmpty() )
090        {
091            getLog().error( "Validation of scm url connection (" + type + ") failed :" );
092
093            Iterator<String> iter = messages.iterator();
094
095            while ( iter.hasNext() )
096            {
097                getLog().error( iter.next().toString() );
098            }
099
100            getLog().error( "The invalid scm url connection: '" + connectionString + "'." );
101
102            throw new MojoExecutionException( "Command failed. Bad Scm URL." );
103        }
104        else
105        {
106            getLog().info( type + " scm connection string is valid." );
107        }
108    }
109}