我正在用Java编写API接口的代码,我想添加一种方法,客户端将使用该方法来检查服务器使用的版本是否相同。
我可以添加类似的内容,final String VERSION = "v1.2.3";
但我想确保该版本始终是最新的。
我想防止可能不会更新字段的事实。
我正在运行时搜索类哈希计算之类的东西(甚至在maven中的编译时可能会生成带有哈希值的资源文件)。
当我使用git时,也许Git可以使用其提交哈希值更新文件中的特殊标记,并且在运行时我为涉及API的每个源返回一个hashCode吗?
我该怎么做?
好的,这就是我的解决方案,它当然不是最有效的方法,但是可以解决问题:
在我的pom.xml中,我添加了以下内容:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-resources</phase>
<configuration>
<target>
<macrodef name="gitlog">
<attribute name="lfile" />
<sequential>
<local name="gitHash"/>
<exec executable="git" outputproperty="gitHash">
<arg value="log" />
<arg value="--format=%H" />
<arg value="-n" />
<arg value="1" />
<arg value="--" />
<arg value="@{lfile}" />
</exec>
<concat append="true" destfile="target/classes/api_hashes.txt">@{lfile}:${gitHash}${line.separator}</concat>
</sequential>
</macrodef>
<loadfile property="api.classes" srcFile="api_classes.txt" />
<delete file="target/classes/api_hashes.txt"/>
<ac:for list="${api.classes}" param="classFile" delimiter="|" xmlns:ac="antlib:net.sf.antcontrib">
<sequential>
<gitlog lfile="@{classFile}" />
</sequential>
</ac:for>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>1.0b3</version>
<exclusions>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</plugin>
我的api_classes.txt文件包含由char |分隔的类文件(源)列表。(无法使用回车),它会生成一个文件,其中包含源列表,char:和earch源的最后一个git哈希。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句