View Javadoc

1   package org.codehaus.mojo.build;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  
21  import org.apache.maven.plugin.AbstractMojo;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.project.MavenProject;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileStatus;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.log.ScmLogDispatcher;
28  import org.apache.maven.scm.log.ScmLogger;
29  import org.apache.maven.scm.provider.hg.HgUtils;
30  import org.apache.maven.scm.provider.hg.command.HgConsumer;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * Goal which sets project properties for changeSet and changeSetDate from the
35   * current Mercurial repository.
36   * 
37   * @author Tomas Pollak
38   * @goal hgchangeset
39   * @phase initialize
40   * @requiresProject
41   * @since 1.0-beta-4
42   */
43  public class HgChangeSetMojo
44      extends AbstractMojo
45  {
46  
47      private ScmLogDispatcher logger = new ScmLogDispatcher();
48  
49      /**
50       * The maven project.
51       * 
52       * @parameter expression="${project}"
53       * @readonly
54       */
55      private MavenProject project;
56  
57      /**
58       * Local directory to be used to issue SCM actions
59       * 
60       * @parameter expression="${maven.changeSet.scmDirectory}" default-value="${basedir}
61       * @since 1.0
62       */
63      private File scmDirectory;
64  
65      private void checkResult( ScmResult result )
66          throws MojoExecutionException
67      {
68          if ( !result.isSuccess() )
69          {
70              getLog().debug( "Provider message:" );
71              getLog().debug( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
72              getLog().debug( "Command output:" );
73              getLog().debug( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
74              throw new MojoExecutionException( "Command failed."
75                  + StringUtils.defaultString( result.getProviderMessage() ) );
76          }
77      }
78  
79      public void execute()
80          throws MojoExecutionException
81      {
82          try
83          {
84              String previousChangeSet = getChangeSetProperty();
85              String previousChangeSetDate = getChangeSetDateProperty();
86              if ( previousChangeSet == null || previousChangeSetDate == null )
87              {
88                  String changeSet = getChangeSet();
89                  String changeSetDate = getChangeSetDate();
90                  getLog().info( "Setting Mercurial Changeset: " + changeSet );
91                  getLog().info( "Setting Mercurial Changeset Date: " + changeSetDate );
92                  setChangeSetProperty( changeSet );
93                  setChangeSetDateProperty( changeSetDate );
94              }
95          }
96          catch ( ScmException e )
97          {
98              throw new MojoExecutionException( "SCM Exception", e );
99          }
100     }
101 
102     protected String getChangeSet()
103         throws ScmException, MojoExecutionException
104     {
105         HgOutputConsumer consumer = new HgOutputConsumer( logger );
106         ScmResult result = HgUtils.execute( consumer, logger, scmDirectory, new String[] { "id", "-i" } );
107         checkResult( result );
108         return consumer.getOutput();
109     }
110 
111     protected String getChangeSetDate()
112         throws ScmException, MojoExecutionException
113     {
114         HgOutputConsumer consumer = new HgOutputConsumer( logger );
115         ScmResult result =
116             HgUtils.execute( consumer, logger, scmDirectory, new String[] { "log", "-r", ".",
117                 "--template", "\"{date|isodate}\"" } );
118         checkResult( result );
119         return consumer.getOutput();
120     }
121 
122     protected String getChangeSetDateProperty()
123     {
124         return getProperty( "changeSetDate" );
125     }
126 
127     protected String getChangeSetProperty()
128     {
129         return getProperty( "changeSet" );
130     }
131 
132     protected String getProperty( String property )
133     {
134         return project.getProperties().getProperty( property );
135     }
136 
137     private void setChangeSetDateProperty( String changeSetDate )
138     {
139         setProperty( "changeSetDate", changeSetDate );
140     }
141 
142     private void setChangeSetProperty( String changeSet )
143     {
144         setProperty( "changeSet", changeSet );
145     }
146 
147     private void setProperty( String property, String value )
148     {
149         if ( value != null )
150         {
151             project.getProperties().put( property, value );
152         }
153     }
154 
155     private static class HgOutputConsumer
156         extends HgConsumer
157     {
158 
159         private String output;
160 
161         private HgOutputConsumer( ScmLogger logger )
162         {
163             super( logger );
164         }
165 
166         public void doConsume( ScmFileStatus status, String line )
167         {
168             output = line;
169         }
170 
171         private String getOutput()
172         {
173             return output;
174         }
175     }    
176 }