1 package org.codehaus.mojo.build;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
35
36
37
38
39
40
41
42
43 public class HgChangeSetMojo
44 extends AbstractMojo
45 {
46
47 private ScmLogDispatcher logger = new ScmLogDispatcher();
48
49
50
51
52
53
54
55 private MavenProject project;
56
57
58
59
60
61
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 }