我们使用Maven进行构建,使用Mercurial进行变更集。尽管我们的软件已经处理了主要版本,但我们真的很想知道Mercurial变更集是用来构建任何运行我们软件的服务器的。
没有人知道Maven中如何在Mercurial中获取工作目录的变更集并将其保存到属性文件或其他内容中的方法,以便当系统管理员针对版本是什么进行“合理性检查”时,我们可以将其显示在应用程序中的某个位置。当前正在运行?
将此合并到您的pom.xml
:
<project>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>hgchangeset</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
然后在.properties
中创建一个src/main/resources
文件,并将属性设置为${changeSet}
。例如:
revision = ${changeSet}
modificationTime = ${changeSetDate}
您可以制作一个更新挂钩,将变更集ID输出到未版本化的.properties文件中:
[hooks]
update = echo changesetid=$HG_PARENT1 > version.properties
此方法的优点是,您可以根据需要轻松地自定义此值,并且该构建独立于版本控制系统(或缺少版本控制系统)。
[如果您想在生成它的Maven构建中放一些东西,您是否看过Buildnumber Maven Plugin(hgchangeset目标)或Maven Mercurial Build Number Plugin?
如果您可以拦截命令输出(进入环境变量,例如),hg id -i
将是简单的方法。可以使用hg log --template "..." tip
您可以使用Maven的antrun插件运行<exec>
或<java>
任务,该任务将生成包含该信息的属性文件。不过,这不是很优雅。
如果需要更多的属性,还可以使用https://github.com/volodya-lombrozo/hg-revision-plugin,而不是org.codehaus.mojo.buildnumber-maven-plugin。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.github.volodya-lombrozo</groupId>
<artifactId>hg-revision-plugin</artifactId>
<version>0.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>
scan
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
然后您可以使用第二个属性:
hg.author=${hg.author}
hg.branch=${hg.branch}
hg.revision=${hg.rev}
hg.node=${hg.node}
hg.tags=${hg.tags}
hg.desc=${hg.desc}
hg.date=${hg.date}