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.io.File;
023
024import org.apache.maven.plugin.MojoExecutionException;
025import org.apache.maven.plugins.annotations.Mojo;
026import org.apache.maven.plugins.annotations.Parameter;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.command.status.StatusScmResult;
030import org.apache.maven.scm.repository.ScmRepository;
031
032/**
033 * This mojo will fail the build if there is any local modifications
034 *
035 * @author Olivier Lamy
036 * @since 1.2
037 */
038@Mojo( name = "check-local-modification" )
039public class CheckLocalModificationsMojo
040    extends AbstractScmMojo
041{
042
043    /**
044     * Custom error message
045     */
046    @Parameter( property = "scm.checkLocalModification.errorMessage",
047                    defaultValue = "The build will stop as there is local modifications" )
048    private String errorMessage; 
049    
050    /**
051     * Skip the check for local modifications if set to {@code true}.
052     */    
053    @Parameter( property = "scm.checkLocalModification.skip", defaultValue = "false" )
054    private boolean skip;
055    
056    /**
057     * current directory
058     */     
059    @Parameter( defaultValue = "${basedir}", readonly = true )
060    private File baseDirectory;
061
062    public void execute()
063        throws MojoExecutionException
064    {
065        if ( skip )
066        {
067            getLog().info( "check-local-modification execution has been skipped" );
068            return;
069        }
070        super.execute();
071
072        StatusScmResult result = null;
073
074        try
075        {
076            ScmRepository repository = getScmRepository();
077            result = getScmManager().status( repository, new ScmFileSet( baseDirectory ) );
078        }
079        catch ( ScmException e )
080        {
081            throw new MojoExecutionException( e.getMessage(), e );
082        }
083
084        if ( !result.isSuccess() )
085        {
086            throw new MojoExecutionException( "Unable to check for local modifications : "
087                                                + result.getProviderMessage() );
088        }
089
090        if ( !result.getChangedFiles().isEmpty() )
091        {
092            getLog().error( errorMessage );
093            throw new MojoExecutionException( errorMessage );
094        }
095
096    }
097    
098}