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