Bukkit中构建后自动打开服务端

作为一个 Mod 开发者,自开发插件以来,一直在想一个问题:为什么插件开发不能一键调试呢?

终于,在一个风雨交加的夜晚,我忍无可忍,于是便有了本文。

注意:由于本人 Gradle 水平堪比学前班,所以不建议将本文内容视为最佳实践
本文仅适用于 IDEA,本人测试版本为 2023.1

添加变量

先在 gradle.properties 中添加两个变量:

  • targetServerPath : 服务端所在的文件夹,比如 D:\Server
  • targetServerJarName : 服务端的名称,比如 xx-server.jar

按照如上设置,本文提供的方法会查找 D:\Server\xx-server.jar 启动。

复制构建产物至目标服务端的插件文件夹

build.gradle 中添加如下内容:

1
2
3
4
5
tasks.register('copyArtifacts', Copy) {
from 'build/libs'
into "${targetServerPath}/plugins/"
include '**/*.jar'
}

生成 runServer 运行配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerException
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult

static void createOption(document, parent, String name, String value) {
final var option = document.createElement("option");

option.setAttribute("name", name);
option.setAttribute("value", value);

parent.appendChild(option)
}

tasks.register('createRunConfigurations') {
def runConfigurationsDir = new File(".idea/runConfigurations")

final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

final var javaDocument = docBuilder.newDocument();
{
final var rootElement = javaDocument.createElement("component");
{
final var configuration = javaDocument.createElement("configuration");
{
configuration.setAttribute("default", "false");
configuration.setAttribute("name", "runServer");
configuration.setAttribute("type", "JarApplication")

createOption(javaDocument, configuration, "JAR_PATH", "${targetServerPath}/${targetServerJarName}")
createOption(javaDocument, configuration, "WORKING_DIRECTORY", targetServerPath)

var method = javaDocument.createElement("method");
{
method.setAttribute("v", "1")

var o1 = javaDocument.createElement("option");

o1.setAttribute("name", "Gradle.BeforeRunTask")
o1.setAttribute("enabled", "true")
o1.setAttribute("tasks", "build")
o1.setAttribute("externalProjectPath", "\$PROJECT_DIR\$")
o1.setAttribute("vmOptions", "")
o1.setAttribute("scriptParameters", "")

method.appendChild(o1)

var o2 = javaDocument.createElement("option");

o2.setAttribute("name", "Gradle.BeforeRunTask")
o2.setAttribute("enabled", "true")
o2.setAttribute("tasks", "copyArtifacts")
o2.setAttribute("externalProjectPath", "\$PROJECT_DIR\$")
o2.setAttribute("vmOptions", "")
o2.setAttribute("scriptParameters", "")

method.appendChild(o2)
}
configuration.appendChild(method)
}
rootElement.appendChild(configuration)
}
javaDocument.appendChild(rootElement);
}

final TransformerFactory transformerFactory = TransformerFactory.newInstance();
final var transformer = transformerFactory.newTransformer();

final DOMSource source = new DOMSource(javaDocument);
final File location = new File(runConfigurationsDir, "runServer.xml");
if (!location.getParentFile().exists())
location.getParentFile().mkdirs();
final StreamResult result = new StreamResult(location);

try {
transformer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}

该任务会生成一个 JarApplication 的配置文件,运行前会执行 buildcopyArtifacts 两个 gradle 任务。


Bukkit中构建后自动打开服务端
http://tt432.github.io/Bukkit中构建后自动打开服务端/
作者
秦千久
发布于
2023年6月27日
许可协议