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