apache/maven · error · ModelInterpolationException

Cannot serialize project model for interpolation.

Error message

Cannot serialize project model for interpolation.

What it means

Thrown by AbstractStringBasedModelInterpolator when the first step of string interpolation - serializing the Model back to XML with MavenStaxWriter - fails with IOException or XMLStreamException. The whole legacy interpolation strategy round-trips the model through XML text; if the model cannot be serialized (invalid characters, broken writer state), interpolation aborts with ModelInterpolationException before any ${} expression is evaluated.

Source

Thrown at compat/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java:139

    @Deprecated
    @Override
    public Model interpolate(Model model, Map<String, ?> context, boolean strict) throws ModelInterpolationException {
        Properties props = new Properties();
        props.putAll(context);

        return interpolate(model, null, new DefaultProjectBuilderConfiguration().setExecutionProperties(props), true);
    }

    @Override
    public Model interpolate(Model model, File projectDir, ProjectBuilderConfiguration config, boolean debugEnabled)
            throws ModelInterpolationException {
        StringWriter sWriter = new StringWriter(1024);

        MavenStaxWriter writer = new MavenStaxWriter();
        try {
            writer.write(sWriter, model.getDelegate());
        } catch (IOException | XMLStreamException e) {
            throw new ModelInterpolationException("Cannot serialize project model for interpolation.", e);
        }

        String serializedModel = sWriter.toString();
        serializedModel = interpolate(serializedModel, model, projectDir, config, debugEnabled);

        StringReader sReader = new StringReader(serializedModel);

        MavenStaxReader modelReader = new MavenStaxReader();
        try {
            model = new Model(modelReader.read(sReader));
        } catch (XMLStreamException e) {
            throw new ModelInterpolationException(
                    "Cannot read project model from interpolating filter of serialized version.", e);
        }

        return model;
    }

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Identify what mutated the model before interpolation: run with -X and inspect which plugin executes in earlier phases (model-processed POM is dumped to target/)
  2. Sanitize property values that contain XML-illegal control characters
  3. Update Maven and custom model-processing plugins; legacy string interpolation is deprecated in favor of field-wise interpolation in newer Maven lines
  4. If embedding, prefer the non-legacy interpolators (StringSearchModelInterpolator via the standard builder) or avoid re-interpolating already-interpolated models

Example fix

// before: plugin injects raw control char into a property
model.getProperties().put("banner", "start\u0003end");
// after: strip non-XML characters before model processing
String clean = raw.replaceAll("[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]", "");
model.getProperties().put("banner", clean);
Defensive patterns

Strategy: try-catch

Validate before calling

// Reject XML-illegal control characters before feeding the model
static String xmlSafe(String s) { return s == null ? null : s.replaceAll("[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]", ""); }

Try / catch

try {
    model = interpolator.interpolate(model, projectDir, config, debug);
} catch (ModelInterpolationException e) {
    // serialization failed: inspect recently mutated model fields, sanitize and retry
}

Prevention

When it happens

Trigger: interpolate(model, ...) on a model containing data the StAX writer cannot emit: control characters (raw \u0000-\u0008) inside properties/strings accumulated from interpolated sub-models, or a writer/encoding failure. The model parsed fine (it came from MavenStaxReader) but cannot round-trip.

Common situations: Rare in normal builds; seen when plugins mutate the Model in memory (adding raw control chars or malformed CDATA), when properties contain characters illegal in XML 1.0, or in memory/encoding-constrained or wrapped writer environments during embedded Maven usage.

Related errors


AI-assisted analysis of apache/maven@e4093d4e12 (2026-08-21). Data as JSON: /api/errors/b2669cad95b9ebad. Report an issue: GitHub.