Question

I am building my simple java application which has some JAR dependencies. The build script is given below. It builds fine if I use the command, gradle mainjar. However, If I use gradle build , the build works fine but I get ClassNotFoundException on running , indicating that JAR files were not linked properly.

I understand the lines which I have wrote under mainjar tasks, but how do I cause them to be executed even with normal build task.

apply plugin: 'java'
apply plugin:'application'


repositories {
    mavenCentral()
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }



    }
     mainjar
}

jar {
  manifest { attributes 'Main-Class': 'in.co.madhur.wunderjava.Main' }
}

mainClassName = "in.co.madhur.wunderjava.Main"

task mainjar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from {configurations.compile.collect {zipTree(it)}}

    manifest {
        attributes 'Main-Class': 'in.co.madhur.wunderjava.Main'
    }
}



dependencies {

    compile 'com.fasterxml.jackson.core:jackson-core:2.3.0-rc1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.0-rc1'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0-rc1'
    compile 'com.squareup.retrofit:retrofit:1.5.0'
}

dependencies {

    runtime 'com.fasterxml.jackson.core:jackson-core:2.3.0-rc1'
    runtime 'com.fasterxml.jackson.core:jackson-databind:2.3.0-rc1'
    runtime 'com.fasterxml.jackson.core:jackson-annotations:2.3.0-rc1'
    runtime 'com.squareup.retrofit:retrofit:1.5.0'
}
Was it helpful?

Solution

The application plugin uses the Jar built by the jar task, not the one built by your mainjar task. Hence the line task mainjar(type: Jar) { should be replaced with jar {. Also, it isn't necessary to repeat compile dependencies under runtime, and configurations.compile.collect { zipTree(it) } should be configurations.runtime.collect { zipTree(it) }. (This will start to make a difference once you have runtime dependencies that aren't also compile dependencies.)

OTHER TIPS

try to add

build.dependsOn mainjar

at the end of your file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top