How to Include Guava in a Maven Project: Complete Dependency Guide
To include Guava in a Maven project, declare a <dependency> with group ID com.google.guava, artifact ID guava, and select either the -jre or -android version suffix to match your target runtime environment.
Guava is Google's core Java utilities library published to Maven Central. Whether you are building a server-side application or Android software, adding Guava to your pom.xml requires understanding the two available build flavors and their specific Maven coordinates as defined in the google/guava repository.
Choosing the Right Guava Flavor (JRE vs Android)
Guava provides two distinct build flavors that determine API availability and Java version compatibility. Choosing the correct flavor when you add Guava to your Maven build prevents runtime errors and ensures access to the appropriate feature set.
-
JRE Flavor (
-jresuffix): Targets Java 8 and higher. This version contains the complete Guava API including classes that depend on newer Java language features. Use this for standard server-side or desktop applications. -
Android Flavor (
-androidsuffix): Compatible with Android runtimes and older Java versions. This flavor excludes APIs that rely on Java 8+ language features unavailable on Android. Referenceandroid/pom.xmlin the repository for the exact build configuration.
According to the Guava README and build definitions, the version suffix determines which artifact variant Maven resolves.
Adding the Maven Dependency
Add Guava to your Maven project by inserting a <dependency> block inside the <dependencies> section of your pom.xml. Use the coordinates com.google.guava:guava with the appropriate version string.
Standard JRE dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.6.0-jre</version>
</dependency>
Android-compatible dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.6.0-android</version>
</dependency>
Replace 33.6.0 with the latest release version found in guava/pom.xml or the project's release notes.
Understanding Guava's Bill of Materials (BOM)
For multi-module Maven projects, Guava provides a Bill of Materials (BOM) to ensure consistent versioning across dependencies. The BOM is defined in guava-bom/pom.xml and allows you to import a managed set of Guava versions via <dependencyManagement>.
Using the BOM prevents version conflicts when multiple modules depend on Guava:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-bom</artifactId>
<version>33.6.0-jre</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<!-- Version is inherited from BOM -->
</dependency>
</dependencies>
Complete Working Example
Below is a minimal, runnable Maven project structure demonstrating how to include Guava in a Maven project and use its ImmutableList class.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>guava-demo</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.6.0-jre</version>
</dependency>
</dependencies>
</project>
src/main/java/com/example/App.java:
package com.example;
import com.google.common.collect.ImmutableList;
public class App {
public static void main(String[] args) {
ImmutableList<String> items = ImmutableList.of("Guava", "in", "Maven");
System.out.println(items);
}
}
Running mvn compile exec:java confirms Guava is correctly resolved from Maven Central and available on the classpath.
Summary
- Guava is available on Maven Central under
com.google.guava:guavawith two flavor suffixes:-jrefor Java 8+ and-androidfor Android compatibility. - The JRE flavor defined in
guava/pom.xmlprovides the complete API, while the Android flavor inandroid/pom.xmlexcludes Java 8-dependent features. - Guava automatically pulls in
com.google.guava:failureaccess:1.0.3as a transitive runtime dependency. - Use
guava-bom/pom.xmlvia<dependencyManagement>to enforce consistent Guava versions across multi-module projects.
Frequently Asked Questions
What is the difference between the JRE and Android flavors of Guava?
The JRE flavor requires JDK 1.8 or higher and includes all Guava APIs, including those using Java 8 language features like streams and lambdas. The Android flavor targets Android runtimes and excludes APIs that depend on Java 8 features unavailable on older Android versions. Both flavors share the same group ID and artifact ID but use different version suffixes (-jre vs -android).
Does Guava have any transitive dependencies I need to worry about?
Guava has a runtime dependency on com.google.guava:failureaccess:1.0.3, which Maven automatically resolves and includes in your classpath. According to the Guava README, no additional manual dependency management is required for standard usage, though you should verify this in guava/pom.xml when upgrading versions.
How do I manage Guava versions across multiple Maven modules?
Import the Guava BOM (Bill of Materials) from guava-bom/pom.xml in your parent POM's <dependencyManagement> section using scope import. This locks all Guava artifacts to the same version across child modules without requiring explicit version tags in individual dependency declarations.
Can I use Guava with Java versions older than Java 8?
No. Both Guava flavors require at least Java 8. The Android flavor maintains compatibility with Android runtimes but still requires Java 8 source compatibility. If you require support for Java 7 or earlier, you must use Guava version 23.0-android or earlier, though these versions are no longer maintained.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →