将 GradleProjectConfigures 变更为使用 BuildConfig 工具,添加了 version_delta

- archiveBaseName 更名为 'morny-coeur'
- 现在在 getJarMd5 时遇到文件读写错误不会再输出错误堆栈信息了
- gradle 基本的参数化
This commit is contained in:
A.C.Sukazyo Eyre 2022-11-06 20:07:11 +08:00
parent c2de1b8748
commit e3c273b370
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
8 changed files with 57 additions and 48 deletions

1
.gitignore vendored
View File

@ -11,6 +11,7 @@
/build/ /build/
/bin/ /bin/
.project .project
lcoal.properties
# debug dir # debug dir
/run/ /run/

View File

@ -1,16 +1,33 @@
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
plugins { plugins {
id 'java' id 'java'
id 'java-library' id 'java-library'
id 'maven-publish' id 'maven-publish'
id 'application' id 'application'
id 'com.github.johnrengelman.shadow' version '7.1.0' id 'com.github.johnrengelman.shadow' version '7.1.0'
id 'com.github.gmazzo.buildconfig' version '3.1.0'
} }
group 'cc.sukazyo' final String proj_group = 'cc.sukazyo'
version VERSION final String proj_package = "${proj_group}.cono.morny"
project.ext.archiveBaseName = 'Coeur_Morny_Cono' final String proj_archive_name = MORNY_ARCHIVE_NAME
project.ext.artifactId = 'morny-coeur'
mainClassName = 'cc.sukazyo.cono.morny.ServerMain' final String proj_version_base = VERSION
final String proj_version_delta = VERSION_DELTA
final String proj_version_use_delta = Boolean.parseBoolean(VERSION_DELTA)
final String proj_version = proj_version_base + (proj_version_use_delta ? "-δ${proj_version_delta}" : "")
final String proj_version_codename = CODENAME
final JavaVersion proj_java = JavaVersion.VERSION_17
final Charset proj_file_encoding = StandardCharsets.UTF_8
group proj_group
version proj_version
project.ext.archiveBaseName = proj_archive_name
project.ext.artifactId = proj_archive_name
mainClassName = "${proj_package}.ServerMain"
repositories { repositories {
mavenCentral() mavenCentral()
@ -31,47 +48,44 @@ dependencies {
} }
task updateVersionCode {
ant.replaceregexp(match:'VERSION = ["a-zA-Z0-9.\\-_+@]+;', replace:"VERSION = \"$project.version\";", flags:'g', byline:true) {
fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java')
}
ant.replaceregexp(match:'CODENAME = ["a-zA-Z0-9]+;', replace:"CODENAME = \"${CODENAME}\";", flags:'g', byline:true) {
fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java')
}
ant.replaceregexp(match:'COMPILE_TIMESTAMP = [0-9]+L;', replace:"COMPILE_TIMESTAMP = ${System.currentTimeMillis()}L;", flags:'g', byline:true) {
fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'GradleProjectConfigures.java')
}
}
compileJava.dependsOn updateVersionCode
test { test {
useJUnitPlatform() useJUnitPlatform()
} }
java { java {
sourceCompatibility JavaVersion.VERSION_17 sourceCompatibility proj_java
targetCompatibility JavaVersion.VERSION_17 targetCompatibility proj_java
withSourcesJar() withSourcesJar()
} }
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
options.encoding = "UTF-8" options.encoding = proj_file_encoding.name()
} }
tasks.withType(Javadoc) { tasks.withType(Javadoc) {
options.encoding = 'UTF-8' options.encoding = proj_file_encoding.name()
options.docEncoding = 'UTF-8' options.docEncoding = proj_file_encoding.name()
options.charSet = 'UTF-8' options.charSet = proj_file_encoding.name()
} }
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
} }
buildConfig {
packageName(proj_package)
buildConfigField('String', 'VERSION', "\"${proj_version}\"")
buildConfigField('String', 'VERSION_DELTA', "\"${proj_version_delta}\"")
buildConfigField('String', 'CODENAME', "\"${proj_version_codename}\"")
buildConfigField('long', 'COMPILE_TIMESTAMP', "${System.currentTimeMillis()}L")
}
shadowJar { shadowJar {
archiveBaseName.set("${project.ext.archiveBaseName}") archiveBaseName.set("${project.ext.archiveBaseName}")
archiveVersion.set("${project.version}") archiveVersion.set("${project.version}")

View File

@ -1,6 +1,11 @@
## Core ## Core
VERSION = 1.0.0-alpha1 MORNY_ARCHIVE_NAME = morny-coeur
VERSION = 1.0.0-alpha2
USE_DELTA = true
VERSION_DELTA = 11061900
CODENAME = beiping CODENAME = beiping

View File

@ -1,10 +0,0 @@
package cc.sukazyo.cono.morny;
/**
* the final field that will be updated by gradle automatically.
*/
public class GradleProjectConfigures {
public static final String VERSION = "1.0.0-alpha1";
public static final String CODENAME = "beiping";
public static final long COMPILE_TIMESTAMP = 1667581079623L;
}

View File

@ -16,7 +16,7 @@ public class MornySystem {
* 程序的语义化版本号<br> * 程序的语义化版本号<br>
* 会由 gradle 任务 {@code updateVersionCode} 更新 * 会由 gradle 任务 {@code updateVersionCode} 更新
*/ */
public static final String VERSION = GradleProjectConfigures.VERSION; public static final String VERSION = BuildConfig.VERSION;
/** /**
* Morny Coeur 当前的版本代号.<br> * Morny Coeur 当前的版本代号.<br>
@ -26,7 +26,7 @@ public class MornySystem {
* <br> * <br>
* 会由 gradle 任务 {@code updateVersionCode} 更新 * 会由 gradle 任务 {@code updateVersionCode} 更新
*/ */
public static final String CODENAME = GradleProjectConfigures.CODENAME; public static final String CODENAME = BuildConfig.CODENAME;
/** /**
* 获取程序 jar 文件的 md5-hash <br> * 获取程序 jar 文件的 md5-hash <br>
@ -43,7 +43,6 @@ public class MornySystem {
try { try {
return FileUtils.getMD5Three(MornyCoeur.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()); return FileUtils.getMD5Three(MornyCoeur.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
} catch (IOException | URISyntaxException e) { } catch (IOException | URISyntaxException e) {
e.printStackTrace(System.out);
return "<non-jar-runtime>"; return "<non-jar-runtime>";
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(System.out); e.printStackTrace(System.out);

View File

@ -192,8 +192,8 @@ public class ServerMain {
%s [UTC]""", %s [UTC]""",
MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(),
MornySystem.getJarMd5(), MornySystem.getJarMd5(),
GradleProjectConfigures.COMPILE_TIMESTAMP, BuildConfig.COMPILE_TIMESTAMP,
CommonFormat.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0) CommonFormat.formatDate(BuildConfig.COMPILE_TIMESTAMP, 0)
)); ));
return; return;
@ -207,7 +207,7 @@ public class ServerMain {
- version %s (%s)(%d) - version %s (%s)(%d)
- Morny %s""", - Morny %s""",
MornySystem.VERSION, MornySystem.VERSION,
MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP, MornySystem.getJarMd5(), BuildConfig.COMPILE_TIMESTAMP,
MornySystem.CODENAME.toUpperCase() MornySystem.CODENAME.toUpperCase()
)); ));

View File

@ -1,6 +1,6 @@
package cc.sukazyo.cono.morny.bot.command; package cc.sukazyo.cono.morny.bot.command;
import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.BuildConfig;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.MornySystem;
import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.MornyJrrp;
@ -249,8 +249,8 @@ public class MornyCommands {
escapeHtml(MornySystem.CODENAME.toUpperCase()), escapeHtml(MornySystem.CODENAME.toUpperCase()),
escapeHtml(MornySystem.VERSION), escapeHtml(MornySystem.VERSION),
escapeHtml(MornySystem.getJarMd5()), escapeHtml(MornySystem.getJarMd5()),
GradleProjectConfigures.COMPILE_TIMESTAMP, BuildConfig.COMPILE_TIMESTAMP,
escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) escapeHtml(formatDate(BuildConfig.COMPILE_TIMESTAMP, 0))
) )
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
} }
@ -310,8 +310,8 @@ public class MornyCommands {
escapeHtml(MornySystem.VERSION), escapeHtml(MornySystem.VERSION),
escapeHtml(MornySystem.CODENAME), escapeHtml(MornySystem.CODENAME),
escapeHtml(MornySystem.getJarMd5()), escapeHtml(MornySystem.getJarMd5()),
escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), escapeHtml(formatDate(BuildConfig.COMPILE_TIMESTAMP, 0)),
GradleProjectConfigures.COMPILE_TIMESTAMP, BuildConfig.COMPILE_TIMESTAMP,
// continuous // continuous
escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)),
System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp,

View File

@ -9,7 +9,7 @@ public class MornyCLI {
public static void main (String[] args) { public static void main (String[] args) {
Scanner line = new Scanner(System.in); Scanner line = new Scanner(System.in);
System.out.print("$ java -jar morny-coeur-"+GradleProjectConfigures.VERSION+".jar " ); System.out.print("$ java -jar morny-coeur-"+BuildConfig.VERSION+".jar " );
String x = line.nextLine(); String x = line.nextLine();
ServerMain.main(UniversalCommand.format(x)); ServerMain.main(UniversalCommand.format(x));