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