在安装阶段通过 maven-plugin 清除本地 maven 存储库

Clear local maven repository via maven-plugin on install phase

我想在安装阶段之前删除整个存储库 (.m2/repository) 的内容。当然,我不想手工完成,所以我正在寻找一个可以发挥魔力的插件。到目前为止,我遇到了 maven-clean-plugin 并且我正在尝试按如下方式使用它:

<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>

我希望这会在下载新工件之前清除整个存储库,并最终从模块中删除 target 文件夹。删除 target 文件夹有效,但是清除存储库有点不起作用。它确实清除了存储库,但是 Maven 抱怨缺少一些所需的工件,因此编译失败并且 returns 此类错误:

[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]

我觉得我已经非常接近解决方案了。可能我只需要调整插件的参数标签。

谁能给个主意?

如果您清理整个本地存储库,您还会删除 Maven 需要的所有插件,这些插件是在清理运行之前下载的。您应该使用依赖项 plaugin 来仅删除作为您项目的依赖项的罐子:

mvn dependency:purge-local-repository

在 pom 中你可以像这样使用它:

  <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>