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 java.io.File;
22  import java.io.IOException;
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.command.diff.DiffScmResult;
29  import org.apache.maven.scm.repository.ScmRepository;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  /**
33   * Display the difference of the working copy with the latest copy in the configured scm url.
34   *
35   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
36   */
37  @Mojo(name = "diff", aggregator = true)
38  public class DiffMojo extends AbstractScmMojo {
39      /**
40       * The version type (branch/tag/revision) of scmVersion.
41       */
42      @Parameter(property = "startScmVersionType")
43      private String startScmVersionType;
44  
45      /**
46       * The version (revision number/branch name/tag name).
47       */
48      @Parameter(property = "startScmVersion")
49      private String startScmVersion;
50  
51      /**
52       * The version type (branch/tag/revision) of scmVersion.
53       */
54      @Parameter(property = "endScmVersionType")
55      private String endScmVersionType;
56  
57      /**
58       * The version (revision number/branch name/tag name).
59       */
60      @Parameter(property = "endScmVersion")
61      private String endScmVersion;
62  
63      /**
64       * Output file name.
65       */
66      @Parameter(property = "outputFile", defaultValue = "${project.artifactId}.diff")
67      private File outputFile;
68  
69      /** {@inheritDoc} */
70      public void execute() throws MojoExecutionException {
71          super.execute();
72  
73          try {
74              ScmRepository repository = getScmRepository();
75  
76              DiffScmResult result = getScmManager()
77                      .diff(
78                              repository,
79                              getFileSet(),
80                              getScmVersion(startScmVersionType, startScmVersion),
81                              getScmVersion(endScmVersionType, endScmVersion));
82  
83              checkResult(result);
84  
85              getLog().info(result.getPatch());
86  
87              try {
88                  if (outputFile != null) {
89                      FileUtils.fileWrite(outputFile.getAbsolutePath(), result.getPatch());
90                  }
91              } catch (IOException e) {
92                  throw new MojoExecutionException("Can't write patch file.", e);
93              }
94          } catch (IOException | ScmException e) {
95              throw new MojoExecutionException("Cannot run diff command : ", e);
96          }
97      }
98  }