001    package org.apache.maven.scm.plugin;
002    
003    import java.io.File;
004    
005    import org.apache.maven.plugin.MojoExecutionException;
006    import org.apache.maven.scm.ScmException;
007    import org.apache.maven.scm.ScmFileSet;
008    import org.apache.maven.scm.command.status.StatusScmResult;
009    import org.apache.maven.scm.repository.ScmRepository;
010    
011    /*
012     * Licensed to the Apache Software Foundation (ASF) under one
013     * or more contributor license agreements.  See the NOTICE file
014     * distributed with this work for additional information
015     * regarding copyright ownership.  The ASF licenses this file
016     * to you under the Apache License, Version 2.0 (the
017     * "License"); you may not use this file except in compliance
018     * with the License.  You may obtain a copy of the License at
019     *
020     *    http://www.apache.org/licenses/LICENSE-2.0
021     *
022     * Unless required by applicable law or agreed to in writing,
023     * software distributed under the License is distributed on an
024     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
025     * KIND, either express or implied.  See the License for the
026     * specific language governing permissions and limitations
027     * under the License.
028     */
029    
030    /**
031     * This mojo will fail the build if there is any local modifications
032     * @goal check-local-modification
033     * @author <a href="mailto:olamy@apache.org">olamy</a>
034     * @since 1.2
035     */
036    public class CheckLocalModificationsMojo
037        extends AbstractScmMojo
038    {
039    
040        /**
041         * Custom error message
042         *
043         * @parameter expression="${scm.checkLocalModification.errorMessage}" 
044         *            default-value="The build will stop as there is local modifications";
045         */
046        private String errorMessage; 
047        
048        /**
049         * Custom error message
050         *
051         * @parameter expression="${scm.checkLocalModification.skip}" default-value="false";
052         */    
053        private boolean skip;
054        
055        /**
056         * current directory
057         *
058         * @parameter default-value="${basedir}";
059         * @readonly
060         */     
061        private File baseDirectory;
062    
063        public void execute()
064            throws MojoExecutionException
065        {
066            if ( skip )
067            {
068                getLog().info( "check-local-modification execution has been skipped" );
069                return;
070            }
071            super.execute();
072    
073            StatusScmResult result = null;
074    
075            try
076            {
077                ScmRepository repository = getScmRepository();
078                result = getScmManager().status( repository, new ScmFileSet( baseDirectory ) );
079            }
080            catch ( ScmException e )
081            {
082                throw new MojoExecutionException( e.getMessage(), e );
083            }
084    
085            if ( !result.isSuccess() )
086            {
087                throw new MojoExecutionException( "Unable to check for local modifications :" + result.getProviderMessage() );
088            }
089    
090            if ( !result.getChangedFiles().isEmpty() )
091            {
092                getLog().error( errorMessage );
093                throw new MojoExecutionException( errorMessage );
094            }
095    
096        }
097        
098    }