Maven – Remove all corrupt jars / dependencies

Learn to rebuild local repository by deleting all corrupt jars from repository and fix missing or corrupt maven dependencies problem.

This solution will help you in situations where you want to force update a maven project or want to download missing maven dependencies.

1. Approach

In maven, you will observe that most dependencies get corrupted because to interrupted downloading of jars. This interruption leaves behind a special file with extension – ".lastUpdated".

In this solution, we have created a windows batch file (please write similar script for mac/linux systems) which does the following:

  1. Find all folders which contain any file with extension ‘.lastUpdated’.
  2. Remove the folder and it’s content – forcefully.
  3. Log the folder location for reference.

2. Batch file to delete maven corrupt jars

Below is the batch file which performs all steps listed in above section.

:: Usage 
:: C:/M2> delete-corrupt-dependencies.bat > files.txt

@echo off
setlocal EnableDelayedExpansion
set last=?

for /f %%I in ('dir /s /b /o:n /a-d "*.lastUpdated"') do (
  if !last! NEQ %%~dpI ( 
    set last=%%~dpI
    echo !last!
    rd /s /q !last!
  )
)
goto end

:end

3. Usage

Place above windows batch file in maven’s M2 folder.

Now open the command prompt and run the batch file. You may capture the output of file (list of deleted corrupt dependencies) in a file.

$ delete-corrupt-dependencies.bat > files.txt

After above batch file completes processing – you may check the list of missing maven dependencies, which just got cleaned up, into files.txt file generated in current directory.

C:\M2\commons-beanutils\commons-beanutils\1.9.3
C:\M2\io\netty\netty-buffer\4.1.39.Final
...
...
...
and so on

Drop me your question related to maven corrupt local repository problem and it’s solution provided above.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

4 thoughts on “Maven – Remove all corrupt jars / dependencies”

  1. This is BS. All the artifact folders have a “.lastUpdated”-file – not just the corrupted ones. Effectively, this advice is the same as deleting the whole .m2-directory.

    Reply
    • These files indicate to Maven that it attempted to obtain the archive by download, but was unsuccessful. In order to save bandwidth, it will not attempt this again until a certain time period encoded in the file has elapsed. Read More

      Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.