How do I create an executable fat jar with Gradle with implementation dependencies

前端 未结 5 578
广开言路
广开言路 2020-12-01 09:02

I\'ve got a simple project in Gradle 4.6 and would like to make an executable jar of it. I\'ve tried shadow, gradle-fatjar-plugin, gradle-one

相关标签:
5条回答
  • 2020-12-01 09:23

    Based on the accepted answer, I needed to add one more line of code:

    task fatJar(type: Jar) {
      manifest {
        attributes 'Main-Class': 'com.yourpackage.Main'
      }
      archiveClassifier = "all"
      from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
      with jar
    }
    

    Without this additional line, it omitted my source files and only added the dependencies:

    configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    
    0 讨论(0)
  • 2020-12-01 09:25

    The same task can be achieved using Gradle Kotlin DSL in a similar way:

    val jar by tasks.getting(Jar::class) {
        manifest {
            attributes["Main-Class"] = "com.package.YourClass"
        }
    
        from(configurations
            .runtime
            // .get() uncomment this on Gradle 6+
            // .files
            .map { if (it.isDirectory) it else zipTree(it) })
    }
    
    0 讨论(0)
  • 2020-12-01 09:33

    You can use the following code.

    jar {
        manifest {
            attributes(
                    'Main-Class': 'com.package.YourClass'
            )
        }
        from {
            configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
        }
     }
    

    Be sure to replace com.package.YourClass with the fully qualified class name containing static void main( String args[] ).

    This will pack the runtime dependencies. Check the docs if you need more info.

    0 讨论(0)
  • 2020-12-01 09:33

    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

    This line is essential to me.

    0 讨论(0)
  • 2020-12-01 09:42

    Kotlin 1.3.72 & JVM plugin, Gradle 6.5.1

    Syntax is changing quickly in all these platforms

    tasks {
        compileKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }
        compileTestKotlin {
            kotlinOptions.jvmTarget = "1.8"
        }
        val main = sourceSets.main.get()
        //TODO
        register<Jar>("buildFatJar") {
            group = "app-backend"
            dependsOn(build)
            // shouldRunAfter(parent!!.tasks["prepCopyJsBundleToKtor"]) -> This is for incorporating KotlinJS gradle subproject resulting js file.
            manifest {
                attributes["Main-Class"] = "com.app.app.BackendAppKt"
            }
    
            from(configurations.compileClasspath.get().files.map { if (it.isDirectory) it else zipTree(it) })
            with(jar.get() as CopySpec)
            archiveBaseName.set("${project.name}-fat")
        }
    }
    
    0 讨论(0)
提交回复
热议问题