apache/maven · error · IllegalArgumentException

writer, outputStream or path must be non null

Error message

writer, outputStream or path must be non null

What it means

Thrown by DefaultModelXmlFactory.write(XmlWriterRequest<Model>) when the request carries no output target. The method accepts a Writer, an OutputStream or a Path and checks all three; if writer, outputStream and path are all null it throws IllegalArgumentException before any serialization starts. This is a caller-contract violation, not an environmental problem.

Source

Thrown at impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java:162

                try (InputStream is = url.openStream()) {
                    return xml.read(is, request.isStrict(), source);
                }
            }
        } catch (Exception e) {
            throw new XmlReaderException("Unable to read model: " + getMessage(e), getLocation(e), e);
        }
    }

    @Override
    public void write(XmlWriterRequest<Model> request) throws XmlWriterException {
        requireNonNull(request, "request");
        Model content = requireNonNull(request.getContent(), "content");
        Path path = request.getPath();
        OutputStream outputStream = request.getOutputStream();
        Writer writer = request.getWriter();

        if (writer == null && outputStream == null && path == null) {
            throw new IllegalArgumentException("writer, outputStream or path must be non null");
        }

        try {
            MavenStaxWriter xmlWriter = new MavenStaxWriter();
            xmlWriter.setAddLocationInformation(false);

            Function<Object, String> formatter = request.getInputLocationFormatter();
            if (formatter != null) {
                xmlWriter.setAddLocationInformation(true);
                Function<InputLocation, String> adapter = formatter::apply;
                xmlWriter.setStringFormatter(adapter);
            }

            if (writer != null) {
                xmlWriter.write(writer, content);
            } else if (outputStream != null) {
                xmlWriter.write(outputStream, content);
            } else {

View on GitHub (pinned to e4093d4e12)

Solutions

  1. Set exactly one output target on the request: .path(Paths.get("pom.xml")), .outputStream(os) or .writer(new StringWriter())
  2. If the target is dynamic, verify at least one of getWriter(), getOutputStream(), getPath() is non-null before calling write
  3. Fail fast at request-build time in your own code so the error carries your context, not the factory's generic message

Example fix

// before
XmlWriterRequest<Model> req = XmlWriterRequest.builder()
    .content(model)
    .build();
modelXmlFactory.write(req); // throws: no output target

// after
XmlWriterRequest<Model> req = XmlWriterRequest.builder()
    .content(model)
    .path(Paths.get("pom.xml"))
    .build();
modelXmlFactory.write(req);
Defensive patterns

Strategy: validation

Validate before calling

XmlWriterRequest<Model> req = XmlWriterRequest.builder()
    .content(model)
    .build();
if (req.getWriter() == null && req.getOutputStream() == null && req.getPath() == null) {
    throw new IllegalStateException("model write request has no output target");
}
modelXmlFactory.write(req);

Prevention

When it happens

Trigger: Building a XmlWriterRequest for a Model without calling .writer(...), .outputStream(...) or .path(...), then passing it to ModelXmlFactory.write (or ModelProcessor.write). Also occurs when the target is set conditionally and that branch is not taken at runtime.

Common situations: Refactoring write code that previously used a hard-coded file; copy-pasting a read-side request (modelId/location only) and forgetting the output target; builder chains where the target comes from a variable that is null.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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