21

I occasionally receive this error when I build my project with

>mvn --version
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 15:51:28+0200)
Maven home: ...\apache-maven-3.0.5
Java version: 1.6.0_45, vendor: Sun Microsystems Inc.
Java home: ...
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

and the error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.5:resources 
(default-resources) on project project-name: Cannot create resource output directory: 
 path\to\project\code\project-name\target\classes -> [Help 1]

Note: this happens sometimes, and it is not related to the code. It can happen in one of two consecutive builds - one after the other, against exactly the same source code.

Does anyone have any idea how to avoid it completely? It tends to interrupt quite a time-consuming build :-/

Mateva
  • 786
  • 1
  • 8
  • 27
  • 2
    Does it happen on a CI Server (Jenkins, ...) or on a developers workstation. In which frequency is maven called? – Jens Baitinger Jul 22 '14 at 12:40
  • I remember it happened just once on CI server; it happens more often locally, say one of 4 times, in different modules of the project. – Mateva Jul 22 '14 at 12:44
  • Possible duplicate of [Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.7.1 from/to central (http://repo1.maven.org/maven2)](https://stackoverflow.com/questions/15334394/could-not-transfer-artifact-org-apache-maven-pluginsmaven-surefire-pluginpom2) – Goddard Aug 05 '19 at 15:23

9 Answers9

26

On Windows, there reasons for being unable to create a folder are:

  1. Some other process is deleting this folder at the same time
  2. You don't have permissions to access this folder
  3. The folder is on a network share

Network shares are notoriously unreliable on Windows. Don't use them for any automated tasks. Always build projects with all files residing on a local hard disk.

If you use Maven and Eclipse to build at the same time, you should configure them to use different target folders. See https://stackoverflow.com/a/54366009/34088

Your POM should look like this:

<project>
  ...

  <build>
    <outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
    <testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
  </build>

  <properties>
    <target.dir>target</target.dir>
  </properties>

  <profiles>
    <profile>
      <id>eclipse-folders</id>
      <properties>
        <target.dir>target-eclipse</target.dir>
      </properties>
    </profile>
  </profiles>
  ...  

All that's left is to enable the profile eclipse-folders in the IDE.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Of all the listed, it seems it can be "1. Some other process is deleting this folder at the same time" – Mateva Jul 22 '14 at 12:57
  • 1
    Is there any other process using the same folder (e.g. Eclipse?) – Jens Baitinger Jul 22 '14 at 13:16
  • Could it be Intellij IDEA? and if so, what would be a possible solution to this? – Mateva Jul 22 '14 at 13:21
  • 2
    That might be. I know this problem using Eclipse: Maven cleans out the resources, the Eclipse Compiler thinks "hey, someone is stealing my class files and resources, lets recompile". And then we have the conflict that Maven and the IDE work in the same directory and strange things happen (e.g. Tests fails that usually work, directoies are locked,...) – Jens Baitinger Jul 22 '14 at 13:35
  • @Mateva: See my edits for a solution how to configure Maven project to use separate output folders in Eclipse – Aaron Digulla Jul 22 '14 at 15:32
  • @AaronDigulla Can you point me to a similar configuration for Intellij IDEA? I am trying to find one, but so far - in vain. – Mateva Jul 23 '14 at 08:37
  • I don't have IDEA around. Look in the Maven config in IDEA where you can set the active profile. If you can't find it, post a new question. – Aaron Digulla Jul 23 '14 at 09:10
  • This happens in my C:Drive, no one is using those folders, and I have admin rights. BTW, if I restart the computer, or log-off then on the PC, the build works fine for the first time, then again, when I build the project, I get the same error. Automatic builds are not enabled, Eclipse. – raja777m Mar 30 '16 at 17:30
  • 1
    I did closed the Windows Explorer, Hit refresh on the project, and Project> clean; then build worked fine – raja777m Mar 30 '16 at 17:53
  • @raja777m Note that m2e sometimes fails to run the resource plugin. I don't know why. It just happens. The effect is that all the .class files are there but the resources are missing. Clean doesn't help in this case. Sometimes, "Update Project" unclogs it. – Aaron Digulla Apr 02 '16 at 15:11
  • @AaronDigulla the link is broken. The followed link leads to a page without instructions – neves May 09 '17 at 23:35
  • After struggling for hours/days with the code sometimes running, I moved the files locally and it compiled and ran! – mauriii Oct 12 '18 at 16:16
8

Disable the automatic build of your IDE (Eclipse or IntellJ IDEA or whatever). It will conflict with the Maven build.

Jens Baitinger
  • 2,230
  • 14
  • 34
6

I experience this issue every time I run the command while I have the output folder or the parent folder opened in Windows Explorer.

If I move one level above the parent, the build ends successfully.

Jr.
  • 115
  • 2
  • 10
2

For me the reason for this was only being in said folder with my git bash while building. Ensure that you don't happen to have the said folder open in any other program.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
0

I had the exact same issue when trying to run the First Cup tutorial.

I fixed it by simply closing NetBeans and running it as administrator (via right click).

Liran H
  • 9,143
  • 7
  • 39
  • 52
0

In my case the parent directory inside which maven was trying to create a directory was root, so I did a sudo chown -R : /path/to/directory to change permissions before rerunning the mvn command.

JL_SO
  • 1,742
  • 1
  • 25
  • 38
0

In my case, I opened the jar in 7zip and I opened the target folder. I run my application and received this error. After closing 7zip, the application run fine and build successfully.

samanthamita
  • 83
  • 2
  • 6
0

You can use the following in pom.xml

<build>
    <directory>${project.basedir}/target1</directory>
</build>
-2

My issue was resolved when I gave mvn clean command from windows coammandprompt

Yash
  • 634
  • 1
  • 6
  • 17