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