|
| 1 | +--- |
| 2 | +title: Maven |
| 3 | +ogTitle: Remote caching for Maven builds |
| 4 | +description: Learn how to use Depot remote caching for Maven builds |
| 5 | +--- |
| 6 | + |
| 7 | +[**Maven**](https://maven.apache.org/) is a build automation and project management tool primarily used for Java projects that helps developers manage dependencies, build processes, and documentation in a centralized way. It follows a convention-over-configuration approach by providing a standard project structure and build lifecycle, allowing teams to quickly begin development without extensive configuration. |
| 8 | + |
| 9 | +[**Depot Cache**](/docs/cache/overview) provides a remote cache service that can be used with Maven, allowing you to incrementally cache and reuse parts of your builds. This cache is accessible from anywhere, both on your local machine and on CI/CD systems. |
| 10 | + |
| 11 | +## Configuring Maven to use Depot Cache |
| 12 | + |
| 13 | +Depot Cache can be used with Maven from Depot's managed GitHub Actions runners, your local machine, or any CI/CD system. |
| 14 | + |
| 15 | +### From Depot-managed Actions runners |
| 16 | + |
| 17 | +[Depot GitHub Actions runners](/docs/github-actions/overview) are pre-configured to use Depot Cache with Maven - each runner is launched with a `settings.xml` file that is pre-populated with the connection details for Depot Cache. You must verify that remote caching is enabled via the [Maven Build Cache extension](https://maven.apache.org/extensions/maven-build-cache-extension/index.html) in `.mvn/maven-build-cache-config.xml`: |
| 18 | + |
| 19 | +```xml |
| 20 | +<cache xmlns="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0" |
| 21 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 22 | + xsi:schemaLocation="http://maven.apache.org/BUILD-CACHE-CONFIG/1.0.0 https://maven.apache.org/xsd/build-cache-config-1.0.0.xsd"> |
| 23 | + <configuration> |
| 24 | + <enabled>true</enabled> |
| 25 | + <hashAlgorithm>SHA-256</hashAlgorithm> |
| 26 | + <validateXml>true</validateXml> |
| 27 | + <remote enabled="true" saveToRemote="true" id="depot-cache"> |
| 28 | + <url>https://cache.depot.dev</url> |
| 29 | + </remote> |
| 30 | + <projectVersioning adjustMetaInf="true" /> |
| 31 | + </configuration> |
| 32 | +</cache> |
| 33 | +``` |
| 34 | + |
| 35 | +It is important to note that the `id` of your remote cache must be set to `depot-cache` for the Depot Cache service to work correctly in Depot GitHub Actions Runners. The cache will not be used if you use a different ID. |
| 36 | + |
| 37 | +You should also verify that you have registered the Build Cache extension in your `pom.xml` file: |
| 38 | + |
| 39 | +```xml |
| 40 | +<build> |
| 41 | + <extensions> |
| 42 | + <extension> |
| 43 | + <groupId>org.apache.maven.extensions</groupId> |
| 44 | + <artifactId>maven-build-cache-extension</artifactId> |
| 45 | + <version>1.0.1</version> |
| 46 | + </extension> |
| 47 | + </extensions> |
| 48 | +</build> |
| 49 | +``` |
| 50 | + |
| 51 | +If this automatic configuration is incompatible with your specific setup, you can disable automatic configuration in your organization settings page and manually configure Maven to use Depot Cache, as described below. |
| 52 | + |
| 53 | +### From your local machine or any CI/CD system |
| 54 | + |
| 55 | +To manually configure Maven to use Depot Cache, you will need to configure remote caching in your `~/.m2/settings.xml` file. Configure Maven to use the Depot Cache service endpoints and set your API token where there is the `DEPOT_TOKEN` below: |
| 56 | + |
| 57 | +`settings.xml`: |
| 58 | + |
| 59 | +```xml |
| 60 | +<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 61 | + xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd"> |
| 62 | + <servers> |
| 63 | + <server> |
| 64 | + <id>depot-cache</id> |
| 65 | + <configuration> |
| 66 | + <httpHeaders> |
| 67 | + <property> |
| 68 | + <name>Authorization</name> |
| 69 | + <value>Bearer DEPOT_TOKEN</value> |
| 70 | + </property> |
| 71 | + </httpHeaders> |
| 72 | + </configuration> |
| 73 | + </server> |
| 74 | + </servers> |
| 75 | +</settings> |
| 76 | +``` |
| 77 | + |
| 78 | +**Note: Maven support currently only supports Depot Organization API tokens, not user tokens.** |
| 79 | + |
| 80 | +## Using Depot Cache with Maven |
| 81 | + |
| 82 | +Once Maven is configured to use Depot Cache, you can run your builds as usual. Maven will automatically communicate with Depot Cache to fetch and reuse any stored build artifacts from your previous builds. |
0 commit comments