使用 Gradle 创建一个 Groovy 可执行 JAR [英] Create a Groovy executable JAR with Gradle

查看:26
本文介绍了使用 Gradle 创建一个 Groovy 可执行 JAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 gradle 构建脚本.

This is my gradle build script.

apply plugin: 'groovy'

project.group = "test.tree"
archivesBaseName = "tree"
project.version = "1.0"
manifest.mainAttributes("Main-Class" : "test.tree.App")

sourceCompatibility=1.6
targetCompatibility=1.6

repositories {
    mavenCentral()
}

dependencies {
    groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.6'
    testCompile group: 'junit', name: 'junit', version: '4.8.2'
}

这编译得很好

问题是我无法运行创建的 JAR,出现异常java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

The problem is that I can't run the created JAR, I get an exception java.lang.NoClassDefFoundError: groovy/lang/GroovyObject

所以我猜 groovy 插件没有在 JAR 中包含所有必要的类.

So I guess the groovy plugin doesn't include all the necessary classes inside the JAR.

如何创建一个我可以简单地..运行的独立 JAR ;-)

How to I create a stand-alone JAR that I can simply .. run ;-)

推荐答案

您正在寻找的是 应用程序插件,它允许您构建一个独立的 JVM 应用程序,包括所有依赖项和运行脚本.

What you are looking for is the application plugin which allows you build a standalone JVM application including all dependencies and run scripts.

apply plugin:'application'
mainClassName = 'test.tree.App'

这应该会创建您想要的 uberjar:

This should create the uberjar you want:

task uberjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }

    manifest {
        attributes 'Main-Class': 'test.tree.App'
    }
}

这篇关于使用 Gradle 创建一个 Groovy 可执行 JAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆