![](/img/trans.png)
[英]Running a Gradle compiled Java application with dependencies via the java command
[英]Compile a groovy script with all it's dependencies which are managed by gradle and then run it as a standalone application via the command line
我有一个具有单个java库依赖项的简单groovy脚本:
package com.mrhacki.myApp
import me.tongfei.progressbar.ProgressBar
class Loading {
static void main(String[] arguments) {
List list = ["file1", "file2", "file3"]
for (String x : ProgressBar.wrap(list, "TaskName")) {
println(x)
}
}
}
我正在使用gradle来管理项目的依赖项。 该项目的gradle配置也非常简单:
plugins {
id 'groovy'
}
group 'com.mrhacki'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile 'me.tongfei:progressbar:0.7.2'
}
如果我从Intellij IDE运行脚本,则脚本将按预期执行。
我现在想做的就是将具有这种依赖关系的脚本编译成一个.jar文件,这样我就可以将其分发,并从任何文件系统路径运行应用程序,因为脚本逻辑将取决于执行被调用。
我尝试了一些gradle fat jars示例,但是没有一个对我有用,因为.jar文件不断抛出尝试运行时Could not find or load main class Loading
。
如果有人愿意提供提示或显示gradle任务的示例,并且该示例可以完成满足我所描述的需求的构建,那么我将非常感激。
我也知道带有@Grab
注释的groovy模块Grape,但由于我不想让用户等待依赖项下载,并希望将它们与应用程序捆绑在一起,因此我将其作为最后的手段。
我在项目中使用groovy 2.5.6和gradle 4.10
谢谢
您可以使用jar
任务轻松地自己创建fat-jar ,而无需任何额外的插件。 对于像您这样的简单/小型项目,它应该很简单:
jar {
manifest {
// required attribute "Main-Class"
attributes "Main-Class": "com.mrhacki.myApp.Loading"
}
// collect (and unzip) dependencies into the fat jar
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
编辑:请考虑其他注释:如果您有多个外部库,则此解决方案可能会出现问题,因此在这种情况下,您应该使用“影子”插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.