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.project.MavenProject;
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.command.update.UpdateScmResult;
028import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
029import org.apache.maven.scm.repository.ScmRepository;
030
031import java.io.IOException;
032
033/**
034 * Update the local working copy with the latest source from the configured scm url.
035 *
036 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
037 */
038@Mojo( name = "update", aggregator = true )
039public class UpdateMojo
040    extends AbstractScmMojo
041{
042    /**
043     * The version type (branch/tag/revision) of scmVersion.
044     */
045    @Parameter( property = "scmVersionType" )
046    private String scmVersionType;
047
048    /**
049     * The version (revision number/branch name/tag name).
050     */
051    @Parameter( property = "scmVersion" )
052    private String scmVersion;
053
054    /**
055     * The project property where to store the revision name.
056     */
057    @Parameter( property = "revisionKey", defaultValue = "scm.revision" )
058    private String revisionKey;
059
060    /**
061     * The Maven project.
062     */
063    @Parameter( defaultValue = "${project}", required = true, readonly = true )
064    private MavenProject project;
065
066    /**
067     * Run Changelog after update.
068     */
069    @Parameter( property = "runChangelog", defaultValue = "false" )
070    private boolean runChangelog = false;
071
072    /** {@inheritDoc} */
073    public void execute()
074        throws MojoExecutionException
075    {
076        super.execute();
077
078        try
079        {
080            ScmRepository repository = getScmRepository();
081
082            UpdateScmResult result = getScmManager().update( repository, getFileSet(),
083                                                             getScmVersion( scmVersionType, scmVersion ),
084                                                             runChangelog );
085
086            checkResult( result );
087
088            if ( result instanceof UpdateScmResultWithRevision )
089            {
090                String revision = ( (UpdateScmResultWithRevision) result ).getRevision();
091
092                getLog().info( "Storing revision in '" + revisionKey + "' project property." );
093
094                if ( project.getProperties() != null ) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
095                {
096                    project.getProperties().put( revisionKey, revision );
097                }
098
099                getLog().info( "Project at revision " + revision );
100            }
101        }
102        catch ( IOException e )
103        {
104            throw new MojoExecutionException( "Cannot run update command : ", e );
105        }
106        catch ( ScmException e )
107        {
108            throw new MojoExecutionException( "Cannot run update command : ", e );
109        }
110    }
111}