Why doesn't Maven's mvn clean ever work the first time?
Asked Answered
S

19

42

Nine times out of ten when I run mvn clean on my projects I experience a build error. I have to execute mvn clean multiple times until the build error goes away. Does anyone else experience this? Is there any way to fix this within Maven? If not, how do you get around it? I wrote a bat file that deletes the target folders and that works well, but it's not practical when you are working on multiple projects. I am using Maven 2.2.1.

[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to delete directory: C:\Documents and Settings\user\My Documents\software-developm
ent\a\b\c\application-domain\target. Reason: Unable to delete directory C:\Documen
ts and Settings\user\My Documents\software-development\a\b\c\application-domai
n\target\classes\com\a\b

[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Fri Oct 23 15:22:48 EDT 2009
[INFO] Final Memory: 11M/254M
[INFO] ------------------------------------------------------------------------
Stationery answered 23/10, 2009 at 19:9 Comment(1)
mvn clean works well, your OS doesn't >:)Assemblyman
E
41

It may be that your IDE or some other process is holding on to the the "target" folder and preventing maven from deleting it.

Emulate answered 23/10, 2009 at 19:18 Comment(5)
I am using Eclipse with Maven integration, but for some reason I am still using the command line to clean and build. I'll try cleans within Eclipse and see if I get the same results.Stationery
Ediz Türkoğlu comment below is one of the major process's on a windows machine that causes this issue. I can confirm it was the case using Netbeans 8.0 on Windows 7. Pausing indexing worked for meSubject
I'm also facing similar problem. Whenever I try to do clean install using Eclipse Maven plugin, it successfully builds the project. But when I try to run the java application through main, I get the following error: java.lang.Error: Unresolved compilation problem: . Now, when I clean install and run the application second time, the application successfully runs. Can anyone please tell me, what I'm doing wrong ?Bichromate
Lookup for any "java.exe"/"javaw.exe" process and kill them. It helped me as well.Horrible
This is true, for example NetBeans doesn't allow me to clean "normally", and I had to stop Weblogic server in order to complete the command. On Windows 10, it really sucks !Fresnel
T
22

This is mostly caused by Windows Indexing. Excluding target folders or .jar extention from indexing will fix the problem. My best practice is excluding .jar extention.

To exclude target files:

  1. Click on windows icon/start menu
  2. Type indexing in search box and click on Indexing Options
  3. Then click on Modify button to investigate directories indexing.
  4. Remove checks of Target folders.

To excluse all .jar files:

  1. Click on windows icon/start menu
  2. Type indexing in search box and click on Indexing Options
  3. Then click on Advanced button.
  4. Go through File Types tab.
  5. In the list, find jar and uncheck it.
Taitaichung answered 16/1, 2013 at 9:14 Comment(2)
The issue resolved once the target is unchecked. But any how .jar file can't be unchecked as the same is disabled for my case.Robinrobina
I confirm this solution worked for me instantly using maven and netbeans 8.0 - I dont see any reason to allow windows to index development .java files either. I wouldnt really recommend using windows search to find content in those files better using an IDESubject
P
13

Turning off Window Search service on Win7 worked for me

Panathenaea answered 11/10, 2010 at 23:47 Comment(0)
H
12

The problem is eclipse is constantly reading the directories and artifacts in your maven project and inevitably has one of them open when you are cleaning.

The best approach is to run maven clean from the eclipse plugin (I use m2eclipse and this seems to work well).

Another approach that sort of works is to run mvn clean with the maven.clean.failOnError flag set to false. If you run it twice usually that is enough to make everything work properly, e.g.

mvn clean -Dmaven.clean.failOnError=false && mvn clean -Dmaven.clean.failOnError=false

You will probably want to turn off eclipse's "Build automatically" from the project menu while you do that.

Haim answered 23/3, 2010 at 4:24 Comment(1)
Did not worked for me. Now its showing list of undeleted file names.Creator
H
6

Following are my findings -

Apart from IDE holding on to the jar, at times you will have some java process which could cause this as well. Lookup for any "java.exe" / "javaw.exe" process in task manager and kill it.

It helped me resolve this at times.

Horrible answered 24/2, 2017 at 17:31 Comment(1)
this reply is underrated. the java.exe programming running its actually the one been holding me back. Thanks. @HorribleAside
S
3

Often you run into this problem when on Windows because Windows doesn't (ordinarily) allow you to delete a file which is in use. Aside from (painfully) going through all your Maven configuration, your best bet is just not to build on Windows (e.g. use a Linux/Solaris/whatever VM).

Sanford answered 23/10, 2009 at 19:13 Comment(0)
B
2

I'm guessing you are opening files in a text editor or leaving a shell open on a directory in target. If some process has a lock on a file or folder Windows won't let you delete it.

If you run a tool like wholockme you'll be able to see what process is locking the file.

Bedding answered 24/10, 2009 at 15:47 Comment(0)
H
2

If you're using IntelliJ, you can pass an additional parameter in the Maven Runner settings:

-Dmaven.clean.failOnError=false

Set it here, in the VM options:

Maven Runner settings

References: http://www.jetbrains.com/idea/webhelp/maven-runner.html http://maven.apache.org/plugins/maven-clean-plugin/faq.html

Haley answered 9/4, 2014 at 20:6 Comment(1)
I believe -D is used to pass system parameters to JVM which is possible from basically all type of IDEs. Not only for IntelliJGarlandgarlanda
B
2

Windows Indexing Service, application server or IDE holding the JAR file is the root cause. Best solution is to disable Windows Search Service or exclude JAR files from indexing.

Another (imperfect) option mvn clean || mvn clean. Double pipe symbol runs second command only if first one fails.

See Releasing Windows file share locks for details on how to find and remove Windows write locks (it advises to use ProcessExplorer or Unlocker).

Blowbyblow answered 8/10, 2014 at 9:20 Comment(2)
This answer is more valuable than the approved one, but proposing to disable Windows Search Service as first option may not satisfy lots of people ;)Posit
Isn't double pipe a bit tricky due to "fails" not always recognized properly (especially when scripts are ran within scripts). Sometimes a fail scenario is treated like a success scenario, if I remember correctly. Haven't done this in a while.Ephemera
P
1

I had the same problem with builds run from Hudson.

Using handle.exe (from Sysinternals) I found out that a java.exe task has an active filehandle to the generated jar file.

If I kill this task the next build succeds. But the next build after this successful build fails again with the same error.

Even if the build task succeeds, it seems not to terminate properly and keeps files open.

I'm a beginner with Hudson and I didn't have this problem at first. Then I played with some plugins and later this problem come up and was reproduceable.

I deactivated almost all plugins (kept only some essential ones like subversion and sonar) and now the problem seems to be solved.

Hope this helps a bit ...

Putrid answered 23/11, 2009 at 23:38 Comment(0)
M
1

Try to disable Windows Indexing Service.

Maines answered 23/8, 2010 at 11:18 Comment(0)
S
1

I had the same problem. I closed eclipse and looked at the running processes in task manager and seen one called 'Java.exe' which is not usually running so I ended it and then run 'mvn clean' again. It should delete the file properly and work correctly.

Sanmicheli answered 19/10, 2012 at 8:46 Comment(0)
D
1

In my case, there is a java process which is executing the surefirebooter.jar which is the plugin to do the maven JUnit test. So you can simply open the task manager and kill the java process.

Duchamp answered 4/12, 2014 at 7:20 Comment(0)
M
0

That's because you block target directory (open a file or enter into (sub)directory of target dir, or any other process/app uses file from this dir). If windows delete command also complains it's not maven fault.

Magnetics answered 23/10, 2009 at 19:30 Comment(0)
B
0

What I have found is that Java development with Maven on Window 7 struggles when the source code is on C drive. Moving my code to another drive works. Even a logical drive will work, the drive doesn't necessarily need to be a different physical hard drive.

Billow answered 17/1, 2012 at 5:35 Comment(0)
J
0

If you don't want to change Windows Indexing settings, you can simply pause it, too.

Jame answered 21/6, 2013 at 18:29 Comment(0)
J
0

you should close all the files in target ,for me i use M2eclipse directly is better you can also try mvn clean -Dmaven.clean.failOnError=false

Jackquelin answered 27/4, 2015 at 8:57 Comment(0)
D
-1

The application which you are running should be stopped. Looks like the app is still running, while you are trying to clean the app.

Downtown answered 27/9, 2013 at 15:21 Comment(0)
N
-5

The solution is restarting windows 7 .It helps to release the resources.

Navigation answered 1/6, 2012 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.