Travis CI Caching: Cut Build Times by 30‑70% with Maven and npm
Travis CI’s cache directive can cut Java or Node build times by 30‑70%. This blog shows a concrete .travis.yml example, explains cache keys, and warns about stale artifacts and storage limits.
22 May 2026, 14:14 UTC

Problem: Every build rebuilds everything
In open‑source projects, Travis CI runs a full dependency resolution on each push. For Maven or npm projects, that means downloading hundreds of jars or thousands of npm packages, which can take 5–15 minutes and consume CI minutes.
Thesis: Cache the dependency layer
Travis offers a cache directive that persists directories between runs. When used correctly, it can shave 30‑70% off build time and reduce minutes usage.
Worked Example: Caching Maven and npm
Below is a minimal .travis.yml that caches the Maven repository and the npm node_modules directory. The cache key is derived from the lock file so that the cache is invalidated automatically when dependencies change.
language: java
# For Maven projects
cache:
directories:
- "$HOME/.m2/repository"
key: "$TRAVIS_REPO_SLUG-maven-{{checksum .travis.yml}}"
script:
- mvn -B clean verify
# For Node projects
# (uncomment the following lines if you also use npm)
# language: node_js
# node_js:
# - "14"
# cache:
# directories:
# - "node_modules"
# key: "$TRAVIS_REPO_SLUG-node-{{checksum package-lock.json}}"
Explanation of the key:
$TRAVIS_REPO_SLUGkeeps the cache scoped to the repo.- The
checksumfunction (available in Travis 3.0+) hashes the lock file; ifpom.xmlorpackage-lock.jsonchanges, the key changes and a fresh cache is created. - Without a key, Travis uses a default hash of the entire repository, which changes on every commit and defeats the purpose of caching.
Trade‑off: Stale caches and storage limits
While caching speeds up builds, it introduces a few pitfalls:
- Stale artifacts: If a dependency updates in a remote repository but the lock file is not updated, the cache will still serve the old jar/npm package. Tests may pass locally but fail in CI. Always run
mvn dependency:resolveornpm ciafter a lock file change. - Cache size: Large caches can hit Travis’s 5 GB per repository limit. Monitor the Cache tab in the Travis UI or use the API to check
sizeandlast_updated. Periodically prune~/.m2/repositoryornode_modulesto keep the cache lean. - Manual key changes: If you forget to update the key when adding a new dependency, the cache will stay in sync and you’ll get a false sense of speed. Automate key generation with lock file checksums to avoid this.
Actionable Next Steps
- Add the
cachesection to your.travis.ymlas shown above. - Commit a change that updates a dependency (e.g., bump a Maven version) and watch Travis rebuild the cache.
- Compare the Build time and CI minutes in the Travis UI before and after enabling caching.
- Set up a small script that runs
mvn dependency:go-offlineornpm ciafter a cache restore to catch stale dependencies early. - Use the Travis API (
https://api.travis-ci.com/repo/:id/cache) to fetch cache metadata and automatically trigger a cache purge when the size exceeds a threshold.
By following these steps, you can reliably reduce build times, lower CI costs, and keep your CI pipeline healthy.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.