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 * Updates all projects in a multi project build. This is useful for users who have adopted the flat project structure
034 * where the aggregator project is a sibling of the sub projects rather than sitting in the parent directory.
035 */
036@Mojo(name = "update-subprojects")
037public class UpdateSubprojectsMojo extends AbstractScmMojo {
038    /**
039     * The version type (branch/tag/revision) of scmVersion.
040     */
041    @Parameter(property = "scmVersionType")
042    private String scmVersionType;
043
044    /**
045     * The version (revision number/branch name/tag name).
046     */
047    @Parameter(property = "scmVersion")
048    private String scmVersion;
049
050    /**
051     * The project property where to store the revision name.
052     */
053    @Parameter(property = "revisionKey", defaultValue = "scm.revision")
054    private String revisionKey;
055
056    /**
057     * The Maven project.
058     */
059    @Parameter(defaultValue = "${project}", required = true, readonly = true)
060    private MavenProject project;
061
062    /** {@inheritDoc} */
063    public void execute() throws MojoExecutionException {
064        super.execute();
065
066        try {
067            ScmRepository repository = getScmRepository();
068
069            UpdateScmResult result =
070                    getScmManager().update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion));
071
072            checkResult(result);
073
074            if (result instanceof UpdateScmResultWithRevision) {
075                getLog().info("Storing revision in '" + revisionKey + "' project property.");
076
077                if (project.getProperties() != null) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
078                {
079                    project.getProperties().put(revisionKey, ((UpdateScmResultWithRevision) result).getRevision());
080                }
081            }
082        } catch (IOException | ScmException e) {
083            throw new MojoExecutionException("Cannot run update command : ", e);
084        }
085    }
086}