Skip to content

Lesson 7 of 9 · 28m

Migrating a Java Codebase Incrementally Without Freezing the Roadmap

Sequence conversion by boundaries, keep mixed-language builds stable, and use strangler-style module migration instead of a big-bang rewrite.

The failed migrations I have watched all shared the same fantasy: convert enough files that the project becomes "mostly Kotlin," then productivity magically arrives. What actually arrived was dual-language complexity with neither language idiomatic. Successful migrations move capability by capability, keeping the system deployable and the architecture legible.

Start where Kotlin buys leverage

Do not begin with the most central legacy class. Begin with new features, leaf services, tests, or integration adapters where nullable input and concurrency are painful:

good first candidates       poor first candidates
new API endpoints           shared base entity hierarchies
edge adapters               generated serialization models
background jobs             cross-cutting framework plumbing
tests and fixtures          highly reflective framework glue

The goal is to create islands of excellent Kotlin that force better boundaries, not to randomize file extensions.

Mixed builds are normal — make them boring

For Gradle, Kotlin compiles before Java in the same source set in many projects, so Java cannot depend on Kotlin unless you arrange compilation correctly. The cleanest long-term answer is module separation:

orders-api/       interfaces and DTOs
orders-core/      Kotlin implementation
orders-legacy/    old Java implementation during transition
orders-app/       Spring/Ktor composition root

Convert behavior, not syntax

Automated conversion produces Java-shaped Kotlin. This is acceptable as a checkpoint, not as a destination:

// conversion artifact
var customer: Customer? = repository.find(id)
if (customer == null) {
    throw NotFoundException()
}

// Kotlin-shaped design
val customer = repository.find(id)
    ?: throw NotFoundException("customer $id not found")

The second pass is where you remove null ambiguity, collapse mutable ceremony, introduce value types, and make threading explicit. Schedule that pass; otherwise the migration stalls in the worst middle state.

Preserve behavior at the boundary

Keep contract tests around endpoints, message consumers, scheduled jobs, and database migrations. A conversion that changes JSON null behavior, validation order, transaction boundaries, or logging identity is not a refactor.

For critical paths, run shadow verification: invoke both implementations with the same inputs and compare persisted outcomes. This is expensive, so reserve it for billing, permissions, inventory, and other irreversible domains.

Use Kotlin first in new code

Write the policy down: new endpoints, services, and tests are Kotlin unless there is a concrete exception. Without that rule, Java keeps expanding underneath the migration and the finish line recedes. Deprecate legacy extension points deliberately. Teams continue using whichever base class is easiest to copy.

Field notes

  • Convert tests early. They preserve behavior and teach the team idiomatic Kotlin faster than production-only conversion.
  • Watch Lombok carefully; migrating entities before understanding generated constructors and builders causes serialization regressions.
  • Keep commits small and mechanical. Syntax conversion, semantic cleanup, and package movement should not be one diff.

Was this lesson helpful?

Sign in to rate