View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import javax.inject.Inject;
22  
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.command.status.StatusScmResult;
30  import org.apache.maven.scm.manager.ScmManager;
31  import org.apache.maven.scm.repository.ScmRepository;
32  import org.apache.maven.settings.crypto.SettingsDecrypter;
33  
34  /**
35   * This mojo will fail the build if there is any local modifications.
36   *
37   * @author Olivier Lamy
38   * @since 1.2
39   */
40  @Mojo(name = "check-local-modification")
41  public class CheckLocalModificationsMojo extends AbstractScmMojo {
42  
43      /**
44       * Custom error message.
45       */
46      @Parameter(
47              property = "scm.checkLocalModification.errorMessage",
48              defaultValue = "The build will stop as there is local modifications")
49      private String errorMessage;
50  
51      /**
52       * Skip the check for local modifications if set to {@code true}.
53       */
54      @Parameter(property = "scm.checkLocalModification.skip", defaultValue = "false")
55      private boolean skip;
56  
57      @Inject
58      public CheckLocalModificationsMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
59          super(manager, settingsDecrypter);
60      }
61  
62      public void execute() throws MojoExecutionException {
63          if (skip) {
64              getLog().info("check-local-modification execution has been skipped");
65              return;
66          }
67          super.execute();
68  
69          try {
70              ScmRepository repository = getScmRepository();
71              StatusScmResult result = getScmManager().status(repository, getFileSet());
72  
73              if (!result.isSuccess()) {
74                  throw new MojoExecutionException(
75                          "Unable to check for local modifications : " + result.getProviderMessage());
76              }
77  
78              if (!result.getChangedFiles().isEmpty()) {
79                  throw new MojoExecutionException(errorMessage);
80              }
81          } catch (IOException | ScmException e) {
82              throw new MojoExecutionException(e.getMessage(), e);
83          }
84      }
85  }