mavenmaven-clean-plugin

Clear local maven repository via maven-plugin on install phase


I'd like to delete the content of my entire repository (.m2/repository) before the installation phase. Of course I don't want to do it by hand so I am looking for a plugin which does the magic. So far I came across maven-clean-plugin and I am trying to use it as follows:

<build>
      <sourceDirectory>src/</sourceDirectory>
      <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.2</version>
            <configuration>
               <source>${jdk.version}</source>
               <target>${jdk.version}</target>
            </configuration>
        </plugin>  
        <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
        <filesets>
                  <fileset>
                      <directory>${settings.localRepository}/</directory>
                      <includes>
                          <include>**/*</include>
                      </includes>
                  </fileset>
        </filesets>
        </configuration>
        <executions>
          <execution>
            <id>auto-clean</id>
            <phase>install</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      </plugins>
   </build>

I expect this to wipe out the entire repository before downloading the new artifacts, and finally to remove the target folder from the modules. Removal of target folders works, however the wiping out the repository is kinda not working. It does wipe out the repository, however then maven complains about some artifacts required are missing so the compilation fails and returns such errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.3:resources (default-resources) on project com.google.protobuf: Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.3:resources failed: Plugin org.apache.maven.plugins:maven-resources-plugin:2.3 or one of its dependencies could not be resolved: Could not find artifact org.apache.maven.plugins:maven-resources-plugin:jar:2.3 -> [Help 1]

I feel like I am pretty close to the solution. Probably I just need to tweak the parameter tags of the plugin.

Could anyone give an idea?


Solution

  • If you clean the entire local repository you also delete all plugins which are needed by maven and was downloaded before clean runs. You should use the dependency plaugin for delteing only jars which are dependecies of your Project:

    mvn dependency:purge-local-repository
    

    In pom you can use it like:

      <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-dependency-plugin</artifactId> 
        <version>2.7</version> 
        <executions> 
          <execution> 
            <id>purge-local-dependencies</id> 
            <phase>clean</phase> 
            <goals> 
              <goal>purge-local-repository</goal> 
            </goals> 
            <configuration> 
              <resolutionFuzziness>groupId</resolutionFuzziness> 
              <includes> 
                <include>org.ambraproject</include> 
              </includes> 
            </configuration> 
          </execution> 
        </executions> 
      </plugin>