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

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

问题描述

  apply插件:'groovy'

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

sourceCompatibility = 1.6
targetCompatibility = 1.6

存储库{
mavenCentral()
}

依赖关系{
groovy组:'org.codehaus.groovy',名称:'groovy',版本:'1.8.6'
testCompile组:'junit',名称:'junit',版本:'4.8.2 '
}

这个编译很好



问题是我无法运行创建的JAR,我得到一个异常
java.lang.NoClassDefFoundError:groovy / lang / GroovyObject



所以我猜groovy插件并没有在JAR中包含所有必需的类。

如何我创建了一个独立的JAR,我可以简单地运行; - )
解决方案

你在找什么是应用程序插件,它允许您构建独立的JVM应用程序,包括所有的依赖项和运行脚本。

  apply插件:'application'
mainClassName ='test.tree.App'

编辑:



这应该创建你想要的uberjar:

 任务uberjar(类型:Jar){
来自文件(sourceSets.main.output.classesDir)
来自configurations.runtime.asFileTree.files.collect {zipTree(it)}

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


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

And this compiles just fine

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

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

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

解决方案

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'

EDIT:

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天全站免登陆