-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ch.32): Close #17: Gradle Cheatsheet #71
base: bleeding
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# CurseMaven | ||
|
||
[CurseMaven](https://github.com/Wyn-Price/CurseMaven) 是个把整个 CurseForge 包装成一个 Maven 仓库的 Gradle 插件。 | ||
有了 CurseMaven 你就能轻松解决“这个 Mod 在 CurseForge 上发布,但是没有提供任何 Maven 仓库”的棘手问题。就像这样: | ||
|
||
```groovy | ||
buildscript { | ||
repositories { | ||
maven { | ||
url = "https://plugins.gradle.org/m2/" | ||
} | ||
maven { | ||
name = "forge" | ||
url = "http://files.minecraftforge.net/maven" | ||
} | ||
} | ||
dependencies { | ||
classpath 'com.wynprice.cursemaven:CurseMaven:2.1.1' // 声明我们会用到 CurseMaven,版本 2.1.1 | ||
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT' | ||
} | ||
} | ||
|
||
apply plugin: 'net.minecraftforge.gradle.forge' | ||
apply plugin: 'com.wynprice.cursemaven' // 加载 CurseMaven 这个插件 | ||
|
||
dependencies { | ||
// 这里我们声明了 JEI 的依赖,并使用 deobfCompile 自动转换 mapping。 | ||
// CurseMaven 最终会去这个地址下载 jar,注意结尾的数字: | ||
// https://www.curseforge.com/minecraft/mc-mods/jei/files/2724420 | ||
deobfCompile "curse.maven:jei:2724420" | ||
|
||
// 另一个例子:Astral Sorcery。我们要使用的是这个版本的 Astral Sorcery: | ||
// https://www.curseforge.com/minecraft/mc-mods/astral-sorcery/files/2920254 | ||
// 那么我们这么声明: | ||
deobfCompile "curse.maven:astral-sorcery:2920254" | ||
} | ||
|
||
// 其余无关内容略去 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Gradle 的依赖管理 | ||
|
||
写 Minecraft Mod 的时候时常会需要跟其他 Mod 打交道,引用其他的 jar 在所难免。 | ||
|
||
## 引入新的依赖项目 | ||
|
||
[和普通的基于 Gradle 的 Java 项目完全一致][ref-1]。就像这样: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 和基于 Gradle 的普通 Java 项目完全一致 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 少点 '的' 看起来会舒服一点( |
||
|
||
```groovy | ||
repositories { | ||
// 我们在这里声明阿里云的 maven 镜像站。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 句号 |
||
maven { | ||
url = "https://maven.aliyun.com/repository/central" | ||
} | ||
} | ||
|
||
dependencies { | ||
// Efficient Java Matrix Library (EJML) 是个专门解决线性代数相关问题的库。 | ||
// 网站:http://ejml.org/ | ||
compile 'org.ejml:all:0.30' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 在?为什么我 idea 里运行没问题,打包成 jar 传服务器启动就会报 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 32 章 -> 构建与发布 -> Shadow/Shade。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还没写,而且建议这里加个 referer 过去( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Already planned. |
||
} | ||
``` | ||
|
||
## 我怎么确定哪个 Maven 镜像有[Mod 名字]? | ||
|
||
很遗憾这个问题非常棘手。 | ||
一般来说,Mod 发布页或者它的源码仓库的 README 会提及 Maven 相关的信息,但奈何不是所有 Modder 都有时间折腾这个。 | ||
|
||
天无绝人之路,你仍然有这么几个选择: | ||
|
||
- [CurseMaven](./curse.md) | ||
- [使用 `mavenLocal()`](./local.md) | ||
|
||
|
||
[ref-1]: https://docs.gradle.org/4.10.2/userguide/declaring_dependencies.html | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 本地 Maven 仓库 | ||
|
||
Maven 不一定非得是在互联网的某个角落里,也可以是在你自己的电脑上。 | ||
比如所谓的 `mavenLocal()` 仓库。 | ||
|
||
```groovy | ||
repositories { | ||
mavenLocal() | ||
} | ||
|
||
dependencies { | ||
deobfCompile 'my_mod:my_lib:0.1.0' | ||
} | ||
``` | ||
|
||
需要搭配 [`publishToMavenLocal` 任务](../publish/local.md)使用。 | ||
|
||
## 这个仓库究竟在我电脑上的什么地方? | ||
|
||
按照 [Gradle 手册][ref-1] 的说法,一般来说是 `${USER_HOME}/.m2/`。也就是说: | ||
|
||
- 对于 Windows,是 `%USERPROFILE%/.m2/`。基本上就是 `C:\Users\[用户名]\.m2`。 | ||
- 对于 Linux、macOS 等深受 Unix 影响的系统,是 `$HOME/.m2/` | ||
- 或者说是 `~/.m2`,如果你用的 Shell 支持展开 `~`。 | ||
|
||
[ref-1]: https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:maven_local |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Gradle 速查指南 | ||
|
||
[Gradle](https://gradle.org) 是个构建工具。 | ||
果你之前用过 [Maven](http://maven.apache.org/),那 Gradle 本质上就是个功能更强的 Maven。 | ||
|
||
Gradle 的存在让我们可以直接一行 `gradle build` 命令就能搞定编译(甚至是发布)而不用头疼别的什么。 | ||
|
||
而这一章附录则收录一些在使用 Gradle 甚至是 ForgeGradle 时会遇到的常见需求解决方案。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 语境下没有 "甚至" 的递进关系,改为 "或是" 或许更恰当。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 或者写成 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmmmmmmmmm |
||
|
||
## 速查指南索引 | ||
|
||
- [依赖管理](./dependencies/index.md) | ||
- [CurseMaven](./dependencies/curse.md) | ||
- [`mavenLocal()`](./dependencies/local.md) | ||
- [自动化](./automation/index.md) | ||
- 读取配置文件 | ||
- 自动生成 `MANIFEST.MF` | ||
- 信息安全 | ||
- Jar 签名 | ||
- [构建与发布](./publish/index.md) | ||
- [强制指定编码方式](./publish/encoding.md) | ||
- Shadow/Shade | ||
- [`publishToMavenLocal` 任务](./publish/local.md) | ||
- CurseGradle | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# 强制指定编码方式 | ||
|
||
出于跨平台的考虑,习惯上 Mod 编译时使用 UTF-8 编码方式对字符进行编码。 | ||
但是 Windows 家族的操作系统的默认编码方式不是 UTF-8。 | ||
这可能会导致构建时因为出现所谓的“非法字符”而导致构建失败。你可以通过在 `build.gradle` 的结尾加上这样一段来强制指定编码方式: | ||
|
||
```groovy | ||
tasks.withType(JavaCompile) { | ||
options.encoding = "UTF-8" | ||
} | ||
``` | ||
|
||
## `JavaCompile`? | ||
|
||
是的,这个选项是在 `JavaCompile` 这一个 task type 下的。 | ||
如果你的 Mod 里还有别的类型的源码,换成对应的 task type 即可。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 发布到本机 Maven 仓库 | ||
|
||
一个 Maven 仓库的本质是有特殊目录格式的目录,或称文件夹,可通过一串 URL 索引。 | ||
|
||
所以你大可以把你电脑上某个目录改装成 Maven 仓库。不过我们甚至连改装都不用——因为 `mavenLocal()` 已经在那了。 | ||
|
||
## `publishToMavenLocal` | ||
|
||
在命令行中执行 | ||
|
||
``` | ||
./gradlew publishToMavenLocal | ||
``` | ||
|
||
然后你的 Mod 就会去本机的 `mavenLocal()` 仓库里了。 | ||
一般来说,这个仓库的真实位置是 `${USER_HOME}/.m2/`,也就是说: | ||
|
||
- 对于 Windows,是 `%USERPROFILE%/.m2/`。基本上就是 `C:\Users\[用户名]\.m2`。 | ||
- 对于 Linux、macOS 等深受 Unix 影响的系统,是 `$HOME/.m2/` | ||
- 或者说是 `~/.m2`,如果你用的 Shell 支持展开 `~`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.