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