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

时间:2016-07-28 08:33:57

标签: maven maven-clean-plugin

我想在安装阶段之前删除整个存储库(.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抱怨所需的一些工件丢失,因此编译失败并返回这样的错误:

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

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

有人可以提出想法吗?

1 个答案:

答案 0 :(得分:4)

如果清理整个本地存储库,还会删除maven所需的所有插件,并在clean运行之前下载。您应该使用依赖项plaugin来仅删除项目依赖项的jar:

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>