Model for Maven POM (Project Object Model)
—
XML readers and writers for serializing models to and from POM files with location tracking and formatting preservation. These classes handle the conversion between Maven Model objects and XML POM files.
Standard XML reader for parsing POM files into Model objects using XPP3 parser.
public class MavenXpp3Reader {
// Basic reading methods
public Model read(Reader reader) throws IOException, XmlPullParserException;
public Model read(InputStream in) throws IOException, XmlPullParserException;
public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException;
public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException;
// Advanced methods with encoding
public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException;
public Model read(InputStream in, boolean strict, String encoding, InputSource source) throws IOException, XmlPullParserException;
}Usage Example:
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import java.io.FileReader;
import java.io.IOException;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
MavenXpp3Reader reader = new MavenXpp3Reader();
try {
// Read from file
Model model = reader.read(new FileReader("pom.xml"));
// Read with strict parsing disabled (allows unknown elements)
Model model2 = reader.read(new FileReader("pom.xml"), false);
System.out.println("Project: " + model.getArtifactId());
System.out.println("Version: " + model.getVersion());
} catch (IOException | XmlPullParserException e) {
System.err.println("Error reading POM: " + e.getMessage());
}Extended XML reader with location tracking support for error reporting and IDE integration.
public class MavenXpp3ReaderEx {
// Reading methods with location tracking
public Model read(Reader reader) throws IOException, XmlPullParserException;
public Model read(InputStream in) throws IOException, XmlPullParserException;
public Model read(Reader reader, boolean strict) throws IOException, XmlPullParserException;
public Model read(InputStream in, boolean strict) throws IOException, XmlPullParserException;
// Advanced methods with encoding and source tracking
public Model read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException;
public Model read(InputStream in, boolean strict, String encoding, InputSource source) throws IOException, XmlPullParserException;
}Usage Example:
import org.apache.maven.model.Model;
import org.apache.maven.model.InputLocation;
import org.apache.maven.model.io.xpp3.MavenXpp3ReaderEx;
MavenXpp3ReaderEx readerEx = new MavenXpp3ReaderEx();
try {
Model model = readerEx.read(new FileReader("pom.xml"), true);
// Access location information for error reporting
InputLocation location = model.getLocation("version");
if (location != null) {
System.out.println("Version defined at line: " + location.getLineNumber());
}
} catch (Exception e) {
System.err.println("Parse error: " + e.getMessage());
}Standard XML writer for serializing Model objects to POM XML format.
public class MavenXpp3Writer {
// Basic writing methods
public void write(Writer writer, Model model) throws IOException;
public void write(OutputStream stream, Model model) throws IOException;
// Methods with encoding control
public void write(Writer writer, Model model, String encoding) throws IOException;
public void write(OutputStream stream, Model model, String encoding) throws IOException;
}Usage Example:
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import java.io.FileWriter;
import java.io.IOException;
// Create or modify model
Model model = new Model();
model.setModelVersion("4.0.0");
model.setGroupId("com.example");
model.setArtifactId("my-project");
model.setVersion("1.0.0");
MavenXpp3Writer writer = new MavenXpp3Writer();
try {
// Write to file
writer.write(new FileWriter("output-pom.xml"), model);
// Write to file with specific encoding
writer.write(new FileWriter("utf8-pom.xml"), model, "UTF-8");
System.out.println("POM file written successfully");
} catch (IOException e) {
System.err.println("Error writing POM: " + e.getMessage());
}Extended XML writer with advanced formatting options and comment preservation.
public class MavenXpp3WriterEx {
// Writing methods with extended formatting
public void write(Writer writer, Model model) throws IOException;
public void write(OutputStream stream, Model model) throws IOException;
// Methods with encoding and formatting control
public void write(Writer writer, Model model, String encoding) throws IOException;
public void write(OutputStream stream, Model model, String encoding) throws IOException;
}Key Features:
All I/O operations can throw these exceptions:
// XML parsing errors
catch (XmlPullParserException e) {
System.err.println("XML syntax error: " + e.getMessage());
System.err.println("Line: " + e.getLineNumber() + ", Column: " + e.getColumnNumber());
}
// File I/O errors
catch (IOException e) {
System.err.println("File I/O error: " + e.getMessage());
}Used by extended readers for tracking source information:
public class InputSource {
public String getLocation();
public String getModelId();
}
public class InputLocation {
public int getLineNumber();
public int getColumnNumber();
public InputSource getSource();
public Map<Object, InputLocation> getLocations();
}Usage for IDE Integration:
// Create input source for tracking
InputSource source = new InputSource();
source.setLocation("pom.xml");
source.setModelId("com.example:my-project:1.0.0");
// Read with source tracking
Model model = readerEx.read(new FileReader("pom.xml"), true, source);
// Later, get location of any element for error reporting or IDE navigation
InputLocation versionLoc = model.getLocation("version");
if (versionLoc != null) {
int line = versionLoc.getLineNumber();
int column = versionLoc.getColumnNumber();
String file = versionLoc.getSource().getLocation();
// Use for IDE "Go to Definition" or error highlighting
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-maven--maven-model