Learn the basic steps to set up the Square SDK for a Java project.
If you're new to developing Square integrations using the Square Java SDK, try the Quickstart. It provides step-by-step instructions to explore your first project.
With the Square Java SDK, you can use either Maven or Gradle for dependency management and build automation. The following are general guidelines for creating a new Java project.
For more information about Maven, including installation instructions, see Welcome to Apache Maven.
Maven provides several archetypes, or templates, that you can use to set up your project. An archetype defines a project's directory structure, dependencies, and code samples that you can modify. For more information, see Maven Archetypes.
For example, you can use the Maven Quickstart to generate a simple project.
mvn archetype:generate \
-DinteractiveMode=false \
-DgroupId=com.square.examples \
-DartifactId=app \
-DarchetypeArtifactId=maven-archetype-quickstart
In this example, the project is created in a new subdirectory (./app).
The project object model (POM) defines the project's configuration, tasks, and dependencies. When you generate a project based on an archetype, Maven creates a POM definition file (pom.xml) that you can modify.
To use the Square Java SDK in your project:
Edit the ./app/pom.xml file and add a new dependency.
<dependency> <groupId>com.squareup</groupId> <artifactId>square</artifactId> <version>SDK_VERSION_HERE</version> <scope>compile</scope> </dependency>Replace SDK_VERSION_HERE with the latest version of the Square Java SDK: 41.1.0.20240919
When you compile your code, Maven resolves all dependencies and makes the Square Java SDK available to your project.
Alternatively, you can use Maven to download the Square Java SDK, and all other dependencies, to your local Maven repository.
mvn install -DskipTests
All the resources you need to run the program are saved in the .m2 directory, located in your home folder.
For more information about Gradle, including installation instructions, see Gradle Build Tool.
You can create a new Gradle project from the command line, using gradle init
. To do this, you first create a new (empty) directory for your project and then initialize the project there.
mkdir ./my-project
cd ./my-project
gradle init \
--project-name app \
--type java-application \
--package com.square.examples \
--dsl groovy \
--test-framework junit
The settings.gradle file defines the project's configuration, tasks, and dependencies.
To use the Square Java SDK in your project:
Edit the ./app/build.gradle file and add a new dependency to the
dependencies
section:implementation 'com.squareup:square:SDK_VERSION_HERE'Replace SDK_VERSION_HERE with the latest version of the Square Java SDK: 41.1.0.20240919
To build your program, use this command:
gradle build -x test
To run your program, use this command:
gradle run