{"record":{"id":"c77a499a12a7c07c","repo":"apache/maven","slug":"unable-to-write-model","errorCode":null,"errorMessage":"Unable to write model: ","messagePattern":"Unable to write model: ","errorType":"exception","errorClass":"XmlWriterException","httpStatus":null,"severity":"error","filePath":"impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java","lineNumber":186,"sourceCode":"\n            Function<Object, String> formatter = request.getInputLocationFormatter();\n            if (formatter != null) {\n                xmlWriter.setAddLocationInformation(true);\n                Function<InputLocation, String> adapter = formatter::apply;\n                xmlWriter.setStringFormatter(adapter);\n            }\n\n            if (writer != null) {\n                xmlWriter.write(writer, content);\n            } else if (outputStream != null) {\n                xmlWriter.write(outputStream, content);\n            } else {\n                try (OutputStream os = Files.newOutputStream(path)) {\n                    xmlWriter.write(os, content);\n                }\n            }\n        } catch (Exception e) {\n            throw new XmlWriterException(\"Unable to write model: \" + getMessage(e), getLocation(e), e);\n        }\n    }\n\n    static class InputFactoryHolder {\n        static final XMLInputFactory XML_INPUT_FACTORY;\n\n        static {\n            XMLInputFactory factory = XmlService.newXMLInputFactory();\n            factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);\n            factory.setProperty(XMLInputFactory.IS_COALESCING, true);\n            XML_INPUT_FACTORY = factory;\n        }\n    }\n\n    /**\n     * Extracts the modelId (groupId:artifactId:version) from a POM XML stream\n     * by parsing just enough XML to get the GAV coordinates.\n     *","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/apache/maven/blob/e4093d4e120eac99d6bdce5ba67cace2f3085c97/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java#L168-L204","documentation":"Wrapper exception thrown by DefaultModelXmlFactory.write when serializing a Model fails for any reason: STaX writer errors (XMLStreamException), IOException while opening or writing the target path, or errors from the configured InputLocationFormatter. The original exception stays attached as the cause, and its message and location are appended to the wrapper text.","triggerScenarios":"Writing to a Path whose parent directory does not exist or is not writable; XMLStreamWriter failures from the STaX implementation; a Model whose content cannot be serialized; an InputLocationFormatter that throws while producing location strings.","commonSituations":"Writing pom.xml into a directory that was never created; read-only checkouts in CI; a classpath without a working STaX implementation; custom location formatters that assume non-null InputLocation.","solutions":["Inspect getCause() on the XmlWriterException - it is almost always the real IOException or XMLStreamException","For path targets, run Files.createDirectories(path.getParent()) before the write and verify the directory is writable","If an InputLocationFormatter is set, test it separately; a throwing formatter surfaces here too","Verify a STaX implementation (JDK built-in or Woodstox) is on the classpath"],"exampleFix":"// before\nmodelXmlFactory.write(XmlWriterRequest.builder()\n    .content(model)\n    .path(target)\n    .build()); // NoSuchFileException if parent dir missing\n\n// after\nFiles.createDirectories(target.getParent());\nmodelXmlFactory.write(XmlWriterRequest.builder()\n    .content(model)\n    .path(target)\n    .build());","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    modelXmlFactory.write(req);\n} catch (XmlWriterException e) {\n    Throwable cause = e.getCause();\n    if (cause instanceof IOException io) {\n        // target not writable, missing directory, disk full\n    } else if (cause instanceof XMLStreamException xs) {\n        // content not serializable as XML\n    }\n}","preventionTips":["Run Files.createDirectories on the parent and check isWritable before path-based writes","Never log only the wrapper message; unwrap and log the cause","Test location formatters in isolation when using inputLocationFormatter"],"tags":["maven","xml","model","io","serialization"],"backgroundTag":"xml-write-failed","analyzedSha":"e4093d4e120eac99d6bdce5ba67cace2f3085c97","analyzedAt":"2026-08-21T22:58:24.034Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}