How to clean old dependencies from maven repositories?

前端 未结 9 1765
小鲜肉
小鲜肉 2020-12-14 05:19

I have too many files in .m2 folder where maven stores downloaded dependencies. Is there a way to clean all old dependencies? For example, if there is a dependency with 3 di

9条回答
  •  醉梦人生
    2020-12-14 05:46

    1. Download all actual dependencies of your projects

      find your-projects-dir -name pom.xml -exec mvn -f '{}' dependency:resolve
      
    2. Move your local maven repository to temporary location

      mv ~/.m2 ~/saved-m2
      
    3. Rename all files maven-metadata-central.xml* from saved repository into maven-metadata.xml*

      find . -type f -name "maven-metadata-central.xml*" -exec rename -v -- 's/-central//' '{}' \;
      
    4. To setup the modified copy of the local repository as a mirror, create the directory ~/.m2 and the file ~/.m2/settings.xml with the following content (replacing user with your username):

      
       
        
         mycentral
         My Central
         file:/home/user/saved-m2/
         central
        
       
      
      
    5. Resolve your projects dependencies again:

      find your-projects-dir -name pom.xml -exec mvn -f '{}' dependency:resolve
      
    6. Now you have local maven repository with minimal of necessary artifacts. Remove local mirror from config file and from file system.

提交回复
热议问题