简体   繁体   中英

Gradle sync task inconsistency

I am trying to copy dependencies of a java project to lib folder and then generating value of Class-Path attribute in manifest.mf from the list of jars copied to lib folder.

Here is build.gradle file -

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

libsDirName = 'package'

ext {
    libDir = file('build/dependencies/lib')
}

task syncDependencies(type: Sync) {
    from  configurations.runtime
    into  libDir
}

jar {
    manifest {
        attributes(
            "Class-Path": libDir.listFiles().collect { 'lib/' + it.getName() }.join(' ')
        )
    }
}

jar.dependsOn syncDependencies

dependencies {
    //external compile dependencies
    compile 'org.eclipse.persistence:javax.persistence:2.1.0'
}

When I execute gradle clean jar , it copies dependencies to lib folder but value of Class-Path attribute is blank. If I execute gradle clean jar again, it generates proper value for Class-Path attribute.

However if i execute gradle clean followed by gradle clean jar , value of Class-Path is again empty.

I am using Gradle 2.6 .

The reason why the Class-Path attribute remains empty when first executing gradle clean jar is that you mix up the build lifecycle phases .

libDir.listFiles().collect { 'lib/' + it.getName() }.join(' ') is evaluated when the project is configured whereas the lib directory is filled by the syncDependencies task when the project is executed.

In order to set the Class-Path attribute correctly, in the configuration phase you can do:

jar {
    manifest {
        attributes(
            "Class-Path": configurations.runtime.collect { "lib/$it.name" }.join(' ')
        )
    }
}

Alternatively the following can still be done in the execution phase :

jar {
    doFirst {
        manifest {
            attributes(
                "Class-Path": libDir.listFiles().collect { 'lib/' + it.getName() }.join(' ')
            )
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM