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 * Updates all projects in a multi project build. This is useful for users who have adopted the flat project structure
038 * where the aggregator project is a sibling of the sub projects rather than sitting in the parent directory.
039 */
040@Mojo(name = "update-subprojects")
041public class UpdateSubprojectsMojo extends AbstractScmMojo {
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    @Inject
067    public UpdateSubprojectsMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
068        super(manager, settingsDecrypter);
069    }
070
071    /** {@inheritDoc} */
072    public void execute() throws MojoExecutionException {
073        super.execute();
074
075        try {
076            ScmRepository repository = getScmRepository();
077
078            UpdateScmResult result =
079                    getScmManager().update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion));
080
081            checkResult(result);
082
083            if (result instanceof UpdateScmResultWithRevision) {
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, ((UpdateScmResultWithRevision) result).getRevision());
089                }
090            }
091        } catch (IOException | ScmException e) {
092            throw new MojoExecutionException("Cannot run update command : ", e);
093        }
094    }
095}