Skip to content

UseGuavaInYourBuild

cpovirk edited this page Dec 8, 2017 · 22 revisions

Using Guava in your build

How to configure your build

For any code snippet below, please substitute the version given with the version of Guava you wish to use.

Add the following snippet to the <dependencies /> section:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.5-jre</version> <!-- or 23.5-android for the Android flavor -->
</dependency>

Ensure the maven standard repositories are available like so:

repositories {
  mavenCentral()
}

Then add the Guava dependency in the dependencies section like so:

dependencies {
  compile group: 'com.google.guava', name: 'guava', version: '23.5-jre' # or 23.5-android for the Android flavor
}

Add the following line to the dependencies section:

<dependency org="com.google.guava" name="guava" rev="23.5-jre" /> <!-- or rev="23.5-android" for the Android flavor -->

and make sure that the "public" resolver is used.

Include this:

compile.with 'com.google.guava:guava:jar:23.5-jre' # or '...:23.5-android' for the Android flavor

Manual Dependencies

You can also just manually download JARs for the classes, sources and javadocs. See the appropriate release.

What about GWT?

In all the examples above, leave "com.google.guava" as it is but replace the other occurrence of "guava" with "guava-gwt". (There is no GWT artifact for the Android flavor.)

What about Guava's own dependencies?

Guava depends on some artifacts that contain annotations. This is primarily for use by static analysis tools and certain compilers, so these annotations need not be present in your runtime classpath unless you want your app to recognize those annotations. In many cases, they need not be present even in your compile-time classpath. Yet we make them available in your compile-time classpath by default to avoid errors in unusual cases. (For examples, see #2652 / #2837 (annotation processors), #2721 / #2824 (-Xlint:all -Werror), and #1095 / #1127 (Scala).) If you wish to avoid the dependencies, you can usually safely exclude them.

(Ideally, we would make the annotations available at compile-time but not runtime. However, this is not currently possible in Maven's model.)

Clone this wiki locally