Create a Groovy executable JAR with Gradle

后端 未结 4 826
陌清茗
陌清茗 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: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.

提交回复
热议问题