本篇教程的视频

(待发布)

本篇教程的源代码

Null

本篇教程目标

  • 理解build.gradle文件中各个部分内容
  • 清楚dependencies中修饰词代表的意思

build.gradle

本期教程的话,我们重点来讲讲Gradle的配置文件,也就是build.gradle文件

这个文件是Gradle构建工具的核心,它定义了项目的构建规则和依赖关系

当我们想利用其他的API或者其他模组进行开发时,就得改写这里的配置,添加依赖

后面的教程我们也会用到一些第三方API,比如TerraformGeckolib等等,所以我们今天先来看看Gradle的配置文件

Fabric Wiki对此也做了一些介绍,可以查阅:Fabric Loom

plugins

1
2
3
4
plugins {
id 'fabric-loom' version '1.9-SNAPSHOT'
id 'maven-publish'
}

这个是Gradle项目使用的插件

FabricLoom插件,它提供了许多有用的功能,我们也用它进行Fabric模组的开发

maven-publish插件则用于发布项目到Maven仓库

repositories

1
2
3
4
5
6
7
8
repositories {
// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.

}

这个是项目依赖的仓库,你可以将你要用到的API的maven仓库放在这里

它这里也给你注释了,你也可以翻译翻译看一下

dependencies

这里我直接借用Fabric Wiki上的内容来讲

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
dependencies {
// Set the minecraft version.
minecraft "com.mojang:minecraft:1.18.1"

// Use mappings from maven.
mappings "net.fabricmc:yarn:1.18.1+build.22:v2"

// Use the offical mojang mappings
mappings loom.officialMojangMappings()

// Layered mappings using official mojang mappings and parchment.
mappings loom.layered() {
officialMojangMappings()
// Use parchment mappings. NOTE: Parchment maven must be manually added. (https://maven.parchmentmc.org)
parchment("org.parchmentmc.data:parchment-1.17.1:2021.09.05@zip")
}

// Remap a mod from maven and apply to gradle's implementation configuration
// (Minor detail: it's not exactly applied *to* the configuration, but a clone of it intended for mod dependencies)
modImplementation "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Remap a mod from maven and apply to gradle's api configuration
modApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Remap a mod from maven and apply to gradle's compileOnly configuration
modCompileOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Remap a mod from maven and apply to gradle's compileOnlyApi configuration
modCompileOnlyApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Remap a mod from maven and apply to gradle's runtimeOnly configuration
modRuntimeOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Remap a mod from maven and apply to loom's localRuntime configuration.
// Behaves like runtimeOnly but is not exposed in to dependents. A bit like testRuntimeOnly but for mods.
modLocalRuntime "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

// Include a mod jar in the remapped jar. None transitive.
include "example:example-mod:1.1.1"

// Include a none mod library jar in the remapped jar. A dummy mod will be generated. None transitive.
include "example:example-lib:1.1.1"

// Helper to aid with depending on a specific fabric api version.
modImplementation fabricApi.module("fabric-api-base", "0.46.2+1.18")

// Depend on a loom sub project by using the namedElements configuration.
implementation project(path: ":name", configuration: "namedElements")
}

这个就是项目依赖,我们一个个来看

minecraft

1
minecraft "com.mojang:minecraft:1.18.1"

这个是Minecraft的版本,我们的模组必须依赖Minecraft,所以这里必须写上

mappings

1
2
3
mappings "net.fabricmc:yarn:1.18.1+build.22:v2"

mappings loom.officialMojangMappings()

这个是我们开发使用的映射和版本,yarnFabric的映射,officialMojangMappingsMojang的映射

不过还有一种

1
2
3
4
5
mappings loom.layered() {
officialMojangMappings()
// Use parchment mappings. NOTE: Parchment maven must be manually added. (https://maven.parchmentmc.org)
parchment("org.parchmentmc.data:parchment-1.17.1:2021.09.05@zip")
}

这个是官方映射和Parchment映射的结合,Parchment也就是羊皮纸映射,如果你接触过ForgeNeoForge的开发,可能用过这个映射

不过在本系列教程中,我们还是用Yarn映射

modImplementation

注:这个部分的修饰词结合了AI的解释

1
modImplementation "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

这个是Fabric API的依赖,Fabric API是Fabric模组开发的基础,必须依赖

这是我们常见的修饰词,一般也会用它来添加依赖

modApi

1
modApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

这个相当于modImplementation + api,用于暴露API给其他模组

暴露API的意思呢,简单来说,你开发了一个A模组,这个是一个Lib

然后你开发了一个B模组,这个是常规的模组,B模组需要用到A模组,那么A模组就需要暴露API给B模组

一般Lib类的模组会用到它

modCompileOnly

1
modCompileOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

仅在编译期提供依赖,不包含在运行时,也不会被打包到最终的jar文件中

用户在实际游玩时,还要安装对应的依赖才能正常运行

modCompileOnlyApi

1
modCompileOnlyApi "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

这个相当于modCompileOnly + api,用于暴露API给其他模组,一般也是Lib类模组会用到

modRuntimeOnly

1
modRuntimeOnly "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

仅在运行时提供依赖,不参与编译,也不会被打包到最终的jar文件中

用户在实际游玩时,也还要安装对应的依赖才能正常运行

modCompileOnly和modRuntimeOnly也常常一起出现

modLocalRuntime

1
modLocalRuntime "net.fabricmc.fabric-api:fabric-api:0.46.2+1.18"

仅在本地运行时提供依赖,不参与编译,也不会被打包到最终的jar文件中

一般是你自己在测试依赖时用的,不怎么用

include

1
include "example:example-mod:1.1.1"

这个是包含一个jar文件,当我们打包模组文件时,会把这个jar文件也打包进去

一般比较小的库或者是自己开发的库,可以选择打包进去,这样用户就不用额外下载了

小结

后面还有两个,不过一般不常用,它们是模块化依赖写法,用于引入一个库中特定的一部分,而不是整个库

这些就是gradle的依赖,带api的一般是Lib类模组用的

其他我们用的比较多的是modCompileOnlymodCompileOnlyApimodImplementationmodRuntimeOnly这几个

但是具体的,还是按照相应库的使用说明来写

processResources

1
2
3
4
5
6
7
processResources {
inputs.property "version", project.version

filesMatching("fabric.mod.json") {
expand "version": project.version
}
}

这个是用于处理资源文件

java

1
2
3
4
5
6
7
8
9
java {
// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task
// if it is present.
// If you remove this line, sources will not be generated.
withSourcesJar()

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

这个是Java的配置,这里我们也可以改Java的版本

publishing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// configure the maven publication
publishing {
publications {
create("mavenJava", MavenPublication) {
artifactId = project.archives_base_name
from components.java
}
}

// See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
repositories {
// Add repositories to publish to here.
// Notice: This block does NOT have the same function as the block in the top level.
// The repositories here will be used for publishing your artifact, not for
// retrieving dependencies.
}
}

这个是用于发布Maven仓库的配置,我们暂时用不到,但是我也提一下

就是这里的repositories,上面也有一个repositories

我还见过不少把依赖的Maven地址写在这里的,这就大错特错了,这里的repositories是用于发布Maven仓库的,而不是用于依赖

大家写的时候看看清楚,别写错地方了

总结

最主要的还是repositoriesdependencies,这两个是我们依赖其他模组开发必须了解的东西

dependencies里面的各个修饰词大家也要搞清楚,用什么、怎么用,当然大部分模组也会提示你写什么