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