-
Notifications
You must be signed in to change notification settings - Fork 3
POM parsing
The PomParser class is used to parse POM (Project Object Model) files and retrieve dependency information. Follow these steps to use the PomParser in your project:
-
Imports:
Import the necessary classes or methods from the
dependency-havenlibrary in your code.import eup.dependency.haven.model.Coordinates; import eup.dependency.haven.model.Dependency; import eup.dependency.haven.model.Exclusion; import eup.dependency.haven.model.Pom; import eup.dependency.haven.repository.Repository;
-
Create an Instance of
PomParser:You can create an instance of the
PomParser. You can optionally pass aRepositoryas a parameter if you have one. Here's how to create an instance:PomParser parser = new PomParser();
or with a
Repository:Repository repository = ... // new RepositoryImplementation(); PomParser parser = new PomParser(repository);
-
Parse a POM File:
To parse a POM file, provide an
InputStreamcontaining the POM XML as input. The parser will return aPomobject that contains the parsed information. Here's how to parse a POM file:InputStream inputStream = ... // Your POM XML InputStream Pom parsedPom = parser.parse(inputStream);
Replace
...with the actualInputStreamcontaining your POM XML. -
Access Parsed Information:
You can access various pieces of information from the parsed POM, such as coordinates, dependencies, exclusions, and managed dependencies.
-
To get the coordinates of the project (group ID, artifact ID, version, packaging):
Coordinates coordinates = parsedPom.getCoordinates();
-
To get the list of dependencies in the project:
List<Dependency> dependencies = parsedPom.getDependencies();
-
To get the list of exclusions in the project:
List<Exclusion> exclusions = parsedPom.getExclusions();
-
To get the list of managed dependencies (from the
dependencyManagementsection):List<Dependency> managedDependencies = parsedPom.getManagedDependencies();
-
-
Additional Features:
The
PomParserclass also supports features such as resolving properties defined in the POM, handling variable references, and more. Please refer to the class implementation for more details.
Here's an example of how to use the PomParser class to parse a POM file and access its information:
PomParser parser = new PomParser();
InputStream inputStream = ... // Your POM XML InputStream
try {
Pom parsedPom = parser.parse(inputStream);
if (parsedPom != null) {
Coordinates coordinates = parsedPom.getCoordinates();
List<Dependency> dependencies = parsedPom.getDependencies();
List<Exclusion> exclusions = parsedPom.getExclusions();
List<Dependency> managedDependencies = parsedPom.getManagedDependencies();
System.out.println("Coordinates: " + coordinates);
System.out.println("Dependencies: " + dependencies);
System.out.println("Exclusions: " + exclusions);
System.out.println("Managed Dependencies: " + managedDependencies);
} else {
System.out.println("Failed to parse the POM.");
}
} catch (IOException e) {
System.err.println("Error parsing the POM: " + e.getMessage());
}Replace ... with your actual InputStream containing the POM XML.
With these instructions and the example, you can use the PomParser to parse POM files and access their content in your project.