Creating jar along with its dependencies

166 Views Asked by At

Hi I have an application with some dependencies (say A). I need to use this application in another project(say B).

I tried tweaking the jar task as shown below. This is project A's gradle file.

import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems.jar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}
tasks.withType<Jar>{
    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

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

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

This is my project B's gradle file.

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

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

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(files("src/main/resources/ProjectA-1.0-SNAPSHOT.jar"))
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

But doing this way I cannot access any class available in the

ProjectA-1.0-SNAPSHOT.jar

from my project B

I also tried using "com.github.johnrengelman.shadow" plugin. There also could not access the class files inside the uber jar.

Am I missing something here?

1

There are 1 best solutions below

0
vignesh suresh On

Not sure if this is an ide issue when I tried creating an uber jar. The jar also included a kotlin library. For this reason the ide was not able to locate the classes inside the jar. This was my gradle file for creating uber jar

plugins {
    kotlin("jvm") version "1.6.21"
    java
    id("com.github.johnrengelman.shadow") version ("7.1.2")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}



tasks.assemble{
    dependsOn("shadowJar")
}
   
dependencies {
    implementation("org.mybatis.dynamic-sql:mybatis-dynamic-sql:1.4.1")
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

tasks.withType<JavaCompile> {

}

ide not recognizing classes even if the jar was added to class path

but when I modified the gradle file with the following

plugins {
    kotlin("jvm") version "1.6.21"
    java
    id("com.github.johnrengelman.shadow") version ("7.1.2")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}



tasks.assemble{
    dependsOn("shadowJar")
}


tasks.withType<com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar>{

    exclude {
        it.name.startsWith("kotlin")
    }
}



dependencies {
    implementation("org.mybatis.dynamic-sql:mybatis-dynamic-sql:1.4.1")
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "1.8"
}

tasks.withType<JavaCompile> {

}

The ide was able to identify the classes inside the jar.

ide recognizing classes if I remove kotlin library