View Javadoc
1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.scm.ScmException;
27  import org.apache.maven.scm.command.update.UpdateScmResult;
28  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
29  import org.apache.maven.scm.repository.ScmRepository;
30  
31  import java.io.IOException;
32  
33  /**
34   * Update the local working copy with the latest source from the configured scm url.
35   *
36   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
37   */
38  @Mojo( name = "update", aggregator = true )
39  public class UpdateMojo
40      extends AbstractScmMojo
41  {
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      /**
67       * Run Changelog after update.
68       */
69      @Parameter( property = "runChangelog", defaultValue = "false" )
70      private boolean runChangelog = false;
71  
72      /** {@inheritDoc} */
73      public void execute()
74          throws MojoExecutionException
75      {
76          super.execute();
77  
78          try
79          {
80              ScmRepository repository = getScmRepository();
81  
82              UpdateScmResult result = getScmManager().update( repository, getFileSet(),
83                                                               getScmVersion( scmVersionType, scmVersion ),
84                                                               runChangelog );
85  
86              checkResult( result );
87  
88              if ( result instanceof UpdateScmResultWithRevision )
89              {
90                  String revision = ( (UpdateScmResultWithRevision) result ).getRevision();
91  
92                  getLog().info( "Storing revision in '" + revisionKey + "' project property." );
93  
94                  if ( project.getProperties() != null ) // Remove the test when we'll use plugin-test-harness 1.0-alpha-2
95                  {
96                      project.getProperties().put( revisionKey, revision );
97                  }
98  
99                  getLog().info( "Project at revision " + revision );
100             }
101         }
102         catch ( IOException e )
103         {
104             throw new MojoExecutionException( "Cannot run update command : ", e );
105         }
106         catch ( ScmException e )
107         {
108             throw new MojoExecutionException( "Cannot run update command : ", e );
109         }
110     }
111 }