Home > Software design >  Gradle task to copy dependent jars to a folder along with group name
Gradle task to copy dependent jars to a folder along with group name

Time:10-30

I already know to copy dependent jars into a folder:

task copyJARsToLib(type: Copy) {    
    from configurations.default
    into 'Applicationlib'
} 

After running this task all the dependent jars get copied into Applicationlib folder, the name of the jar is {artifactName-version.jar} for eg. for implementation("org.apache.poi:poi-ooxml:5.2.2") jar name becomes poi-ooxml-5.2.2.jar.

What I want is after copying jar name should also have group name like: group.name_version.jar for eg. org.apache.poi.poi-ooxml-5.2.2.jar

I tried

configurations.default.each {
          
}

But this iterator has File object hence only has jar file name and not group name.

CodePudding user response:

There are certainly other ways to achieve this, but the following will work fine, if your project has only module dependencies (else you might need to adapt a bit)

dependencies {
    implementation("org.apache.poi:poi-ooxml:5.2.2")
}
task copyDependenciesWithGroup {
    doLast {
        configurations.default.resolvedConfiguration.resolvedArtifacts.forEach{ ResolvedArtifact artifact ->
            project.copy {
                from artifact.file
                into 'ApplicationlibWithGroup'
                rename { String fileName ->
                    "${artifact.moduleVersion.id.group}.$fileName"
                }
            }
        }
    }
}

This will copy the folling jars in ApplicationlibWithGroup directory:

$ ls -l ApplicationlibWithGroup/
 com.github.virtuald.curvesapi-1.07.jar
 com.zaxxer.SparseBitSet-1.2.jar
 commons-codec.commons-codec-1.15.jar
 commons-io.commons-io-2.11.0.jar
 org.apache.commons.commons-collections4-4.4.jar
 org.apache.commons.commons-compress-1.21.jar
 org.apache.commons.commons-math3-3.6.1.jar
 org.apache.logging.log4j.log4j-api-2.17.2.jar
 org.apache.poi.poi-5.2.2.jar
 org.apache.poi.poi-ooxml-5.2.2.jar
 org.apache.poi.poi-ooxml-lite-5.2.2.jar
 org.apache.xmlbeans.xmlbeans-5.0.3.jar

  • Related