{"record":{"id":"b2669cad95b9ebad","repo":"apache/maven","slug":"cannot-serialize-project-model-for-interpolation","errorCode":null,"errorMessage":"Cannot serialize project model for interpolation.","messagePattern":"Cannot serialize project model for interpolation\\.","errorType":"exception","errorClass":"ModelInterpolationException","httpStatus":null,"severity":"error","filePath":"compat/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java","lineNumber":139,"sourceCode":"    @Deprecated\n    @Override\n    public Model interpolate(Model model, Map<String, ?> context, boolean strict) throws ModelInterpolationException {\n        Properties props = new Properties();\n        props.putAll(context);\n\n        return interpolate(model, null, new DefaultProjectBuilderConfiguration().setExecutionProperties(props), true);\n    }\n\n    @Override\n    public Model interpolate(Model model, File projectDir, ProjectBuilderConfiguration config, boolean debugEnabled)\n            throws ModelInterpolationException {\n        StringWriter sWriter = new StringWriter(1024);\n\n        MavenStaxWriter writer = new MavenStaxWriter();\n        try {\n            writer.write(sWriter, model.getDelegate());\n        } catch (IOException | XMLStreamException e) {\n            throw new ModelInterpolationException(\"Cannot serialize project model for interpolation.\", e);\n        }\n\n        String serializedModel = sWriter.toString();\n        serializedModel = interpolate(serializedModel, model, projectDir, config, debugEnabled);\n\n        StringReader sReader = new StringReader(serializedModel);\n\n        MavenStaxReader modelReader = new MavenStaxReader();\n        try {\n            model = new Model(modelReader.read(sReader));\n        } catch (XMLStreamException e) {\n            throw new ModelInterpolationException(\n                    \"Cannot read project model from interpolating filter of serialized version.\", e);\n        }\n\n        return model;\n    }\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/compat/maven-compat/src/main/java/org/apache/maven/project/interpolation/AbstractStringBasedModelInterpolator.java#L121-L157","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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/)","Sanitize property values that contain XML-illegal control characters","Update Maven and custom model-processing plugins; legacy string interpolation is deprecated in favor of field-wise interpolation in newer Maven lines","If embedding, prefer the non-legacy interpolators (StringSearchModelInterpolator via the standard builder) or avoid re-interpolating already-interpolated models"],"exampleFix":"// before: plugin injects raw control char into a property\nmodel.getProperties().put(\"banner\", \"start\\u0003end\");\n// after: strip non-XML characters before model processing\nString clean = raw.replaceAll(\"[\\\\x00-\\\\x08\\\\x0B\\\\x0C\\\\x0E-\\\\x1F]\", \"\");\nmodel.getProperties().put(\"banner\", clean);","handlingStrategy":"try-catch","validationCode":"// Reject XML-illegal control characters before feeding the model\nstatic String xmlSafe(String s) { return s == null ? null : s.replaceAll(\"[\\\\x00-\\\\x08\\\\x0B\\\\x0C\\\\x0E-\\\\x1F]\", \"\"); }","typeGuard":null,"tryCatchPattern":"try {\n    model = interpolator.interpolate(model, projectDir, config, debug);\n} catch (ModelInterpolationException e) {\n    // serialization failed: inspect recently mutated model fields, sanitize and retry\n}","preventionTips":["Never inject raw control characters into Model properties from plugins","Prefer modern field-wise interpolation over the legacy string-based one","Keep model mutations to dedicated phases to ease diagnosis"],"tags":["maven","interpolation","xml","stax","model"],"backgroundTag":"xml-serialization-failed","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}