# War plugin - handling of duplicated artifact names

**URL:** <https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726>\
**Category:** Plugin Portal\
**Tags:** plugins\
**Created:** [November 9, 2017, 6:55pm UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726 "2017-11-09T18:55:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jporzelt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jporzelt/32/6593_2.png) [@jporzelt](https://discuss.gradle.org/u/jporzelt)\
**Post date:** [November 9, 2017, 6:55pm UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/1 "2017-11-09T18:55:07Z")

</div>

Hi all,

currently we are migrating a build from Maven to Gradle. So far everything worked out pretty smooth and we are already see a lot of benefits of the migration (faster build times, less build description code, less plugins needed).

Now we are facing the following problem:  
We have a multi module maven build where various modules were building artifacts with the same name.  
E.g:  
**Module A**  
groupId: com.company.moduleA  
artifactId: app-adapter

**Module B**  
groupId: com.company.moduleB  
artifactId: app-adapter

Both producing an artifact named: app-adapter-1.0.0.jar

The maven-war-plugin handled this by prefixing the conflicting jar files with their groupId.  
So we have:

- WEB-INF/lib/com.company.moduleA-app-adapter-.jar
- WEB-INF/lib/com.company.moduleB-app-adapter-.jar

With Gradle this is a different story:  
In the war file we see all those jar files with the same name and when it is extracted only one of them wins on the file system.  
Resulting in missing libraries.  
Tested with Gradle 4.1 and 3.3

Sure we can handle this in our application by renaming the artifactsIds to be unique. But this wouldn’t solve library conflicts that might come through transitive dependencies. Thinking of something like _util-1.0.0.jar_ or _common-1.0.0.jar_

Is there any similar approach in Gradle like in Maven? Google did not help so far…

Any suggestion, opinion, solution is welcome 😉

Cheers,  
Johannes

---

<div class="post-metadata">

**Author:** ![jporzelt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jporzelt/32/6593_2.png) [@jporzelt](https://discuss.gradle.org/u/jporzelt)\
**Post date:** [November 9, 2017, 7:40pm UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/2 "2017-11-09T19:40:48Z")

</div>

we already discussed the approach of modifying the _archivesBaseName_ like:

**archivesBaseName = "${project.group}-${[project.name](http://project.name)}”**

But since we are also publishing those artifacts to our nexus repo this is not a satisfying solution.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [November 10, 2017, 8:00pm UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/3 "2017-11-10T20:00:50Z")

</div>

You could probably do this via `Configuration.getResolvedArtifacts()`

Eg:

```gradle
task copyAndDedup() {
    doLast {
        Map<String, List<ResolvedArtifact>> groupedArtifacts = [:]
        configurations.runtime.resolvedConfiguration.resolvedArtifacts.each { ResolvedArtifact artifact ->
            String aName = artifact.moduleVersion.id.name
            List<ResolvedArtifact> group = groupedArtifacts[name] ?: []
            group << artifact
            groupedArtifacts[name] = group
        }
        File outputDir = file("$buildDir/copyAndDedup")

        // copy all non-jars from the 2runtime" configuration
        copy {
            from configurations.runtime.asFileTree.matching {
                exclude '*.jar'
            }
        }
        groupedArtifacts.values().each { List<ResolvedArtifact> group ->
             if (group.size() == 1) {
                 // don't need to rename this jar
                 copy {
                     from group[0].file
                     into outputDir
                 }
             } else {
                 // two or more artifacts with the same name, rename
                 group.each { ResolvedArtifact artifact ->
                     def moduleVersionId = artifact.moduleVersion.id
                     copy {
                         from artifact.file
                         into outputDir
                         rename ".*", "${moduleVersionId.group}-${moduleVersionId.name}-${moduleVersionId.version}.jar"
                     }
                 }
             }
        }
    }
}

war {
   dependsOn copyAndDedup
   classpath = fileTree("$buildDir/copyAndDedup")
}

```

Warning! Not compiled or tested  
Inspiration taken from [here](https://github.com/uklance/gradle-dependency-export/blob/master/src/main/groovy/com/lazan/dependency/export/MavenDependencyExport.groovy#L52)

---

<div class="post-metadata">

**Author:** ![jporzelt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jporzelt/32/6593_2.png) [@jporzelt](https://discuss.gradle.org/u/jporzelt)\
**Post date:** [November 13, 2017, 10:02am UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/4 "2017-11-13T10:02:41Z")

</div>

Hi Lance,

thanks a lot for your reply. Looks like a reasonable workaround for the problem.  
We also had a long discussion within the company and in the end we agreed on changing our artifactIds.

Anyways I have the opinion if the unique identifier for a maven artifact is the combination of _groupId+artifactId+version_ (I already learned that this was not the case some time ago), the war plugin should be able to properly handle this.

---

<div class="post-metadata">

**Author:** ![Lance](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lance/32/787_2.png) [@Lance](https://discuss.gradle.org/u/Lance)\
**Post date:** [November 13, 2017, 10:22am UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/5 "2017-11-13T10:22:52Z")

</div>

I agree that the default behavior of ignoring one of the jars is not great. It would be nice to have a configurable hook for a custom merge strategy. You could catch this in future by failing, not ideal but arguably better

Eg:

```gradle
war {
   duplicatesStrategy = DuplicatesStrategy.FAIL
}

```

---

<div class="post-metadata">

**Author:** ![jporzelt](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jporzelt/32/6593_2.png) [@jporzelt](https://discuss.gradle.org/u/jporzelt)\
**Post date:** [November 13, 2017, 10:42am UTC](https://discuss.gradle.org/t/war-plugin-handling-of-duplicated-artifact-names/24726/6 "2017-11-13T10:42:53Z")

</div>

Thank you, this I will definitely add.
