Java services are a large share of the code that product teams and services firms maintain, and they are not an easy fit for a coding agent on a laptop. A Maven or Gradle build downloads a lot on the first run, uses a lot of memory, and can take long enough that an agent waiting on it looks stuck. Give three agents the same laptop and the builds fight over memory.
This post is about setting up one agent’s machine for a Java repository so the loop stays quick. Commands for the Sandscale CLI come from version 1.4.1. The Maven and Gradle options come from their own documentation. We do not show build output, because it depends on your project.
Why a machine per agent suits a JVM build
Three things make a Java build different from a typical script test run:
- A cold start is expensive. The first build resolves and downloads dependencies, and later builds reuse a local cache (
~/.m2/repositoryfor Maven,~/.gradlefor Gradle). - The JVM wants memory. Unless you set it, a modern JVM caps its heap at a fraction of the machine’s memory (a quarter by default), and a Gradle build can have a daemon and a separate test JVM running at once.
- Parallel agents multiply both. Two agents sharing one machine share, and corrupt, one cache and one pool of memory.
A Sandscale workspace is a persistent cloud machine with its own disk, CPU and memory, per agent. The disk survives stops and starts, so the dependency cache and the compiled classes are still there next time. The first build is the slow one. Later ones are not.
What goes in the template
Ask whoever administers your workspace templates to put these in once, so no agent installs them on the fly:
- A pinned JDK, the version your service builds with in CI.
- Your build tool through the project’s wrapper where you have one, so the machine runs
./mvnwor./gradlewand the version is the repository’s, not the machine’s. git, and whatever your tests call outside the JVM (a database client, for instance).
Then each engineer creates a machine for the task:
sandscale workspace create --name pay-2210-refund-api --template-id TEMPLATE_ID --ttl-seconds 28800
sandscale workspace ssh pay-2210-refund-api
Whether the machine can reach Maven Central or your internal artifact repository depends on your plan and your organization’s network policy. If the first build cannot download, that is the first place to look.
Warm the cache once
Before handing the agent a task, run the dependency resolution on the machine so the agent does not pay for it in its first loop. Use what your build offers:
./mvnw -q dependency:go-offline
./gradlew --quiet dependencies > /dev/null
These resolve dependencies and plugins into the local cache. They are standard Maven and Gradle tasks, but how complete the result is varies by project (plugins, test scopes), so treat them as a warm-up, not a guarantee of offline builds.
The loop: one test, not the suite
An agent that reruns the entire suite after every edit will be slow on any machine. Teach it the single-test forms.
Maven, using Surefire’s -Dtest option:
./mvnw -q test -Dtest=RefundServiceTest
./mvnw -q test -Dtest='RefundServiceTest#rejectsExpiredOrder'
Gradle, using --tests:
./gradlew test --tests 'com.acme.pay.RefundServiceTest'
./gradlew test --tests 'com.acme.pay.RefundServiceTest.rejectsExpiredOrder'
In a multi-module build, tell the agent which module to build, with -pl module -am in Maven or :module:test in Gradle, so it does not compile the whole tree for a one-class change.
Memory: make the settings explicit
Two places set memory, and an agent will not guess either:
- The build tool’s own JVM. For Gradle,
org.gradle.jvmargsingradle.properties; for Maven,MAVEN_OPTSor.mvn/jvm.config. - The test JVM that runs your tests. Gradle’s
maxHeapSizeon the test task; Surefire’sargLine.
Look at your CI configuration, which probably sets these already, and copy the values. Then pick the machine size from them rather than from a guess: a workspace comes with a memory allowance per vCPU (4 GiB per vCPU on Pro and Team), and memory beyond that counts as extra vCPU-hours. Our post on sizing a sandbox explains how to measure instead of guess. A machine sized for the loop is usually smaller than one sized for the full suite.
What the agent should not do
- Run the full suite after every edit. Put that in
AGENTS.md. - Delete the cache to “fix” a build. The cache is what makes the second build fast. Tell the agent to ask first.
- Start a database or message broker on the machine without being asked. If your integration tests use Testcontainers, they need a container runtime on the machine. Whether your template has one is a template question, so ask your administrator before telling an agent to run those tests, and do not assume.
The AGENTS.md block
## Build and test (Java)
- Use ./mvnw (or ./gradlew), never a global Maven or Gradle.
- While working, run only the test class you changed:
./mvnw -q test -Dtest=<ClassName>
- Run the module's tests before committing: ./mvnw -q test -pl <module> -am
- Do not delete ~/.m2 or ~/.gradle. If a build looks stuck on dependencies, say so and stop.
- Do not run integration tests (names ending in IT) unless the task says to.
- Before opening a PR, run the full build once and put the result in the PR description.
The full suite and the integration tests
Those are the steps that do not fit a loop-sized machine. A full build with integration tests can want many cores for a short time, and that is the case for leasing a build host for that one step and releasing it afterwards. Our post on leasing a build host for a full test suite shows the commands. Leases count against the same allowance as workspaces, only while held.
Handing it over
When the agent finishes, it pushes a branch and opens a pull request. The reviewer, who may be in another time zone, reads the branch and does not need access to the agent’s machine. Stop the machine when the task is done, since a running machine counts toward the plan’s hours whether the agent is busy or waiting:
sandscale workspace stop --ws-id WORKSPACE_ID
The disk is kept, so next time the cache is still warm. For a team in India running a Java service for clients abroad, see also handing work across an IST and US team, and for planning hours across several agents, our worksheet.