15.5. Adding Gson to a Project

To use Gson in a Maven project, Gson must be added as a dependency in the project’s pom.xml file — refer to the Dependency Management section in Maven’s documentation for more information:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.10.1</version>
</dependency>

Once added as a dependency, Gson will automatically be available on the classpath. For example, you could include the following near the top of a class to make a Gson object available within that class — this code will compile using mvn compile so long as the Gson dependency is added to the pom.xml file correctly:

private static Gson GSON = new GsonBuilder()
    .setPrettyPrinting()
    .create();