Maven shade access is denied


The error looks like this:

Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (default) on project <project_name> 
Error creating shaded jar: <path>\target\classes (Access is denied)

Stacktrace

Caused by: java.io.FileNotFoundException: <path>\target\classes (Access is denied)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:215)
        at java.util.zip.ZipFile.<init>(ZipFile.java:145)
        at java.util.jar.JarFile.<init>(JarFile.java:153)
        at java.util.jar.JarFile.<init>(JarFile.java:117)
        at org.apache.maven.plugins.shade.DefaultShader.newJarFile(DefaultShader.java:267)
        at org.apache.maven.plugins.shade.DefaultShader.shade(DefaultShader.java:100)
        at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:471)
        ... 21 more

maven-shade-plugin tries to open classes folder as zip file. This happens when one of the dependencies is found on the local filesystem and not in the local maven repository.

Before unpacking, the plugin should check whether target\classes is a directory or a zip file, but it doesn’t. One solution is to clean the local Maven repository from dependencies.

It is located here:

  • Windows 7: C:/Documents and Settings/<username>/.m2/repository
  • Windows 10: C:/Users/<username>/.m2/repository
  • Linux: /home/<username>/.m2/repository
  • Mac: /Users/<username>/.m2/repository

Go to the repository folder and delete all content. After that, you need to re-import the dependencies.