I need to copy all compile dependencies in one fol...
# community-support
f
I need to copy all compile dependencies in one folder but it should strip version also. For eg; connector-api-1.jar to connector-api.jar doxia-sink-api-7.jar to doxia-sink-api.jar esapiport-04.jar to esapiport.jar guice-4.2.1-no_aop.jar to guice-no_aop.jar hibernate-annotations-3.4.0.GA.jar to hibernate-annotations.jar hibernate-validator-5.2.4.Final.jar to hibernate-validator.jar jaxrpc-impl-1.1.3_01.jar to jaxrpc-impl.jar netty-buffer-4.1.94.Final.jar to netty-buffer.jar mssql-jdbc-7.4.1.jre8.jar to mssql-jdbc.jar common-1.0-SNAPSHOT-wrapper to common-1.0-SNAPSHOT-wrapper How to do that in gradle. In maven it was done simply by using <stripVersion>true</stripVersion> configuration
p
Use a Copy task and transform the names. But why do you need it at all? The jar file does not need to follow any pattern, only pom needs. And locally, the jar files are named after the project name by default, but could be changed.
f
Yes @Philip W, I am working with izpack plugin and current implementation in maven reads the jars from lib folder. And those are required for <installFile>${izpack.staging}/install.xml</installFile> in this xml it is having jar name without version I need to migrate the same in gradle. I tried with copy task and added renaming logic with regex to handle this something like below
Copy code
tasks.register('copyProductDependencies', Copy) {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from configurations.compileClasspath
    into "$buildDir/$izpackStaging/libb"
    exclude 'org/codehaus/izpack/**'
    rename { String fileName ->
        fileName.replaceFirst(/-(\d+\.)*\d+(-SNAPSHOT)?(?=\.(jar|zip)$)/, '$3')
    }
but it is not able to handle all type of jars as there are many type of conversion. I am surprised maven is able to do in on line but in gradle I need to lot of work. is there any alternative way in gradle.
p
That's the way to do it in Gradle, but like I said, there is no convention for the jar name.
a
if the JAR is from a regular Maven repo it should follow the standard layout, so the file name is
${artifactId}-${version}-${classifier}.${extension}
.
you could use ResolvedArtifactResult to get both the coordinates of the dependency and the actual resolved file. You can then rename the file based on the coords. It can be tricky to set use, but there's a good example in the docs https://docs.gradle.org/9.2.1/userguide/artifact_resolution.html#resolving_artifacts
v
I am surprised maven is able to do in on line but in gradle I need to lot of work. is there any alternative way in gradle.
Maybe because it is usually a very bad idea and Gradle tries to help you not shooting your own foot? Artifacts without version in the name are always problematic later on, as you don't know their version, ... 🤷‍♂️ I would probably not try to regex-manipulate stuff, but just do it the way I need it upfront, something like
Copy code
interface FileSystemOperationsProvider {
    @get:Inject
    val fileSystemOperations: FileSystemOperations
}
val foo by tasks.registering {
    val compileClasspath = configurations.compileClasspath
    inputs.files(compileClasspath).withPathSensitivity(PathSensitivity.NAME_ONLY).withPropertyName("compileClasspath")
    val destinationDir = layout.buildDirectory.dir("versionless-libs")
    outputs.dir(destinationDir)
    val fs = objects.newInstance<FileSystemOperationsProvider>().fileSystemOperations
    val artifacts = compileClasspath.map { it.incoming.artifacts }
    doLast {
        fs.sync {
            artifacts.get().forEach { artifact ->
                from(artifact.file) {
                    rename {
                        when (val id = artifact.id.componentIdentifier) {
                            is ModuleComponentIdentifier -> "${id.module}.jar"
                            is ProjectComponentIdentifier -> "${id.projectName}.jar"
                            else -> it
                        }
                    }
                }
            }
            into(destinationDir)
        }
    }
}
👍 1
s
I would instead use Gradle to generate the install.xml content with the "real" file names.
👆 1