Why does Maven disable caching for HTTP requests?
Asked Answered
W

1

6

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

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.

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

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.

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?

Winchell answered 29/10, 2010 at 18:45 Comment(1)
I suppose technically, "cache-control: no-cache" and "pragma: no-cache" allow for caching, given validation, and "cache-store: no-store" is not a valid HTTP header, so maybe we can get cached responses.Winchell
U
7

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.

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>

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>

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

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

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?

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

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.

References

Unreserve answered 30/10, 2010 at 1:57 Comment(3)
It'd be helpful to note that Maven 2.2.0 will fail with the server stanza above because it defaults to the HttpClient wagon. But with 2.2.1, works perfectly. Thanks!Winchell
You're welcome. And indeed, the mentioned versions are related to the availability of said setter in the API, independently of the default Wagon used. But this should be cleared now.Unreserve
Maven 3 uses HTTPClientWagon again, so the solution is to disable default headers as shown in maven.apache.org/guides/mini/guide-http-settings.htmlLinnea

© 2022 - 2024 — McMap. All rights reserved.