Skip to content

UseGuavaInYourBuild

Colin Decker edited this page Oct 16, 2017 · 5 revisions

Using Guava in you build

Developers may use several different kinds of build system to compile/package their code. Guava can be used with any of them. For any code snippet below, please substitute the version given with the version of Guava you wish to use.

If you are building for Android, append -android to the version string.

Maven

Apache Maven is a system for building projects and source code into deployment binaries, executing tests, and other software assembly lifecycle actions. It supports managed dependencies from central and private repositories. Guava is deployed to the common maven repositories, so to use it, you need only add the following snippet to the <dependencies /> section of the project's pom.xml file.

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

JSR-305 note: If you are using JSR-305 annotations (Guava's only dependency) in your own code, you must declare that dependency directly.

Scala note: The Scala compiler, unlike the Java compiler, requires that annotations used by a library be available when compiling against that library. If you are compiling with Scala, you must declare a dependency on JSR-305.

Gradle

Gradle is a build system that uses a domain specific language to construct instructions on how to build, test, assemble, deploy software from source. It supports managed dependencies and can make use of the maven repository for software artifacts. In order to use guava from Gradle, one must ensure the maven standard repositories are available like so:

repositories {
  mavenCentral()
}

Then one can add the guava dependency in the dependencies section like so:

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

JSR-305 note: If you are using JSR-305 annotations (Guava's only dependency) in your own code, you must declare that dependency directly.

Scala note: The Scala compiler, unlike the Java compiler, requires that annotations used by a library be available when compiling against that library. If you are compiling with Scala, you must declare a dependency on JSR-305.

Ivy

Ivy is a dependency management system for builds. Ivy users should add the following line to the dependencies section of the ivy.xml file for any module that depends on Guava:

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

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

JSR-305 note: If you are using JSR-305 annotations (Guava's only dependency) in your own code, you must declare that dependency directly.

Scala note: The Scala compiler, unlike the Java compiler, requires that annotations used by a library be available when compiling against that library. If you are compiling with Scala, you must declare a dependency on JSR-305.

Buildr

With Buildr, include this:

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

JSR-305 note: If you are using JSR-305 annotations (Guava's only dependency) in your own code, you must declare that dependency directly.

Scala note: The Scala compiler, unlike the Java compiler, requires that annotations used by a library be available when compiling against that library. If you are compiling with Scala, you must declare a dependency on JSR-305.

Manual Dependencies

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

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

Clone this wiki locally