Hi all - New to gradle. I’m sure I’m doing something silly, but I’m trying to add a jar building cap...
m
Hi all - New to gradle. I’m sure I’m doing something silly, but I’m trying to add a jar building capability to my gradle file. But I’m getting an error:
Expression 'jar' cannot be invoked as a function. The function 'invoke()' is not found
. I already added the
java
plugin in the
plugins
function. Would any one be able to provide some guidance? Thanks!
Copy code
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.6.21"
    java
    application
}

group = "me.talha"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile>() {
    kotlinOptions.jvmTarget = "11"
}

application {
    mainClass.set("MainKt")
}

jar {
    manifest {
        attributes 'Main-Class': 'MainKt'
    }
}
v
You are trying to configure a task named "jar", so use
tasks.jar { ... }
. There is no extension registered that is called "jar". Just doing without
tasks.
is syntactic sugar only available in Groovy DSL, not in Kotlin DSL. But actually, I'd recommend you don't set the main class attribute on the jar and worse probably try to build a fat jar. You already applied and configured the "application" plugin, so use it, it builds a proper distribution with all dependencies, your code, additional files you probably configured and generated start scripts with which you can easily start your application. Just use the
distZip
or
distTar
task to generate a distributable archive of the respective type, or `assemble`and thus also
build
to create them both.
m
Thanks, @Vampire - Seems like it might be appropriate at some point for me to take a step back and understand Gradle better. Trial and error only seems to be getting me so far. Your tips helped a lot. I see the generated zip file after using the
distZip
command. However when I run the jar i get a
no main manifest attribute, in copycurrbranchkt-1.0-SNAPSHOT.jar
error. Any idea why that might be? Is there a way in Gradle to also generate a manifest attribute?
Hmm or maybe I’m misunderstanding how to run it. The executable in the bin folder works fine and I’m guessing uses the jar
e
Gradle generates start scripts that are also in the distribution, bin folder as you have found
it sets up the classpath for your jar and all its runtime dependencies
m
Okay that’s awesome - thanks!
Out of curiosity, the generated jar(s) alone should work fine in any projects I want to use it in right?
v
You can even configure where the start scripts are generated. I personally like them to be at the root directory of the distribution
Out of curiosity, the generated jar(s) alone should work fine in any projects I want to use it in right?
Heavily depends on what you mean with that
It is just a jar with your code compiled in it
So yes, you can use it as library too if that is intended
e
the jar does not carry its dependencies with it. if you want to share it between projects, you would best use a Maven repository, which can carry additional metadata, or other tools like Gradle's includeBuild
(but if your jar is meant to be a library, it seems a bit unusual to apply the
application
plugin?)
m
Probably, I actually don’t know much about what’s in here yet 😅
Thanks for the help! I think it’s worth for me to learn some Gradle basics.
v
Always 🙂
1718 Views