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  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.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.command.status.StatusScmResult;
30  import org.apache.maven.scm.repository.ScmRepository;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Display the modification status of the files in the configured scm url.
35   *
36   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
37   * @author Olivier Lamy
38   */
39  @Mojo( name = "status", aggregator = true )
40  public class StatusMojo
41      extends AbstractScmMojo
42  {
43      /** {@inheritDoc} */
44      public void execute()
45          throws MojoExecutionException
46      {
47          super.execute();
48  
49          try
50          {
51              ScmRepository repository = getScmRepository();
52  
53              StatusScmResult result = getScmManager().status( repository, getFileSet() );
54  
55              checkResult( result );
56  
57              File baseDir = getFileSet().getBasedir();
58  
59              // Determine the maximum length of the status column
60              int maxLen = 0;
61  
62              for ( ScmFile file : result.getChangedFiles() )
63              {
64                  maxLen = Math.max( maxLen, file.getStatus().toString().length() );
65              }
66  
67              for ( ScmFile file : result.getChangedFiles() )
68              {
69                  // right align all of the statuses
70                  getLog().info(
71                                 StringUtils.leftPad( file.getStatus().toString(), maxLen ) + " status for "
72                                     + getRelativePath( baseDir, file.getPath() ) );
73              }
74          }
75          catch ( IOException e )
76          {
77              throw new MojoExecutionException( "Cannot run status command : ", e );
78          }
79          catch ( ScmException e )
80          {
81              throw new MojoExecutionException( "Cannot run status command : ", e );
82          }
83      }
84  
85      /**
86       * Formats the filename so that it is a relative directory from the base.
87       *
88       * @param baseDir
89       * @param path
90       * @return The relative path
91       */
92      protected String getRelativePath( File baseDir, String path )
93      {
94          if ( path.equals( baseDir.getAbsolutePath() ) )
95          {
96              return ".";
97          }
98          else if ( path.indexOf( baseDir.getAbsolutePath() ) == 0 )
99          {
100             // the + 1 gets rid of a leading file separator
101             return path.substring( baseDir.getAbsolutePath().length() + 1 );
102         }
103         else
104         {
105             return path;
106         }
107     }
108 }