为什么Maven禁用HTTP请求的缓存? [英] Why does Maven disable caching for HTTP requests?

查看:290
本文介绍了为什么Maven禁用HTTP请求的缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTTP请求的默认Maven设置,例如Maven用于从存储库获取工件的设置,包括以下标头:

The default Maven settings for HTTP requests, such as the ones Maven uses to fetch artifacts from repositories, include the following headers:

Cache-control: no-cache
Cache-store: no-store
Pragma: no-cache
Expires: 0
Accept-Encoding: gzip

这似乎是记录的行为。 HTTP的默认Maven旅行车(即轻量级客户端)似乎不允许禁用这些标头。

This seems to be the documented behavior. The default Maven wagon for HTTP (i.e., the "lightweight" client) does not seem to allow disabling these headers.

为什么Maven默认以这种方式配置?对于实际拥有版本的工件,它们永远不会改变,对吧?

Why is Maven configured in this way by default? For artifacts that actually have versions, they should never change, right?

我在许多开发人员共享一个公共HTTP代理的环境中工作,这种行为意味着开发人员永远不会受益来自缓存。并且,我们在所有依赖项上都有 dependencyManagement ,并且不使用SNAPSHOT或其他可能更改的版本,所以看起来缓存应该是安全的。

I work in an environment where many developers share a common HTTP proxy and this behavior means that developers never benefit from caching. And, we have dependencyManagement on all of our dependencies and do not use SNAPSHOTs or other versions that might change, so it seems that caching should be safe.

我可以在settings.xml或pom.xml中放置什么来禁用这些标头并允许我们的代理缓存响应并返回它们?

What can I put in my settings.xml or pom.xml to disable these headers and allow our proxy to cache responses and return them?

推荐答案


这似乎是记录在案的行为。 HTTP的默认Maven旅行车(即轻量级客户端)似乎不允许禁用这些标头。

This seems to be the documented behavior. The default Maven wagon for HTTP (i.e., the "lightweight" client) does not seem to allow disabling these headers.

实际上,你可以配置轻量级HTTP Wagon 客户端使用可用的setter,例如(Maven 2.0 +):

Actually, you can configure the Lightweight HTTP Wagon client using the available setters, for example (Maven 2.0+):

<servers>
  <server>
    <id>central</id>
    <configuration>
      <useCache>true</useCache>
    </configuration>
  </server>
</servers>

甚至覆盖或提供额外的HTTP标头(Maven 2.1 +):

Or even override or provide additional HTTP headers (Maven 2.1+):

<server>
  <id>central</id>
  <configuration>
    <httpHeaders>
      <property>
        <name>User-Agent</name>
        <value>Internal-Build-System/1.0</value>
      </property>
    </httpHeaders>
  </configuration>
</server>

Brett Porter在配置Maven HTTP连接

This is nicely covered by Brett Porter in Configuring Maven HTTP Connections.


为什么Maven默认以这种方式配置?

Why is Maven configured in this way by default?

狂野猜测:这是一个安全的默认设置,可以避免配置错误的问题代理(不确定这是真的)。​​

Wild guess: it's a safe default to avoid problems with badly configured proxies (not really sure this is true).


我可以在settings.xml或pom.xml中添加什么来禁用这些标头和允许我们的代理缓存响应并返回它们?

What can I put in my settings.xml or pom.xml to disable these headers and allow our proxy to cache responses and return them?

以上设置进入 settings.xml (当然,如果需要,调整 id central 用于Maven使用的默认存储库)。

The above settings go in the settings.xml (of course, adapt the id if required, central is for the default repository used by Maven).

如果它不起作用(它应该),另一种方法是切换回 HTTPClient Wagon 并按照 HttpClient HTTP Wagon的高级配置

If it doesn't work (it should), the alternative would be to switch back to HTTPClient Wagon and to configure it as documented in the Advanced Configuration of the HttpClient HTTP Wagon.

  • Configuring Maven HTTP Connections
  • Advanced Configuration of the HttpClient HTTP Wagon

这篇关于为什么Maven禁用HTTP请求的缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆