001    package 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    
022    import org.apache.maven.plugin.MojoExecutionException;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.command.diff.DiffScmResult;
025    import org.apache.maven.scm.repository.ScmRepository;
026    import org.codehaus.plexus.util.FileUtils;
027    
028    import java.io.File;
029    import java.io.IOException;
030    
031    /**
032     * Display the difference of the working copy with the latest copy in the configured scm url.
033     *
034     * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
035     * @version $Id: DiffMojo.java 1371922 2012-08-11 11:41:43Z rfscholte $
036     * @goal diff
037     * @aggregator
038     */
039    public class DiffMojo
040        extends AbstractScmMojo
041    {
042        /**
043         * The version type (branch/tag/revision) of scmVersion.
044         *
045         * @parameter expression="${startScmVersionType}"
046         */
047        private String startScmVersionType;
048    
049        /**
050         * The version (revision number/branch name/tag name).
051         *
052         * @parameter expression="${startScmVersion}"
053         */
054        private String startScmVersion;
055    
056        /**
057         * The version type (branch/tag/revision) of scmVersion.
058         *
059         * @parameter expression="${endScmVersionType}"
060         */
061        private String endScmVersionType;
062    
063        /**
064         * The version (revision number/branch name/tag name).
065         *
066         * @parameter expression="${endScmVersion}"
067         */
068        private String endScmVersion;
069    
070        /**
071         * Output file name.
072         *
073         * @parameter expression="${outputFile}"
074         * default-value="${project.artifactId}.diff"
075         */
076        private File outputFile;
077    
078        /** {@inheritDoc} */
079        public void execute()
080            throws MojoExecutionException
081        {
082            super.execute();
083    
084            try
085            {
086                ScmRepository repository = getScmRepository();
087    
088                DiffScmResult result = getScmManager().diff( repository, getFileSet(),
089                                                             getScmVersion( startScmVersionType, startScmVersion ),
090                                                             getScmVersion( endScmVersionType, endScmVersion ) );
091    
092                checkResult( result );
093    
094                getLog().info( result.getPatch() );
095    
096                try
097                {
098                    if ( outputFile != null )
099                    {
100                        FileUtils.fileWrite( outputFile.getAbsolutePath(), result.getPatch() );
101                    }
102                }
103                catch ( IOException e )
104                {
105                    throw new MojoExecutionException( "Can't write patch file.", e );
106                }
107            }
108            catch ( IOException e )
109            {
110                throw new MojoExecutionException( "Cannot run diff command : ", e );
111            }
112            catch ( ScmException e )
113            {
114                throw new MojoExecutionException( "Cannot run diff command : ", e );
115            }
116        }
117    }