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