View Javadoc

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