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;
028
029import java.util.Iterator;
030import java.util.List;
031
032/**
033 * Validate scm connection string.
034 *
035 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036 * @author Olivier Lamy
037 */
038@Mojo( name = "validate", requiresProject = false )
039@Execute( phase = LifecyclePhase.VALIDATE )
040public class ValidateMojo
041    extends AbstractScmMojo
042{
043    /**
044     * The scm connection url.
045     */
046    @Parameter( property = "scmConnection", defaultValue = "${project.scm.connection}" )
047    private String scmConnection;
048
049    @Parameter( defaultValue = "${project}", readonly = true )
050    private MavenProject project;
051
052    /**
053     * The scm connection url for developers.
054     */
055    @Parameter( property = "scmDeveloperConnection", defaultValue = "${project.scm.developerConnection}" )
056    private String scmDeveloperConnection;
057
058    /**
059     * <em>(Subversion specific)</em> Enables checking that "URL" field returned by svn info matches what is specified
060     * under the scm tag.
061     */
062    @Parameter( property = "scmCheckWorkingDirectoryUrl", defaultValue = "false" )
063    // Actually unused in the code here. Present for doc purpose,
064    // see org.apache.maven.scm.provider.svn.AbstractSvnScmProvider.CHECK_WORKING_DIRECTORY_URL
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( "scmCheckWorkingDirectoryUrl.currentWorkingDirectory",
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}