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 org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFile;
25  import org.apache.maven.scm.command.status.StatusScmResult;
26  import org.apache.maven.scm.repository.ScmRepository;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.util.Iterator;
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   * @version $Id: StatusMojo.java 538856 2007-05-17 09:58:55Z evenisse $
38   * @goal status
39   * @description Project status
40   * @aggregator
41   */
42  public class StatusMojo
43      extends AbstractScmMojo
44  {
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 ( Iterator iter = result.getChangedFiles().iterator(); iter.hasNext(); )
64              {
65                  ScmFile file = (ScmFile) iter.next();
66                  maxLen = Math.max( maxLen, file.getStatus().toString().length() );
67              }
68  
69              for ( Iterator iter = result.getChangedFiles().iterator(); iter.hasNext(); )
70              {
71                  ScmFile file = (ScmFile) iter.next();
72  
73                  // right align all of the statuses
74                  getLog().info( StringUtils.leftPad( file.getStatus().toString(), maxLen ) + " status for " +
75                      getRelativePath( baseDir, file.getPath() ) );
76              }
77          }
78          catch ( IOException e )
79          {
80              throw new MojoExecutionException( "Cannot run status command : ", e );
81          }
82          catch ( ScmException e )
83          {
84              throw new MojoExecutionException( "Cannot run status command : ", e );
85          }
86      }
87  
88      /**
89       * Formats the filename so that it is a relative directory from the base.
90       *
91       * @param baseDir
92       * @param path
93       * @return The relative path
94       */
95      protected String getRelativePath( File baseDir, String path )
96      {
97          if ( path.equals( baseDir.getAbsolutePath() ) )
98          {
99              return ".";
100         }
101         else if ( path.indexOf( baseDir.getAbsolutePath() ) == 0 )
102         {
103             // the + 1 gets rid of a leading file separator
104             return path.substring( baseDir.getAbsolutePath().length() + 1 );
105         }
106         else
107         {
108             return path;
109         }
110     }
111 }