LibGDX Gradle: 不包含打包资源的导出 .jar

huangapple go评论101阅读模式
英文:

LibGDX Gradle: Export .jar without packed resources

问题

The desktop project's build.gradle file looks like the following...

apply plugin: "java"

sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = [ "../core/assets" ]

project.ext.mainClassName = "com.rin.desktop.ClientLauncher"
project.ext.assetsDir = new File("../core/assets");
project.buildDir = '/Users/lukassongajlo/Dropbox/Elementar/Implementation/Test Versions/Client Version';

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

task dist(type: Jar) {
    manifest {
        attributes 'Main-Class': project.mainClassName
    }
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}

dist.dependsOn classes

eclipse {
    project {
        name = appName + "-desktop"
        linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/core/assets'
    }
}

task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
    doLast {
        def classpath = new XmlParser().parse(file(".classpath"))
        new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
        def writer = new FileWriter(file(".classpath"))
        def printer = new XmlNodePrinter(new PrintWriter(writer))
        printer.setPreserveWhitespace(true)
        printer.print(classpath)
    }
}

... and after running gradlew desktop:dist I get the following folder structure...

LibGDX Gradle: 不包含打包资源的导出 .jar

[Question]: In libs is a standalone .jar (see picture below), so there is no need for the other resources. Is there a way to get only this .jar file without this whole folder structure?

LibGDX Gradle: 不包含打包资源的导出 .jar

英文:

The desktop project's build.gradle file looks like the following...

apply plugin: "java"
sourceCompatibility = 1.8
sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = [ "../core/assets" ]
project.ext.mainClassName = "com.rin.desktop.ClientLauncher"
project.ext.assetsDir = new File("../core/assets");
project.buildDir = '/Users/lukassongajlo/Dropbox/Elementar/Implementation/Test Versions/Client Version'
task run(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
}
task debug(dependsOn: classes, type: JavaExec) {
main = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
standardInput = System.in
workingDir = project.assetsDir
ignoreExitValue = true
debug = true
}
task dist(type: Jar) {
manifest {
attributes 'Main-Class': project.mainClassName
}
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
dist.dependsOn classes
eclipse {
project {
name = appName + "-desktop"
linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/core/assets'
}
}
task afterEclipseImport(description: "Post processing after project generation", group: "IDE") {
doLast {
def classpath = new XmlParser().parse(file(".classpath"))
new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
def writer = new FileWriter(file(".classpath"))
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(classpath)
}
}

... and after running gradlew desktop:dist I get the following folder structure...

LibGDX Gradle: 不包含打包资源的导出 .jar

[Question]: In libs is a standalone .jar (see picture below), so there is no need for the other resources. Is there a way to get only this .jar file without this whole folder structure? Big thanks in advance LibGDX Gradle: 不包含打包资源的导出 .jar

LibGDX Gradle: 不包含打包资源的导出 .jar

答案1

得分: 2

以下是翻译好的内容:

这些构建目录在创建JAR文件时是必需的,因此您不能完全没有它们。但是在构建完成后,您肯定可以删除它们

您可以尝试以下方式:

// ...

// 您的'dist'任务保持不变
task dist(type: Jar) {
    manifest {
        attributes 'Main-Class': project.mainClassName
    }
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}

task deleteUnusedBuildDirs(type: Delete) {
    delete 'build/classes';
    delete 'build/generated';
    delete 'build/resources';
    delete 'build/tmp';
}

dist.finalizedBy deleteUnusedBuildDirs

//...
英文:

These build directories are needed to create the jar file, so you can't go without them completely. But you shure can delete them after the build finished.

You can try like this:

// ...
// your 'dist' task stays untouched
task dist(type: Jar) {
manifest {
attributes 'Main-Class': project.mainClassName
}
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
with jar
}
task deleteUnusedBuildDirs(type: Delete) {
delete 'build/classes';
delete 'build/generated';
delete 'build/resources';
delete 'build/tmp';
}
dist.finalizedBy deleteUnusedBuildDirs
//...

huangapple
  • 本文由 发表于 2020年8月31日 03:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63661223.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定