001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import java.io.IOException;
022
023import org.apache.maven.plugin.MojoExecutionException;
024import org.apache.maven.plugins.annotations.Mojo;
025import org.apache.maven.plugins.annotations.Parameter;
026import org.apache.maven.project.MavenProject;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.command.update.UpdateScmResult;
029import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
030import org.apache.maven.scm.repository.ScmRepository;
031
032/**
033 * Update the local working copy with the latest source from the configured scm url.
034 *
035 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
036 */
037@Mojo(name = "update", aggregator = true)
038public class UpdateMojo extends AbstractScmMojo {
039    /**
040     * The version type (branch/tag/revision) of scmVersion.
041     */
042    @Parameter(property = "scmVersionType")
043    private String scmVersionType;
044
045    /**
046     * The version (revision number/branch name/tag name).
047     */
048    @Parameter(property = "scmVersion")
049    private String scmVersion;
050
051    /**
052     * The project property where to store the revision name.
053     */
054    @Parameter(property = "revisionKey", defaultValue = "scm.revision")
055    private String revisionKey;
056
057    /**
058     * The Maven project.
059     */
060    @Parameter(defaultValue = "${project}", required = true, readonly = true)
061    private MavenProject project;
062
063    /**
064     * Run Changelog after update.
065     */
066    @Parameter(property = "runChangelog", defaultValue = "false")
067    private boolean runChangelog = false;
068
069    /** {@inheritDoc} */
070    public void execute() throws MojoExecutionException {
071        super.execute();
072
073        try {
074            ScmRepository repository = getScmRepository();
075
076            UpdateScmResult result = getScmManager()
077                    .update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion), runChangelog);
078
079            checkResult(result);
080
081            if (result instanceof UpdateScmResultWithRevision) {
082                String revision = ((UpdateScmResultWithRevision) result).getRevision();
083
084                getLog().info("Storing revision in '" + revisionKey + "' project property.");
085
086                if (project.getProperties() != null) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
087                {
088                    project.getProperties().put(revisionKey, revision);
089                }
090
091                getLog().info("Project at revision " + revision);
092            }
093        } catch (IOException | ScmException e) {
094            throw new MojoExecutionException("Cannot run update command : ", e);
095        }
096    }
097}