清除构建机器上的 Maven 本地存储库

发布于 2024-08-02 01:20:59 字数 82 浏览 6 评论 0原文

在 CI 构建服务器上,本地 Maven 存储库会重复填充文件系统(几天后)。 在这种情况下,其他人正在采取什么策略来修剪本地存储库? -最大限度

On a CI build server, the local Maven repository fills up the file system repetitively (after a few days).
What strategy are others doing to trim the local repository in such a case?
-Max

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

离旧人 2024-08-09 01:20:59

Maven 依赖插件有一个 purge-local-repository 目标允许您从本地存储库中删除给定项目的依赖项,如果每天在每个项目上运行一次,则快照将不会累积。


或者,您可以采取更焦土的方法。 由于问题通常是带时间戳的快照工件,因此您可以使用 maven-antrun-plugin< /a> 删除所有与资源收集模式匹配的文件。

例如(请注意,这可能需要一些调整,因为我是凭记忆完成的):

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <configuration>
        <tasks>
          <delete>
            <fileset dir="${settings.localRepository}">
              <include name="**/*.jar"/>
              <exclude name="**/*.pom"/>
              <exclude name="**/*.war"/>
              <exclude name="**/*.ear"/>
              <exclude name="**/*.md5"/>
              <exclude name="**/*.sha"/>
              <!--any other extensions?...-->
              <!--match the timestamp pattern-->
              <containsregexp expression="[0-9]{8}.[0-9]{6}-[0-9]+"/>
            </fileset>
          </delete>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The Maven dependency plugin has a purge-local-repository goal that allows you to delete the dependencies for a given project from the local repository, if this is run say once a day on each project the snapshots will not accumulate.


Alternatively there's a more scorched-earth approach you could take. As the problem is typically the timestamped snapshot artifacts, you could use the maven-antrun-plugin to delete all files that match the resource collection pattern.

For example (note this might need some tweaking as I've done it from memory):

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <configuration>
        <tasks>
          <delete>
            <fileset dir="${settings.localRepository}">
              <include name="**/*.jar"/>
              <exclude name="**/*.pom"/>
              <exclude name="**/*.war"/>
              <exclude name="**/*.ear"/>
              <exclude name="**/*.md5"/>
              <exclude name="**/*.sha"/>
              <!--any other extensions?...-->
              <!--match the timestamp pattern-->
              <containsregexp expression="[0-9]{8}.[0-9]{6}-[0-9]+"/>
            </fileset>
          </delete>
        </tasks>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>
三五鸿雁 2024-08-09 01:20:59

如果您使用的是 hudson,您可以设置一个计划作业,每天删除整个存储库一次或类似的操作。 我有一个名为 hudson-maven-repo-clean 的作业,它具有以下配置:

  • 构建/执行 shell: rm -rf ~hudson/.m2/repository
  • 构建触发器/ 定期构建:0 0 * * *

If you're using hudson, you can set up a scheduled job to just delete the entire repository once a day or something like that. I've got a job called hudson-maven-repo-clean which has this configuration:

  • Build / Execute shell: rm -rf ~hudson/.m2/repository
  • Build Triggers / Build periodically: 0 0 * * *
话少心凉 2024-08-09 01:20:59

除了 purge-local-repository(对我来说,它就像一个核选项,因为它只提供 excludes 配置,而不是显式 includes),看看在删除项目工件mojo 。 我现在正在考虑实现它,因为我的确切用例是清除在我的 CI(有时是工作站)机器上构建的大型 WAR 和 EAR 快照。

In addition to purge-local-repository (which reads to me like a nuclear option, as it only offers an excludes configuration as opposed to an explicit includes), take a look at the Remove Project Artifact mojo. I'm looking to implement it now, as my exact use case is to clear out large WAR and EAR snapshots that are being built on my CI (and sometimes workstation) machines.

云仙小弟 2024-08-09 01:20:59

为此,我们特别使用 build-helper 插件。 在我们公司的父 pom 中,remove-project-artifact 目标嵌入在我们的 hudson 构建的配置文件中。 这样,在安装当前构建版本之前,该工件的所有旧版本都会被删除。

...
<profile>
  <id>hudson</id>
  <activation>
    <property>
      <name>BUILD_TAG</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>remove-old-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>remove-project-artifact</goal>
            </goals>
            <configuration>
              <removeAll>true</removeAll>
            </configuration>
          </execution>
        </executions>
      </plugin>
  ...

使用将removeAll 设置为true 将清除除您正在处理的快照之外的所有其他快照。 这可能很危险,因为它可能意味着分支的快照也将被删除。

例如,如果您有代表 HEAD 的快照 1.0.0.18-SNAPSHOT 和代表分支的快照 1.0.1.17-SNAPSHOT,则使用 1.0.0.18-SNAPSHOT 版本运行此插件将擦除 1.0.1.17-SNAPSHOT 文件夹。

为了解决这种情况,removeAll 应设置为 false。

We use especially for this purpose the build-helper plugin. In our company parent pom is the remove-project-artifact goal embedded in the profile for our hudson builds. This way all old versions of this artifact are removed prior to installing the currently build version.

...
<profile>
  <id>hudson</id>
  <activation>
    <property>
      <name>BUILD_TAG</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>remove-old-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>remove-project-artifact</goal>
            </goals>
            <configuration>
              <removeAll>true</removeAll>
            </configuration>
          </execution>
        </executions>
      </plugin>
  ...

Using removeAll set to true will wipe out all other snapshots except the one your working on. This can be dangerous as it may mean snapshots for a branch will be wiped out as well.

For instance if you have a snapshot 1.0.0.18-SNAPSHOT representing HEAD and snapshot 1.0.1.17-SNAPSHOT representing a branch, running this plugin with 1.0.0.18-SNAPSHOT build will wipe the 1.0.1.17-SNAPSHOt folder.

To get around this scenario the removeAll should be set to false.

好菇凉咱不稀罕他 2024-08-09 01:20:59

我们采用了一种略有不同(且狡猾)的技术。 所有构建“大型事物”(EAR、WAR、TAR)的工件都会像这样覆盖其部署位置:

<properties>
   <discard-me-in-bit-bucket>file://${basedir}/target/_DELETEME</discard-me-in-bit-bucket> 
</properties>

<distributionManagement>
  <repository>
    <id>upload-InternalSite</id>
    <name>SoftwareLibrary External</name>
    <url>${discard-me-in-bit-bucket}</url>
    <layout>legacy</layout>
    <uniqueVersion>false</uniqueVersion>
  </repository>
  <snapshotRepository>
    <id>upload-InternalSite</id>
    <name>Repository Name</name>
    <url>${discard-me-in-bit-bucket}</url>
    <layout>legacy</layout>
    <uniqueVersion>false</uniqueVersion>
  </snapshotRepository>
</distributionManagement>

此策略导致部署目标将事物放入目标目录中,该目录当然会被下一个 CLEAN 操作破坏。 为了变得更加积极,我们有一个构建后步骤来做到这一点:

find -type d -name '*_DELETEME' -exec rm -rf '{}' ';' -prune || echo $?

我们还采用了另一种策略。 在 Hudson/Jenkins 中,我们提供了一个设置文件,用于将 .m2 存储库放置在作业的工作空间中。 这允许我们在作业之前或之后删除整个存储库。 它还使工件在工作区中可见,这有助于调试一些问题。

We have employed a slightly different (and devious) technique. All artifacts that build "large things" (EARs, WARs, TARs) have their deploy location overriden like so:

<properties>
   <discard-me-in-bit-bucket>file://${basedir}/target/_DELETEME</discard-me-in-bit-bucket> 
</properties>

<distributionManagement>
  <repository>
    <id>upload-InternalSite</id>
    <name>SoftwareLibrary External</name>
    <url>${discard-me-in-bit-bucket}</url>
    <layout>legacy</layout>
    <uniqueVersion>false</uniqueVersion>
  </repository>
  <snapshotRepository>
    <id>upload-InternalSite</id>
    <name>Repository Name</name>
    <url>${discard-me-in-bit-bucket}</url>
    <layout>legacy</layout>
    <uniqueVersion>false</uniqueVersion>
  </snapshotRepository>
</distributionManagement>

This strategy causes the deploy goal to put things in the target directory, which of course is destroyed by the next CLEAN operation. To get even more aggressive, we have a postbuild step that does this:

find -type d -name '*_DELETEME' -exec rm -rf '{}' ';' -prune || echo $?

We employ yet one more strategy, too. In Hudson/Jenkins we provide a settings file to place the .m2 repository in the workspace for the job. This allows us to delete the entire repository before or after the job. It also makes artifacts visible in the workspace which aids in debugging some problems.

眼眸印温柔 2024-08-09 01:20:59

文件系统有多大? 我们每晚分配 10GB 来构建和删除 30 天以上的快照。 这似乎有效

您是否每隔 X 小时或在代码更改时进行构建? 切换到代码更改将减少工件数量,而不会降低覆盖范围。

您是否在本地安装所有快照? 您不需要在所有情况下都这样做。 在大多数情况下,只有那些主动开发依赖项的快照需要在本地安装。

您是否在本地安装 EAR/WAR 文件? 您可能也不需要它们。

您保留了多少个工作空间? 我们使用 hudson 并仅保留最后 5 个版本。

How big is the file system? We have 10gb allocated to builds and zap snapshots older than 30 days every night. That seems to work

Are you doing builds every X hours or when code changes? Switching to code changes will reduce the number of artifacts without reducing coverage.

Are you installing all snapshots locally? You don't need to do this in all cases. In most cases, just those snapshots that are actively developed dependancies need to be installed locally.

Are you installing EAR/WAR files locally? You probably don't need them either.

How many workspaces are you keeping? We use hudson and keep only the last 5 builds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文