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