![](/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.