View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import java.io.IOException;
22  
23  import org.apache.maven.plugin.MojoExecutionException;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.command.update.UpdateScmResult;
29  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
30  import org.apache.maven.scm.repository.ScmRepository;
31  
32  /**
33   * Updates all projects in a multi project build. This is useful for users who have adopted the flat project structure
34   * where the aggregator project is a sibling of the sub projects rather than sitting in the parent directory.
35   */
36  @Mojo(name = "update-subprojects")
37  public class UpdateSubprojectsMojo extends AbstractScmMojo {
38      /**
39       * The version type (branch/tag/revision) of scmVersion.
40       */
41      @Parameter(property = "scmVersionType")
42      private String scmVersionType;
43  
44      /**
45       * The version (revision number/branch name/tag name).
46       */
47      @Parameter(property = "scmVersion")
48      private String scmVersion;
49  
50      /**
51       * The project property where to store the revision name.
52       */
53      @Parameter(property = "revisionKey", defaultValue = "scm.revision")
54      private String revisionKey;
55  
56      /**
57       * The Maven project.
58       */
59      @Parameter(defaultValue = "${project}", required = true, readonly = true)
60      private MavenProject project;
61  
62      /** {@inheritDoc} */
63      public void execute() throws MojoExecutionException {
64          super.execute();
65  
66          try {
67              ScmRepository repository = getScmRepository();
68  
69              UpdateScmResult result =
70                      getScmManager().update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion));
71  
72              checkResult(result);
73  
74              if (result instanceof UpdateScmResultWithRevision) {
75                  getLog().info("Storing revision in '" + revisionKey + "' project property.");
76  
77                  if (project.getProperties() != null) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
78                  {
79                      project.getProperties().put(revisionKey, ((UpdateScmResultWithRevision) result).getRevision());
80                  }
81              }
82          } catch (IOException | ScmException e) {
83              throw new MojoExecutionException("Cannot run update command : ", e);
84          }
85      }
86  }