简体   繁体   English

使用gradle管理具有其所有依赖项的groovy脚本,然后通过命令行将其作为独立应用程序运行

[英]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

I have a simple groovy script with a single java library dependency: 我有一个具有单个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)
            }        
        }
    }

I'm using gradle to manage the dependencies of the project. 我正在使用gradle来管理项目的依赖项。 The gradle configuration for the project is pretty straightforward too: 该项目的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'
}  

If I run the script from the Intellij IDE, the script is executed as expected. 如果我从Intellij IDE运行脚本,则脚本将按预期执行。

What I would like to do now is to compile the script with this dependency into one single .jar file, so I can distribute it as such, and run the application from any filesystem path, as the script logic will be dependent on the path from which the execution was called. 我现在想做的就是将具有这种依赖关系的脚本编译成一个.jar文件,这样我就可以将其分发,并从任何文件系统路径运行应用程序,因为脚本逻辑将取决于执行被调用。

I've tried with a few gradle fat jars examples out there but none have worked for me since the .jar file is constantly throwing Could not find or load main class Loading when I try it to run. 我尝试了一些gradle fat jars示例,但是没有一个对我有用,因为.jar文件不断抛出尝试运行时Could not find or load main class Loading

If anyone would be so kind to give a hint or to show an example of a gradle task that would do a build that fits my described needs I would be very gratefull. 如果有人愿意提供提示或显示gradle任务的示例,并且该示例可以完成满足我所描述的需求的构建,那么我将非常感激。

I'm aware of the groovy module Grape with the @Grab annotation too, but I would leave that as a last resort since I don't want the users to wait for the dependencies download, and would like to bundle them with the app. 我也知道带有@Grab注释的groovy模块Grape,但由于我不想让用户等待依赖项下载,并希望将它们与应用程序捆绑在一起,因此我将其作为最后的手段。

I'm using groovy 2.5.6 and gradle 4.10 for the project 我在项目中使用groovy 2.5.6和gradle 4.10

Thanks 谢谢

You can simply create the fat-jar yourself, without any extra plugin, using the jar Task. 您可以使用jar任务轻松地自己创建fat-jar ,而无需任何额外的插件。 For a simple/small project like yours, it should be straightforward : 对于像您这样的简单/小型项目,它应该很简单:

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) 
        }
    }
}

EDIT : pleas take other comments into consideration: if you have more that one external lib you might have issues with this solution, so you should go for a solution with "shadow" plugins in this case. 编辑:请考虑其他注释:如果您有多个外部库,则此解决方案可能会出现问题,因此在这种情况下,您应该使用“影子”插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM