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 javax.inject.Inject;
22  
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.command.update.UpdateScmResult;
31  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
32  import org.apache.maven.scm.manager.ScmManager;
33  import org.apache.maven.scm.repository.ScmRepository;
34  import org.apache.maven.settings.crypto.SettingsDecrypter;
35  
36  /**
37   * Updates all projects in a multi project build. This is useful for users who have adopted the flat project structure
38   * where the aggregator project is a sibling of the sub projects rather than sitting in the parent directory.
39   */
40  @Mojo(name = "update-subprojects")
41  public class UpdateSubprojectsMojo extends AbstractScmMojo {
42      /**
43       * The version type (branch/tag/revision) of scmVersion.
44       */
45      @Parameter(property = "scmVersionType")
46      private String scmVersionType;
47  
48      /**
49       * The version (revision number/branch name/tag name).
50       */
51      @Parameter(property = "scmVersion")
52      private String scmVersion;
53  
54      /**
55       * The project property where to store the revision name.
56       */
57      @Parameter(property = "revisionKey", defaultValue = "scm.revision")
58      private String revisionKey;
59  
60      /**
61       * The Maven project.
62       */
63      @Parameter(defaultValue = "${project}", required = true, readonly = true)
64      private MavenProject project;
65  
66      @Inject
67      public UpdateSubprojectsMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
68          super(manager, settingsDecrypter);
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void execute() throws MojoExecutionException {
75          super.execute();
76  
77          try {
78              ScmRepository repository = getScmRepository();
79  
80              UpdateScmResult result =
81                      getScmManager().update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion));
82  
83              checkResult(result);
84  
85              if (result instanceof UpdateScmResultWithRevision) {
86                  getLog().info("Storing revision in '" + revisionKey + "' project property.");
87  
88                  if (project.getProperties() != null) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
89                  {
90                      project.getProperties().put(revisionKey, ((UpdateScmResultWithRevision) result).getRevision());
91                  }
92              }
93          } catch (IOException | ScmException e) {
94              throw new MojoExecutionException("Cannot run update command : ", e);
95          }
96      }
97  }