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