001package org.apache.maven.scm.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.plugins.annotations.Mojo;
024import org.apache.maven.plugins.annotations.Parameter;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.command.diff.DiffScmResult;
027import org.apache.maven.scm.repository.ScmRepository;
028import org.codehaus.plexus.util.FileUtils;
029
030import java.io.File;
031import java.io.IOException;
032
033/**
034 * Display the difference of the working copy with the latest copy in the configured scm url.
035 *
036 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
037 */
038@Mojo( name = "diff", aggregator = true )
039public class DiffMojo
040    extends AbstractScmMojo
041{
042    /**
043     * The version type (branch/tag/revision) of scmVersion.
044     */
045    @Parameter( property = "startScmVersionType" )
046    private String startScmVersionType;
047
048    /**
049     * The version (revision number/branch name/tag name).
050     */
051    @Parameter( property = "startScmVersion" )
052    private String startScmVersion;
053
054    /**
055     * The version type (branch/tag/revision) of scmVersion.
056     */
057    @Parameter( property = "endScmVersionType" )
058    private String endScmVersionType;
059
060    /**
061     * The version (revision number/branch name/tag name).
062     */
063    @Parameter( property = "endScmVersion" )
064    private String endScmVersion;
065
066    /**
067     * Output file name.
068     */
069    @Parameter( property = "outputFile", defaultValue = "${project.artifactId}.diff" )
070    private File outputFile;
071
072    /** {@inheritDoc} */
073    public void execute()
074        throws MojoExecutionException
075    {
076        super.execute();
077
078        try
079        {
080            ScmRepository repository = getScmRepository();
081
082            DiffScmResult result = getScmManager().diff( repository, getFileSet(),
083                                                         getScmVersion( startScmVersionType, startScmVersion ),
084                                                         getScmVersion( endScmVersionType, endScmVersion ) );
085
086            checkResult( result );
087
088            getLog().info( result.getPatch() );
089
090            try
091            {
092                if ( outputFile != null )
093                {
094                    FileUtils.fileWrite( outputFile.getAbsolutePath(), result.getPatch() );
095                }
096            }
097            catch ( IOException e )
098            {
099                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}