mojohaus / license-maven-plugin

Maven plugin to download and collect license files from project dependencies.
https://www.mojohaus.org/license-maven-plugin/
GNU Lesser General Public License v3.0
106 stars 126 forks source link

FileAlreadyExists exception when organizeLicensesByDependencies is true and a license is cached #312

Open srdo opened 5 years ago

srdo commented 5 years ago

Trying to download licenses with organizeLicensesByDependencies set to true.

[WARNING] Unable to retrieve license from URL 'http://www.apache.org/licenses/LICENSE-2.0.txt' for dependency 'org.apache.commons:commons-lang3': /mnt/c/Users/Esran/Documents/NetbeansProjects/storm/target/generated-resources/licenses/org.apache.commons.commons-lang3_the_apache_software_license,_version_2.0
[DEBUG] 
java.nio.file.FileAlreadyExistsException: /mnt/c/Users/Esran/Documents/NetbeansProjects/storm/target/generated-resources/licenses/org.apache.commons.commons-lang3_the_apache_software_license,_version_2.0
    at sun.nio.fs.UnixCopyFile.copy (UnixCopyFile.java:551)
    at sun.nio.fs.UnixFileSystemProvider.copy (UnixFileSystemProvider.java:253)
    at java.nio.file.Files.copy (Files.java:1274)
    at org.codehaus.mojo.license.AbstractDownloadLicensesMojo.downloadLicenses (AbstractDownloadLicensesMojo.java:1082)

The broken code seems to be https://github.com/mojohaus/license-maven-plugin/blob/license-maven-plugin-1.19/src/main/java/org/codehaus/mojo/license/AbstractDownloadLicensesMojo.java#L1082

I'm not sure what that code is supposed to do, but if the target file is supposed to be overwritten, the copy call should set CopyOption.REPLACE_EXISTING.

ppalaga commented 5 years ago

Thanks for the report.

Yes, I think adding CopyOption.REPLACE_EXISTING could fix it.

Looks like we have just two integration tests with organizeLicensesByDependencies=true which apparently do not cover this situation. Please add a test in https://github.com/mojohaus/license-maven-plugin/tree/master/src/it that reproduces your scenario and make sure it fails before and passes after applying your fix.

Vlatombe commented 4 years ago

This happens if a dependency with the same ga appears several times in the dependency tree with different versions. Since the current filename mapping doesn't contain the version, the second execution fails.