75

I use STS(spring tool suite) + maven plugin.

Every time when I run my application using maven-clean I see following error:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hhsystem ui 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ ui ---
[INFO] Deleting C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.471s
[INFO] Finished at: Mon Oct 21 12:34:33 MSK 2013
[INFO] Final Memory: 2M/90M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean) on project ui: Failed to clean project: Failed to delete C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target\org.ow2.util.asm-asm-tree-3.1.jar -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I have to close STS and go to C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target and delete org.ow2.util.asm-asm-tree-3.1.jar

After starting STS again it works, but this is a hassle.

Can you help me with this problem?

UPDATE
for Kalathoki L.

I run maven-clean from this state:

STS screen shot

I see same behaviour from the command line as from the eclipse plugin

If I watch unlocker when STS is running I see

unlocker screen shot:

Captain Man
  • 5,651
  • 3
  • 41
  • 64

23 Answers23

58

Stop your server before you start to clean.

Stopping a server

You can stop the server from the Servers view.

To stop the server:

  1. In the Servers view ( Window > Show View > Other > Server > Servers > OK), select the server that you want to stop.
  2. Click the Stop the server this icon in the toolbar. In the Servers view, the status of the server changes to Stopped.
  3. If for some reason the server fails to stop, you can terminate the process as follows:

    a. Switch to the Debug perspective.

    b. In the Process view, select the server process that you want to stop.

    c. Click the Terminate This is an image of the Terminate icon in the toolbar.

Note: When terminating a server, the server process will end and the server will not go through the normal routine of stopping, for example calling the destroy() method on a servlet.

Source: Eclipse Help

Yubaraj
  • 3,066
  • 7
  • 32
  • 52
36

You may also try try -Dmaven.clean.failOnError=false (from Maven FAQ)

Bartosz Bilicki
  • 10,502
  • 8
  • 63
  • 98
26

Delete the java.exe process in Task Manager and run mvn clean install.It worked for me.

Bibek Shrestha
  • 291
  • 4
  • 9
  • 1
    Is there a way to do without doing this? My server needs some time to go up and I do not want that =( – Rafael Andrade Aug 01 '18 at 11:44
  • This worked for me. But, I'd like to know why it worked. How do you know that java.exe is the only one which has a "lock" on the jar. – MasterJoe Jul 01 '19 at 18:02
  • This worked for me, but isn't there a more "proper" way to stop the server? I started it from command line with mvnw... isn't there a way to stop it as well? – Svend Hansen Jul 28 '19 at 10:55
15

Your problem is that a running process within STS is using files located in your target directory while you execute a mvn clean command. Maven will be unable to delete these files (since other processes are still accessing them) and thus fail with that error.

Try to stop all processes (tests, servers, applications) from within STS before running Maven console commands. Look out: This behaviour might also appear if STS is cleaning up the projects and thus re-compiles the sources, and not running a process.

Scorpio
  • 2,163
  • 1
  • 26
  • 43
9

Probably there are many processes that use sources compiled, could be a server, an old maven instruction or IDE. Sure terminate all processes, stop the server and then execute maven again. If the problem persists, you should close java.exe process.

Regards!

Jhonatan Alarcon
  • 319
  • 4
  • 12
  • 1
    Adding my two cents since It sorts of expands on this answer: It could for example be a folder synchronization process like Google Drive that's hogging your files. This was the problem in my case. Just kill the process and building will work again. – Sargon1 Oct 17 '16 at 10:24
  • This was my solution. I had an unexpected java.exe process running in background which was preventing maven from properly executing. – Michael Fayad Jan 15 '18 at 22:18
8

Close the target folder and its file you have opened before mvn clean

Neha
  • 81
  • 1
  • 1
  • 3
    This worked for me. I had the target folder open in a terminal, which made maven unable to clean the target folder. After `cd`-ing out of the folder, maven was able to clean again. – Idva Jul 19 '17 at 11:47
  • Yep, I had the folder open in windows explorer! – Pavel Mar 29 '18 at 14:49
  • 1
    Had folder open in windows explorer. This helped! – lpkej Oct 16 '19 at 14:55
7

Try disabling Project->Build Automatically.

As Scorpio suggested, there's a process running that's got a lock on a file somewhere.

I have a large multi-module maven project that regularly fails on clean and this resolves it for me. I re-enable Build Automatically when I'm done.

Davetron
  • 96
  • 2
  • 3
5

Summary : Use an external script (batch file) that a) stops the server and b) Unlocks the .jar file before running maven-clean.

Steps:

  1. Bind the maven-antrun-plugin:run goal to the pre-clean phase of mvn clean. See how to do this here

  2. See how to use the antrun plugin to run an external batch file in Windows here : sblundy's answer to "Starting external process". Let's call this file unlock_handles.bat

  3. Use the Sysinternals handle utility download link in the pre_clean.bat to a) stop the Tomcat server and b) unlock the .jar file. handle -c and handle -p would be useful here.

Whew! That's a bit of work, but it will reliably automate the process for you so that you do need to do it manually yourself each time!


OK, here's a rough, proof-of-concept version of unlock_handles.bat for you to try out:

REM "Use handle.exe to figure out the process id and handle ids, parse the output, then close the handle (again using handle.exe)"

cd "C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target"
"c:/Program Files/Process Explorer/handle.exe" -p java.exe "C:\Users\Nikolay_Tkachev\workspace\HHSystem\UI\target" > handles.txt

@echo "O====== Going to unlock all the below file handles! =======O"
FOR /F "tokens=1-26 delims=: " %a in (handles.txt) DO @echo %h
FOR /F "tokens=1-26 delims=: " %a in (handles.txt) DO handle -p %c -c %f -y

Of course, you have to change the path to Sysinternals' handle.exe before giving this a try.

Community
  • 1
  • 1
vijucat
  • 1,979
  • 1
  • 14
  • 16
  • How can I get **pre_clean.bat** ? –  Oct 24 '13 at 06:48
  • It is will helpfull if you write more details about second item –  Oct 24 '13 at 07:36
  • I will try to get back with a rough version for you later today, but basically, we have to write it; it's not something that exists! (Also, unlock_files.bat is a better name for what it does; pre-clean is the context in which it is used). – vijucat Oct 24 '13 at 08:30
  • 1
    I updated the answer with a unlock_handles.bat that you can run. Let me know if it works for you! Edit : parsing strings in DOS batch files is so...hacked together :-) – vijucat Oct 24 '13 at 16:31
  • I don't understand what I need to make –  Oct 29 '13 at 14:55
5

I have same problem and this

mvn clean install -U

command fixed the error.

Amir
  • 594
  • 7
  • 16
4

If all steps (in existing answers) dont work , Just close eclipse and again open eclipse .

3

For linux users: possible solution.

Build error due to "Failed to delete < any-file-or-folder >" will occur if there is by chance of only delete access provided to root user rather to normal-user.

Fix : type ll command to list file that cannot be deleted, if the file is given root access, change to normal user by :

sudo chown -R user-name:user-name filename

Later try for maven clean and build.

ram
  • 1,995
  • 3
  • 23
  • 37
  • thank you, You're save my life. cause privilege on my target folder so I can not to clear via mvn – Huỳnh Tú Jan 06 '20 at 04:00
  • I also had this permission issue in ubuntu. `chmod -R 777 /folder/path/here` fixed my issue. thanks – Ish Mar 30 '21 at 07:31
2

solved by deleting the target folder manually.

Ahmed Emad
  • 107
  • 3
1

I had similar issue. Earlier I was using Maven 3 to build the project. After switching to maven 2 , I had the above error.

Solved it by switching to Maven 3.

Madhu V Rao
  • 867
  • 1
  • 12
  • 23
1

Maven complains if you do not have admin permissions on the target folder.Check if you have admin right to delete that folder.

enter image description here

reshma
  • 371
  • 3
  • 6
1

I solved mine by doing:

  • mvn clean
  • mvn install
  • mvn clean install

For some reasons it worked for me . Good luck !!

Boris_Ndong
  • 211
  • 2
  • 3
  • Thanks. Running with separate commands solved this. I believe this is because the install phase of the prior module had a lock on future maven build modules which then did a clean as their first step before running install. – Jared Nov 14 '18 at 19:23
0

If you lock org.ow2.util.asm-asm-tree-3.1.jar and start eclipse, the logging shows who was unable to lock the file. The same codeline who cant lock the file will not release the lock.

Grim
  • 4,939
  • 8
  • 46
  • 97
0

In pre-clean phase I execute with Maven Unlocker program. This program unlock all files and directory for anyone program.

I execute this with maven-antrun-plugin and only in windows systems

<profile>
  <activation>
    <os>
      <family>windows</family>
    </os>
  </activation>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <phase>pre-clean</phase>
            <configuration>
              <tasks>
                <exec dir="${project.build.directory}" executable="cmd" failonerror="false">
                  <arg value="Unlocker.exe" />
                  <arg value="/S" />
                </exec>
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>
0

Please close all the browser tab. And next time when you try to upload war from another place except target folder.

Atul Jain
  • 653
  • 2
  • 7
  • 18
0

If you run into this error in an IDE like Eclipse or IntelliJ, you may have one or more debug windows open and paused on a break-point. Terminate them.

arcadeblast77
  • 550
  • 2
  • 12
0

The reason for this error is, the file which maven is trying to delete is being used by some process. One example of such process is Eclipse or any other IDE is doing validation on same project.

Please make sure that no validation running from eclipse or in any other IDE where project is open.

0
  1. I closed Git Bash (might be another terminal you are working on). It stops the process
  2. Delete ...0.0.1-SNAPSHOT.jar
  3. mvn clean install (or other start your project command; we have .sh)
0

If you have a process running that using the packaged app deployed like an ftp or ssh session - it could prevent the resource being deleted or moved:

so identify the open port which is using the war or jar, close it - then restart

sftp - ssh port : 22

ssh port : depend on config

user5329403
  • 57
  • 1
  • 6
-1

for me

mvn install
mvn clean install

worked..

Sergey Shcherbakov
  • 3,584
  • 2
  • 30
  • 48