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