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
056     * under the scm tag.
057     */
058    @Parameter( property = "scmCheckWorkingDirectoryUrl", defaultValue = "false" )
059    // Actually unused in the code here. Present for doc purpose,
060    // see org.apache.maven.scm.provider.svn.AbstractSvnScmProvider.CHECK_WORKING_DIRECTORY_URL
061    private boolean scmCheckWorkingDirectoryUrl;
062
063    /** {@inheritDoc} */
064    public void execute()
065        throws MojoExecutionException
066    {
067        super.execute();
068
069        //check connectionUrl provided with cli
070        try
071        {
072            validateConnection( getConnectionUrl(), "connectionUrl" );
073        }
074        catch ( NullPointerException e )
075        {
076            // nothing to do. connectionUrl isn't defined
077        }
078
079        //check scm connection
080        if ( scmConnection != null )
081        {
082            validateConnection( scmConnection, "project.scm.connection" );
083        }
084
085        // Check scm developerConnection
086        if ( scmDeveloperConnection != null )
087        {
088            validateConnection( scmDeveloperConnection, "project.scm.developerConnection" );
089        }
090
091    }
092
093    private void validateConnection( String connectionString, String type )
094        throws MojoExecutionException
095    {
096        List<String> messages = getScmManager().validateScmRepository( connectionString );
097
098        if ( !messages.isEmpty() )
099        {
100            getLog().error( "Validation of scm url connection (" + type + ") failed :" );
101
102            Iterator<String> iter = messages.iterator();
103
104            while ( iter.hasNext() )
105            {
106                getLog().error( iter.next().toString() );
107            }
108
109            getLog().error( "The invalid scm url connection: '" + connectionString + "'." );
110
111            throw new MojoExecutionException( "Command failed. Bad Scm URL." );
112        }
113        else
114        {
115            getLog().info( type + " scm connection string is valid." );
116        }
117    }
118}