Create a Groovy executable JAR with Gradle

后端 未结 4 793
陌清茗
陌清茗 2020-12-04 15:35

This is my gradle build script.

apply plugin: \'groovy\'

project.group = \"test.tree\"
archivesBaseName = \"tree\"
project.version = \"1.0\"
manifest.mainAt         


        
相关标签:
4条回答
  • 2020-12-04 16:14

    I got the same issue, after adding jar block in build.gradle then it works.

    jar {
      manifest {
        attributes 'Main-Class': 'demo.Demo'
      }
      from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    }
    
    0 讨论(0)
  • 2020-12-04 16:19

    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'
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:23

    the following code is also ok,use spring boot plugin

    buildscript {
        ext {
            springBootVersion = '1.2.6.RELEASE'
        }
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }
    
    apply plugin: 'groovy'
    apply plugin: 'eclipse'
    apply plugin: 'spring-boot'
    
    
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    
    repositories {
        mavenCentral()
    }
    
    
    sourceSets {
        main {
            groovy {
                srcDirs = ['src']
            }
        }
    
        /*test {
            groovy {
                srcDirs = ['test/groovy']
            }
        }*/
    }
    
    dependencies {
        compile "com.alibaba:fastjson:1.1.34"
        compile 'org.codehaus.groovy:groovy-all:2.4.5'
        //testCompile group: 'junit', name: 'junit', version: '4.8.2'
    }
    springBoot {
        backupSource = false
        mainClass = "com.zhb.hello.Hello"
    }
    jar{
        baseName = 'hellogroovy'
        version = '1.1.0'
    }
    
    0 讨论(0)
  • 2020-12-04 16:25

    I would throw in a vote for the shadow gradle plugin. It is capable of building uber jars and is quite versatile and capable of things like class relocation to prevent dependency hell.

    I will not get into comparing the two plugins, but I will go as far as saying that I have gravitated towards using shadow from having used application in the past because of the added features.

    When I get tired of the startup times of @Grab based groovy scripts, I tend to write a gradle build file using the shadow plugin even for single file groovy scripts. An example gradle build file capable of building an uber jar of a groovy script file in the current directory. The main class name needs to correspond to the script file name:

    repositories { 
      jcenter()
      mavenCentral()
    }
    
    defaultTasks = ['shadowJar']
    version      = "1.0"
    
    dependencies {
      compile "org.codehaus.groovy:groovy:2.4.7", 
              "commons-cli:commons-cli:1.2"
    }
    
    sourceSets {
      main {
        groovy {
            srcDirs = [rootDir]
        }
      }
    }
    
    project.tasks.remove jar
    
    shadowJar {
      manifest {
        attributes 'Main-Class': 'MyGroovyScriptName'
      }
    
      classifier = ""
    }
    

    the uber jar will be generated in the build/libs directory.

    0 讨论(0)
提交回复
热议问题