營(yíng)銷(xiāo)型網(wǎng)站建設(shè)定制網(wǎng)絡(luò)營(yíng)銷(xiāo)的四種模式
本文使用Maven插件來(lái)自動(dòng)生成一個(gè) Version.java
類(lèi),可以在Java代碼中使用里面對(duì)應(yīng)的常量,獲取當(dāng)前版本號(hào)和構(gòu)建時(shí)間。
Maven編譯后自動(dòng)生成的 Version.java
文件內(nèi)容如下所示:
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";
}
pom.xml 中插件的使用示例如下所示:
<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時(shí)間,跟北京時(shí)間有8個(gè)小時(shí)的時(shí)差,使用插件 build-helper-maven-plugin:timestamp-property 解決這個(gè)時(shí)差問(wèn)題--><!--<property name="buildtime" value="${maven.build.timestamp}"/>--><property name="buildtime" value="${current.time}"/><!--生成一個(gè) 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 = "${project.version}";${line.separator}"/><echo file="${src.dir}/${package.dir}/Version.java" append="true"message=" public static String BUILD_TIME = "${buildtime}";${line.separator}"/><echo file="${src.dir}/${package.dir}/Version.java" append="true"message="}${line.separator}"/></target></configuration></execution></executions>
</plugin>
你也可用這種方法生成普通的版本配置文件,例如 version.properties,上文生成 java 文件是為了在 Java 代碼中的相關(guān)業(yè)務(wù)中直接使用常量。
(END)