# Resolve duplicates when copying dependencies

**URL:** <https://discuss.gradle.org/t/resolve-duplicates-when-copying-dependencies/30193>\
**Category:** Help/Discuss\
**Created:** [January 16, 2019, 4:38pm UTC](https://discuss.gradle.org/t/resolve-duplicates-when-copying-dependencies/30193 "2019-01-16T16:38:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![id-ilych](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/id-ilych/32/7436_2.png) [@id-ilych](https://discuss.gradle.org/u/id-ilych)\
**Post date:** [January 16, 2019, 4:38pm UTC](https://discuss.gradle.org/t/resolve-duplicates-when-copying-dependencies/30193/1 "2019-01-16T16:38:50Z")

</div>

Hello there. I found a lot of ‘How to copy dependencies to a directory?’ topics but they do not cover one critical issue so here we are again (mentioned topics are locked so I cannot ask there).

**TL;DR**  
How should I go about jar files of the same name but different content?

**Full story**  
Consider following `build.gradle` file.

```gradle
apply plugin: 'java'

dependencies {
	compile group: 'android.arch.lifecycle', name: 'common', version: '1.1.1'
	compile group: 'android.arch.lifecycle', name: 'runtime', version: '1.1.1'
}

repositories {
	maven {
		url "https://maven.google.com/"
	}
}

task getDeps(type: Copy) {
	from sourceSets.main.runtimeClasspath
	into 'deps/'
	duplicatesStrategy 'warn'
}

```

When `getDeps` task is executed the ouput is:

```gradle
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:getDeps
Encountered duplicate path "common-1.1.1.jar" during copy operation configured with DuplicatesStrategy.WARN

BUILD SUCCESSFUL

Total time: 8.662 secs

```

Running gradle with `dependencies` argument gives:

```gradle
runtime - Runtime dependencies for source set 'main'.
+--- android.arch.lifecycle:common:1.1.1
| \--- com.android.support:support-annotations:26.1.0
\--- android.arch.lifecycle:runtime:1.1.1
     +--- android.arch.lifecycle:common:1.1.1 (*)
     +--- android.arch.core:common:1.1.1
     | \--- com.android.support:support-annotations:26.1.0
     \--- com.android.support:support-annotations:26.1.0

```

The problem is that two groups (`android.arch.lifecycle` and `android.arch.core`) have artifacts of name `common-1.1.1.jar`. The contents of these files are different of course so I need both of them. How should I go about that? Can group id be included in filename to avoid duplicate names? Or maybe jar files could be merged during copying to have two sets of clasess?

Thank you!

**UPDATE**  
Based on [this answer from StackOverflow](https://stackoverflow.com/a/42203636/986783) I modified my `build.gradle` to prepend artifact names with group id:

```gradle
apply plugin: 'java'

dependencies {
	compile group: 'android.arch.lifecycle', name: 'common', version: '1.1.1'
	compile group: 'android.arch.lifecycle', name: 'runtime', version: '1.1.1'
}

repositories {
	maven {
		url "https://maven.google.com/"
	}
}

task getDeps {
    inputs.files configurations.compile
    outputs.dir "deps"
    doLast {
        mkdir "deps"
        delete fileTree(dir: "deps" , include: "**/*")
        ResolvedConfiguration rc = configurations.compile.resolvedConfiguration
        Set<ResolvedArtifact> artifacts = rc.resolvedArtifacts
        artifacts.each { ra ->
            ModuleVersionIdentifier mvi = ra.moduleVersion.id
            copy {
                from ra.file
                into "deps"
                duplicatesStrategy 'fail'
                rename { old ->
                    return "${mvi.group}-${mvi.name}-${ra.classifier}-${mvi.version}.${ra.extension}"
                }
            }
        }
    }
}

```

Is it reliable solution? Are there other caveats? Do I really need this `ra.classifier` which always seems to be `null`?
