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   * Update the local working copy with the latest source from the configured scm url.
34   *
35   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
36   */
37  @Mojo(name = "update", aggregator = true)
38  public class UpdateMojo extends AbstractScmMojo {
39      /**
40       * The version type (branch/tag/revision) of scmVersion.
41       */
42      @Parameter(property = "scmVersionType")
43      private String scmVersionType;
44  
45      /**
46       * The version (revision number/branch name/tag name).
47       */
48      @Parameter(property = "scmVersion")
49      private String scmVersion;
50  
51      /**
52       * The project property where to store the revision name.
53       */
54      @Parameter(property = "revisionKey", defaultValue = "scm.revision")
55      private String revisionKey;
56  
57      /**
58       * The Maven project.
59       */
60      @Parameter(defaultValue = "${project}", required = true, readonly = true)
61      private MavenProject project;
62  
63      /**
64       * Run Changelog after update.
65       */
66      @Parameter(property = "runChangelog", defaultValue = "false")
67      private boolean runChangelog = false;
68  
69      /** {@inheritDoc} */
70      public void execute() throws MojoExecutionException {
71          super.execute();
72  
73          try {
74              ScmRepository repository = getScmRepository();
75  
76              UpdateScmResult result = getScmManager()
77                      .update(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion), runChangelog);
78  
79              checkResult(result);
80  
81              if (result instanceof UpdateScmResultWithRevision) {
82                  String revision = ((UpdateScmResultWithRevision) result).getRevision();
83  
84                  getLog().info("Storing revision in '" + revisionKey + "' project property.");
85  
86                  if (project.getProperties() != null) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
87                  {
88                      project.getProperties().put(revisionKey, revision);
89                  }
90  
91                  getLog().info("Project at revision " + revision);
92              }
93          } catch (IOException | ScmException e) {
94              throw new MojoExecutionException("Cannot run update command : ", e);
95          }
96      }
97  }