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