Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

POM parsing

EUP edited this page Nov 4, 2023 · 3 revisions

PomParser - POM File 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:

  1. Imports:

    Import the necessary classes or methods from the dependency-haven library 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;
  2. Create an Instance of PomParser:

    You can create an instance of the PomParser. You can optionally pass a Repository as 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);
  3. Parse a POM File:

    To parse a POM file, provide an InputStream containing the POM XML as input. The parser will return a Pom object 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 actual InputStream containing your POM XML.

  4. 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 dependencyManagement section):

      List<Dependency> managedDependencies = parsedPom.getManagedDependencies();
  5. Additional Features:

    The PomParser class 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.

Example

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.

Clone this wiki locally