001    package 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    
022    import org.apache.maven.plugin.MojoExecutionException;
023    
024    import java.util.Iterator;
025    import java.util.List;
026    
027    /**
028     * Validate scm connection string.
029     *
030     * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
031     * @author Olivier Lamy
032     *
033     * @goal validate
034     * @execute phase="validate"
035     * @requiresProject false
036     */
037    public class ValidateMojo
038        extends AbstractScmMojo
039    {
040        /**
041         * The scm connection url.
042         *
043         * @parameter expression="${scmConnection}" default-value="${project.scm.connection}"
044         */
045        private String scmConnection;
046    
047        /**
048         * The scm connection url for developers.
049         *
050         * @parameter expression="${scmDeveloperConnection}" default-value="${project.scm.developerConnection}"
051         */
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    }