问题描述
桌面项目的build.gradle
文件如下所示...
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)
}
}
...,运行gradlew desktop:dist
后,我得到以下文件夹结构...
[问题]::libs
中有一个独立的.jar(请参见下图),因此不需要其他资源。有没有办法获得没有整个文件夹结构的仅此.jar文件?预先非常感谢:)
解决方法
这些构建目录是创建jar文件所必需的,因此您不能完全没有它们。但是您可以确定构建完成后删除它们。
您可以尝试这样:
// ...
// 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
//...