gpt4 book ai didi

java - gradle - 最小化 processResources 中的 json 资源

转载 作者:行者123 更新时间:2023-11-30 06:50:46 29 4
gpt4 key购买 nike

我目前正在做一个项目。该项目的资源包含许多 json 文件,这些文件最终都在最终的 jar 中。我已经在处理资源了。就像重命名文件和替换某些其他文件中的某些字符串一样。所以 processResources 任务已经被使用。

现在的问题是我如何扩展它以使其最小化所有 json 文件。由于 Groovy 本身具有 json 实用程序,因此能够获取文件内容并在目标位置替换它们应该足以让一切正常运行。

这是我当前的 processResources 任务:

processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version

// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include "mcmod.info"

// replace version and mcversion
expand "version": project.version, "mcversion": project.minecraft.version
}

// Minify json resources
from(sourceSets.main.resources.srcDirs) {
include "**/*.json"

// Minify every file here
}

// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude "mcmod.info"
exclude "**/*.json"
}

rename "(.+_at.cfg)", 'META-INF/$1'

from MainDirResources
}

最小化文件应该适用于以下两行中的任何一行:

JsonOutput.toJson(new JsonSlurper().parseText( <file content here> ))
JsonOutput.toJson(new JsonSlurper().parse( <file here> ))

那么我需要做什么才能获取所有文件的内容或文件本身的所有实例并在输出目录中修改它们的内容?

最佳答案

下面的技巧:

processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version

// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include "mcmod.info"

// replace version and mcversion
expand "version": project.version, "mcversion": project.minecraft.version
}

// copy everything else, thats not the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude "mcmod.info"
}

from MainDirResources

rename "(.+_at.cfg)", 'META-INF/$1'

// Minify json resources
doLast {
fileTree(dir: outputs.files.asPath, include: "**/*.json").each {
File file -> file.text = JsonOutput.toJson(new JsonSlurper().parse(file))
}
}
}

由于 outputs.files 仅包含一个目录(文件被复制到的目录),您可以将其转换为文件树并遍历文件并最小化 json 文件。尽管应该作为 processResources 中的最后一个操作运行

关于java - gradle - 最小化 processResources 中的 json 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41028030/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com