Not able to include dependancies in JAR file

50 views Asked by At

I am working on one gradle project, I want to create two jar file for same package, one containing all the class and dependencies and other with contains only few classes with log4j dependancy. I have written below code in build.gradle


// Applies BrazilGradle plugin then uses it to setup the build script classpath.

/*
 Applies core Gradle plugins, which are ones built into Gradle itself.
*/
plugins {
    // Java for compile and unit test of Java source files. Read more at:
    // https://docs.gradle.org/current/userguide/java_plugin.html
    id 'java'

    id 'com.github.johnrengelman.shadow' version '7.1.2'

    // JaCoCo for coverage metrics and reports of Java source files. Read more at:
    // https://docs.gradle.org/current/userguide/jacoco_plugin.html
    id 'jacoco'
}

repositories {
    maven {
        name = 'Brazil WIRE read-only repository'
        url = uri(wireReadRepositoryUrl)
        allowInsecureProtocol = true
    }
    mavenCentral()
}

/*
 Resolve build, test, tool, and runtime dependencies using BrazilGradle.
*/
dependencies {
    compileOnly('org.projectlombok:lombok:1.18.26')
    annotationProcessor('org.projectlombok:lombok:1.18.26')
    implementation('com.amazonaws:aws-lambda-java-core:1.2.2')
    implementation('org.apache.logging.log4j:log4j-core:2.20.0')
    implementation('com.fasterxml.jackson.core:jackson-databind:2.15.1')
    implementation("org.apache.flink:flink-connector-kinesis:1.15.4")
    implementation('org.apache.flink:flink-streaming-java_2.12:1.14.4')
    implementation('org.apache.flink:flink-connector-elasticsearch7_2.11:1.14.4')
    implementation('com.amazonaws:aws-java-sdk-lambda:1.12.472')
    implementation('software.amazon.kinesis:amazon-kinesis-connector-flink:2.4.1')
    implementation('com.amazonaws:aws-kinesisanalytics-flink:2.1.0')
    implementation("com.amazonaws:aws-kinesisanalytics-runtime:1.2.0")
    implementation('com.amazonaws:aws-java-sdk-appconfig:1.12.471')
    implementation("software.amazon.awssdk:appconfig:2.20.121")
    implementation("com.amazonaws:aws-java-sdk-dynamodb:1.12.175")


    testImplementation('org.mockito:mockito-core:2.22.0')
    testImplementation('org.mockito:mockito-inline:3.4.0')
    testImplementation('org.junit.jupiter:junit-jupiter:5.7.1')
    testImplementation("org.apache.flink:flink-test-utils_2.12:1.11.1")
    testImplementation("org.powermock:powermock-api-mockito2:2.0.9")
    testImplementation("org.powermock:powermock-module-junit4:2.0.9")
    testImplementation('org.apache.flink:flink-streaming-java_2.12:1.14.4:tests')
    testImplementation('org.apache.flink:flink-runtime_2.12:1.13.6')
    compileOnly('org.projectlombok:lombok:1.18.26')
    annotationProcessor("org.projectlombok:lombok:1.18.22")
}

test {
    finalizedBy jacocoTestReport
    testLogging {
        showStandardStreams = true
    }
    useJUnitPlatform()
}

// Fat jar that contains all dependencies along with code.
shadowJar {
    destinationDir = file("${project.buildDir}/lib")
    classifier = ""
    archiveName = "LiveAnnouncementApp.jar"
    mergeServiceFiles()
    manifest {
        attributes('Main-Class': 'com.amazon.amazonliveannouncementapplication.Processor.AnnouncementProcessor')
    }
    zip64 true
}

task lambdaJar(type: Jar) {
    destinationDir = file("${project.buildDir}/lib")
    classifier = ""
    archiveName = "AnnouncementDecider.jar"

    from sourceSets.main.output.classesDirs
    include "com/amazon/amazonliveannouncementapplication/LambdaFunction/**"
    include "com/amazon/amazonliveannouncementapplication/AppConfig/**"
    include "com/amazon/amazonliveannouncementapplication/POJO/AnnouncementEventResult.class"

    manifest {
        attributes('Main-Class': 'com.amazon.amazonliveannouncementapplication.LambdaFunction.AnnouncementDecider')
    }
}

// Only build the shadow jars
// jar.enabled = false

task copyConfiguration(type: Copy) {
    from "${project.projectDir}/configuration"
    into "${project.buildDir}"
}

// Copy over config and build shadowJar after completing build
tasks.build.finalizedBy(tasks.copyConfiguration, tasks.shadowJar, tasks.lambdaJar)
// Add release task for pipelines packaging (build fleet)
task release {
}
//Register "release" task in build.gradle. The "release" task is required for the package to be built in https://build.amazon.com/
tasks.release.finalizedBy(tasks.build)

As mentioned in above build.gradle, I wants to created AnnouncementDecider.jar with containing only few classes from directories LambdaFunction, AppConfig and AnnouncementEventResult class itself,

along with this, I want dependency org.apache.logging.log4j:log4j-core:2.20.0 to be part of AnnouncementDecider jar, can someone please help me how can I do so?

I am able to create LiveAnnouncementApp fat jar successfully but not this AnnouncementDecider one.

I have gone through multiple solution over stack over flow but nothing help me.

0

There are 0 answers