Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change "bootJar" target directory

Tags:

java

gradle

New to gradle, facing issues in jenkins pipeline. Entire build is successful but not able to find the bootable jar of my spring project. If you can please let me know how can I move the jar from target/lib/ to /target folder.

Below is my build.gradle

import io.swagger.codegen.DefaultGenerator
import io.swagger.codegen.config.CodegenConfigurator

buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
        jacocoWorkspaceDirectory = "/jacoco/"
    }
    repositories {
        mavenLocal()
        maven { url "http://repo.maven.apache.org/maven2" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
        classpath group: "io.spring.gradle", name: "dependency-management-plugin", version: "1.0.6.RELEASE"
        classpath 'com.abc.sonar:abc-sonar-gradle-plugin:2.7.1.RELEASE'
        classpath group: "org.unbroken-dome.gradle-plugins", name: "gradle-testsets-plugin", version: "1.4.2"
        classpath("io.swagger:swagger-codegen:2.3.1")
        classpath("io.swagger.core.v3:swagger-gradle-plugin:2.0.5")
    }
}

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: "io.spring.dependency-management"
apply plugin: "org.springframework.boot"
apply plugin: "io.swagger.core.v3.swagger-gradle-plugin"
apply plugin: 'abc-sonar'
apply plugin: "jacoco"
apply plugin: "findbugs"
apply plugin: "checkstyle"
apply plugin: "pmd"


def excludeStudentManagementConstants = "src/main/java/com/abc/student/management/constants/studentManagementConstants.java"
def excludeCaasConstants = "src/main/java/com/abc/student/management/pcf/constants/CaasConstants.java"

sourceCompatibility = 1.8
targetCompatibility = 1.8

project.buildDir = 'target'

group = 'com.abc.studentmanagement'
version = '1.0.0-SNAPSHOT'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

repositories {
    mavenLocal()
    maven { url "http://repo.maven.apache.org/maven2" }
}


// PLUGIN CONFIGs
springBoot {
    mainClassName = 'com.abc.student.management.StudentManagementApplication'
    buildInfo()
}

bootJar {
    baseName = 'student-management'
}

// FIXME - IN FUTURE WE NEED TO MAKE THIS to Zero.
checkstyle {
    maxErrors = 11
    maxWarnings = 11
    toolVersion = 8.17
}

[checkstyleMain, checkstyleTest].each { task ->
    task.logging.setLevel(LogLevel.LIFECYCLE)
    // FIXME -  This is to be updated with False.
    task.ignoreFailures = false
}
// ---- PLUGIN CONFIG ENDs


dependencyManagement {
    imports {
        mavenBom "org.springframework.boot:spring-boot-dependencies:$springBootVersion"
    }
}

dependencies {
    // Spring specific dependencies
    implementation('org.springframework:spring-tx')
    implementation('org.springframework:spring-core')
    implementation('org.springframework:spring-context')
    implementation('org.springframework:spring-beans')
    implementation('org.springframework:spring-expression')
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-aop'
    implementation group: 'org.springframework.retry', name: 'spring-retry'

    // Spring data-mongo - Remove mongodb eventually in favor of Oracle.
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-mongodb'

    // Spring cloud
    // Upgraded springcloud-starter-vault from 2.0.0.RC1 to 2.0.1.RELEASE.
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-vault-config', version: '2.0.1.RELEASE'
    implementation group: 'io.pivotal.spring.cloud', name: 'spring-cloud-services-starter-config-client', version: '2.0.1.RELEASE'
    // Upgraded "spring-cloud-starter-bus-amqp" following from 2.0.0.RC1 to 2.0.1.RELEASE
    implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '2.0.1.RELEASE'


    // MongoDB - Dependencies - Remove mongodb eventually in favor of Oracle.
    implementation group: 'org.mongodb', name: 'mongodb-driver', version: '3.10.1'
    implementation group: 'org.mongodb', name: 'mongodb-driver-core', version: '3.10.1'
    implementation group: 'org.mongodb', name: 'bson', version: '3.10.1'

    // SWagger dependencies
    implementation group: 'io.swagger', name: 'swagger-parser', version: '1.0.23'
    implementation group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    implementation group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
    implementation group: 'com.atlassian.oai', name: 'swagger-request-validator-core', version: '1.3.9'

    // Apache - Commons
    implementation group: 'commons-io', name: 'commons-io', version: '2.6'

    // Oracle dependencies
//    implementation group: 'com.oracle.jdbc', name: 'ojdbc7', version: '12.1.0'

    // Logback
    implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
    implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'

    implementation group: 'org.apache.camel', name: 'camel-jsonpath', version: '2.22.0'
    implementation group: 'com.google.gdata', name: 'core', version: '1.47.1'
    implementation group: 'com.google.guava', name: 'guava', version: '20.0'

    // Mapper
    implementation group: 'ma.glasnost.orika', name: 'orika-core', version: '1.5.4'

    // JSON
    implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.8.10'
    implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'


    // abc - exception handler
    implementation(group: 'com.abc.consumer.exception', name: 'exception-handler', version: '1.3.3-SNAPSHOT') {
        exclude group: 'org.springframework'
    }

    // AXON - SDK
    implementation(group: 'com.abc.apie.axon', name: 'axon-sdk', version: '2.0.5') {
        exclude(module: 'hibernate-validator')
    }

    // abc Reference Arch - Messaging
    implementation(group: 'com.abc.refarch', name: 'ref-arch-messaging-base', version: '5.3.2') {
        exclude module: 'axon-sdk'
        exclude module: 'hibernate-validator'
        exclude group: 'org.springframework'
    }


    // Test specific.
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
    testImplementation group: 'com.github.tomakehurst', name: 'wiremock', version: '2.21.0'

    // abc Reference Arch - Testing
    testImplementation(group: 'com.abc.refarch', name: 'ref-arch-unit-test-jar', version: '5.3.2') {
        exclude group: 'org.springframework'
    }
    testImplementation(group: 'com.intuit.karate', name: 'karate-apache', version: '0.9.4') {
        exclude(module: 'logback-classic')
    }
    testImplementation group: 'net.masterthought', name: 'cucumber-reporting', version: '3.20.0'

    // Karate Framework
    testImplementation group: 'com.intuit.karate', name: 'karate-junit4', version: '0.9.4'

    // The version of lombok was actually - 1.16.20 - but had problem with Gradle version 4.9 hence upgraded to 1.18.10.
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.10'
}

ext.appName = 'student-management'
ext.apiPackage = 'com.abc.student.management.generated.api'
ext.modelPackage = 'com.abc.student.management.generated.model'
ext.swaggerFile = "${rootDir}/src/main/resources/StudentManagement.v1.yaml"
ext.swaggerBuildDir = "${project.buildDir}/${ext.appName}"

// Actual task for generating the server
task generateServer {
    doLast {
        def config = new CodegenConfigurator()
        config.setLang("spring")
        config.setApiPackage(rootProject.apiPackage)            // Package to be used for the API interfaces
        config.setModelPackage(rootProject.modelPackage)        // Package to be used for the API models
        config.setInputSpec(rootProject.swaggerFile.toString()) // The swagger API file
        config.setOutputDir(rootProject.swaggerBuildDir)
        // The output directory, user-service-contract/build/user-service-server/
        config.addSystemProperty("models", "");
        config.addSystemProperty("apis", "");

        config.setAdditionalProperties([
                'dateLibrary'    : 'java8', // Date library to use
                'useTags'        : 'true',  // Use tags for the naming
                'delegatePattern': 'true'   // Generating the code with the delegate pattern
        ])
        new DefaultGenerator().opts(config.toClientOptInput()).generate() // Executing the generation
    }
}

findbugs {
    toolVersion = "3.0.1"
    sourceSets = [sourceSets.main]
    // FIXME - Make this false.
    ignoreFailures = true
    effort = "max"
    reportLevel = "medium"
    excludeFilter = file("$rootDir/config/findbugs/exclude.xml")
}

pmd {
    toolVersion = "5.6.1"
    ignoreFailures = false
    sourceSets = [sourceSets.main]
    reportsDir = file("${project.buildDir}/reports/pmdTest")
    ruleSets = [
            'java-basic'
    ]
}

tasks.withType(FindBugs) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

tasks.withType(Pmd) {
    reports {
        xml.enabled = false
        html.enabled = true
    }
}

sonarqube {
    properties {
        property "sonar.projectName", "student-management"
        property "sonar.projectKey", "student-management"
        property "sonar.jacoco.reportPath", "build/jacoco/test.exec"
        property "sonar.junit.reportPaths", "build/test-results/test"
        property "sonar.host.url", "https://fusion.abc.int/sonar"
        property "sonar.gateId", "307"
        property "sonar.projectDescription", "student management microservice."
        property "sonar.skip.qualityGate", "false"
        property "sonar.exclusions", [excludeStudentManagementConstants, excludeCaasConstants]
    }
}

P.S- I tried project.buildDir but that changed the build to target, the jar is still generated inside the target/lib directory.

like image 658
Bilbo Baggins Avatar asked Oct 31 '25 14:10

Bilbo Baggins


1 Answers

In the Configuring executable archive packaging section of the Spring Boot Gradle Plugin Reference Guide we can read:

The BootJar and BootWar tasks are subclasses of Gradle’s Jar and War tasks respectively. As a result, all of the standard configuration options that are available when packaging a jar or war are also available when packaging an executable jar or war.

Having that in mind if we look at Gradle's Jar plugin documentation it has destinationDirectory property denoting the directory where the archive will be placed.

Try updating your build.gradle script according to this:

bootJar {
  destinationDirectory = file(project.buildDir)
}

This should do the trick.

like image 149
Marcin Kłopotek Avatar answered Nov 02 '25 02:11

Marcin Kłopotek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!