office-stamper stamps Spring Expression Language into Word templates. Since the beginning, the engine was welded to one library: docx4j. Every R, every P, every part of the document model came from docx4j’s classes.

That weld had been costing office-stamper for years. docx4j has a record of breaking its API between releases, and of depending tightly on whichever JAXB runtime the JVM or the classpath provides — every new version forced awkward choices on code built on top of it. Users who relied on the library before I started maintaining it had made one request especially clear: let them use the docx4j version they already run, instead of baking one into office-stamper’s hard dependencies. The split had sat at the back of my mind for years, and several earlier attempts never survived contact with the codebase. The freshly published docx4j 17 line, which repackaged the JAXB object model, was the drop that flooded the vase.

Version 4.0 breaks the weld. The engine artifact no longer contains docx4j — not “no imports”, no dependency — and the document access it needs goes through a small SPI that provider modules implement. This post is the tour of that split: what the seams are, and why each one is where it is.

The SPI is the test

The public contract is four interfaces and a couple of value types in pro.verron.officestamper.api: OfficeDocument, OfficePart, ElementFactory, and the reshaped AnchoredComment. OfficeDocument has four methods:

public interface OfficeDocument {
    OfficePart mainPart();
    List<OfficePart> parts();
    void save(OutputStream out);
    <T> T unwrap(Class<T> type);
}

unwrap is the escape hatch: a provider can hand back its native object, and code that really needs docx4j can ask for it. Everything else on the SPI is deliberately narrower than what docx4j offers, and that narrowness is the point. When the engine cannot express an operation through the SPI, the compiler says so — there is no longer a convenient docx4j class one import away to make the wrong thing easy.

One decision did more work than the rest: element creation lives on the part, not the engine. OfficePart.factory() returns an ElementFactory that knows how to build runs, paragraphs, and the other replacement content. The javadoc on OfficePart spells out the rule:

replacement content is always created by the same provider that owns the part being modified — resolvers and exception resolvers never build elements themselves

That rule is what let the exception resolver drop its OfficePart parameter entirely. The engine never builds a w:r; it asks the part that owns the paragraph to build one. A raw-XML provider and a docx4j provider can therefore produce different kinds of runs for the same expression, each correct for its document.

A package exported to exactly four modules

The engine keeps a core package of internals — paragraph processors, tag machinery, the stamping pipeline. Before the split it was public de facto, because everything lived in one artifact and public was the only visibility that mattered. Now the module descriptor decides:

module pro.verron.officestamper {
    requires spring.core;
    requires spring.expression;
    requires pro.verron.officestamper.utils;
    uses pro.verron.officestamper.api.OfficeStamper;

    exports pro.verron.officestamper.api;
    exports pro.verron.officestamper.preset;

    exports pro.verron.officestamper.core to pro.verron.officestamper.docx4j11.test,
            pro.verron.officestamper.docx4j11,
            pro.verron.officestamper.docx4j17.test,
            pro.verron.officestamper.docx4j17;
}

core is exported to the two providers and their test modules, and to nobody else. A user of the library cannot reach into it even with reflection; a pro.verron.officestamper.core import from application code does not compile. This is the visibility the old single-module layout could only approximate with naming conventions.

The dependency-level cut went further than the import-level one. The engine also dropped java.desktop, which it had been using for one thing: java.awt.Dimension in the image utilities. A custom Dimension record replaced it. When you decide an engine module must not pull in a dependency, every transitive convenience gets the same audit.

One source tree, compiled twice

The providers — officestamper-docx4j11 and officestamper-docx4j17 — share almost everything. Sixty classes of adapter code are identical between the two flavors, and early in the migration they existed as twins in both modules. Sonar’s duplication gate flagged the pair, and fixing it by hand would have meant maintaining the drift forever.

The fix is unusual Maven: a module with sources but no artifact of its own.

officestamper-docx4j-sources holds the shared src/main/java tree; a sibling officestamper-test-sources holds the shared tests. Neither produces a jar. Each provider adds them as extra source roots:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-shared-docx-sources</id>
            <phase>generate-sources</phase>
            <goals><goal>add-source</goal></goals>
            <configuration>
                <sources>
                    <source>${project.basedir}/../officestamper-docx4j-sources/src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

So one source tree is compiled twice, once against each docx4j line, and the two provider jars are siblings with no runtime coupling to a “shared” artifact that would only confuse dependency trees.

What differs between the flavors? After this refactor, exactly one class: JaxbCompat. docx4j’s JAXB elements carry a parent-tracking interface whose fully-qualified name moved between the two lines — org.jvnet.jaxb2_commons.ppp.Child in the 11 line, org.jvnet.jaxb.lang.Child in the 17 line. Both interfaces declare the same operations, so the shared sources route every parent-tracking access through one seam class, implemented once per flavor under the same package and name:

// officestamper-docx4j11: import org.jvnet.jaxb2_commons.ppp.Child;
// officestamper-docx4j17: import org.jvnet.jaxb.lang.Child;
public static @Nullable Object parentOf(Object child) {
    if (child instanceof Child c) return c.getParent();
    return null;
}

Sixty identical classes and one deliberate difference. That ratio is the health signal I watch now: when the next docx4j packaging change lands, the blast radius should stay one file.

The proof: a provider that isn’t docx4j

An SPI is only proven when something other than the original implementation can satisfy it. So the migration grew a third provider, officestamper-xml, which parses word/document.xml and word/comments.xml with plain org.w3c.dom — no docx4j anywhere on its classpath.

Its module descriptor states the intent:

Exists to validate that the decoupled engine + provider SPI is sufficient to stamp documents without docx4j; deliberately narrow (body paragraphs, runs, inline smart tags, anchored comments). Not production-ready.

It is small, but it is real: it re-implements the same replace-with-formatting fidelity problem the docx4j provider solved, splicing character spans out of a paragraph’s canonical text and cloning run properties onto what it inserts. And its round-trip behavior is stricter than docx4j’s — every zip entry it does not understand is carried through byte-for-byte and re-serialized untouched, so a stamping pass cannot silently rewrite parts it never meant to touch.

The engine never noticed the difference. That is the whole test, and it passed.

What this means for users

On the module path, nothing changes about how you use a stamper: the providers register through ServiceLoader, and the engine consumes them through uses. On the classpath, the dependency you add changes: instead of the engine plus a manual docx4j line, you add one provider artifact — pro.verron.office-stamper:officestamper-docx4j11 or officestamper-docx4j17 — and it brings the matching docx4j itself. Depending on the engine alone now fails loudly at startup with “No OfficeStamper provider found on the classpath”, which is the signal that the provider is missing.

For code that touched internals, 4.0 keeps the pre-4.0 classes working through compatibility shims, and the project site carries a migration guide with a per-type mapping table. I will cover the migration machinery — the shims, the utils upgrade trap, and the auditable API diff — in a separate post.

The split took about two weeks of red-test-driven work across some seventy commits, and the shape it left behind is the one I wanted from the start: an engine that knows nothing about docx4j, providers that each own their document model, and a seam between them narrow enough to read in one sitting. It also leaves open a door the welded engine could never open: trimming artifact size, and eventually streaming documents through the SPI instead of loading a full file into memory. That is a story for a later release.