Maven generates compile time and version Java classes

This article uses the Maven plug-in to automatically generate a Version.javaclass. You can use the corresponding constants in the Java code to obtain the current version number and build time.

The content of the file automatically generated by Maven after compilation Version.javais as follows:

package com.shanhy.demo;

public final class Version {
    
    
    public static String NUMBER = "0.0.41-SNAPSHOT";
    public static String BUILD_TIME = "2023-08-15 10:54:16";
}

An example usage of the plugin in pom.xml is as follows:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>build-helper-maven-plugin</artifactId>
	<version>3.4.0</version>
	<executions>
		<execution>
			<id>timestamp-property</id>
			<goals>
				<goal>timestamp-property</goal>
			</goals>
                     <configuration>
                         <name>current.time</name>
                         <pattern>yyyy-MM-dd HH:mm:ss</pattern>
                         <timeZone>GMT+8</timeZone>
                         <locale>zh_CN</locale>
                     </configuration>
		</execution>
	</executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>run</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <property name="src.dir" value="${project.build.sourceDirectory}"/>
                    <property name="package.dir" value="com/shanhy/demo"/>
                    <property name="package.name" value="com.shanhy.demo"/>
                    <!--maven.build.timestamp是UTC时间,跟北京时间有8个小时的时差,
                        使用插件 build-helper-maven-plugin:timestamp-property 解决这个时差问题-->
                    <!--<property name="buildtime" value="${maven.build.timestamp}"/>-->
                    <property name="buildtime" value="${current.time}"/>

                    <!--生成一个 Version.java 文件,里面生成常量,可以在Java代码中直接使用-->
                    <echo file="${src.dir}/${package.dir}/Version.java"
                          message="package ${package.name};${line.separator}${line.separator}"/>
                    <echo file="${src.dir}/${package.dir}/Version.java" append="true"
                          message="public final class Version {${line.separator}"/>
                    <echo file="${src.dir}/${package.dir}/Version.java" append="true"
                          message="    public static String NUMBER = &quot;${project.version}&quot;;${line.separator}"/>
                    <echo file="${src.dir}/${package.dir}/Version.java" append="true"
                          message="    public static String BUILD_TIME = &quot;${buildtime}&quot;;${line.separator}"/>
                    <echo file="${src.dir}/${package.dir}/Version.java" append="true"
                          message="}${line.separator}"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>

You can also use this method to generate common version configuration files, such as version.properties. The java file generated above is to directly use constants in related businesses in Java code.


(END)

Guess you like

Origin blog.csdn.net/catoop/article/details/132295069