Class PomFileRewriter

java.lang.Object
com.vaadin.copilot.PomFileRewriter

public class PomFileRewriter extends Object
Pom File rewriter handles modifications for the given pom file by copying original file to temporary file. When save() method is called, original file would be replaced with temporary file.

Rewriter file takes two steps. SAX parser traverses through all elements to gather line numbers, and they are used to replace nodes using String manipulation to keep file formatting preserved

  • Constructor Details

    • PomFileRewriter

      public PomFileRewriter(Path file) throws IOException, SAXException
      Creates a rewriter for the given pom.xml file.
      Parameters:
      file - the pom.xml file to be modified
      Throws:
      IOException - if the file cannot be read
      SAXException - if a parsing error occurred
  • Method Details

    • findDependencyByGroupIdAndArtifactId

      public PomFileRewriter.Dependency findDependencyByGroupIdAndArtifactId(String groupId, String artifactId)
      Finds dependency under both dependencies and dependencyManagement tags by groupId and artifactId
      Parameters:
      groupId - Group ID to look for e.g. com.vaadin
      artifactId - Artifact ID to look for e.g. flow-server
      Returns:
      Dependency if found, null otherwise
    • findDependency

      public Optional<PomFileRewriter.Dependency> findDependency(String groupId, String artifactId)
      Finds the dependency with the given groupId and artifactId. Does not try to resolve the version unless it is part of the same <dependency> tag.
      Parameters:
      groupId - Group ID to look for e.g. com.vaadin
      artifactId - Artifact ID to look for e.g. flow-server
      Returns:
      An Optional containing the dependency if found, or an empty Optional if not found.
    • hasRepositoryByIdAndUrl

      public boolean hasRepositoryByIdAndUrl(String id, String url)
      Checks repository exists in repositories tag by id and url
      Parameters:
      id - id of the repository
      url - url of the repository
      Returns:
      true if found, false otherwise.
    • hasRepositoryById

      public boolean hasRepositoryById(String id)
      Checks repository exists in repositories tag by id
      Parameters:
      id - id of the repository
      Returns:
      true if found, false otherwise.
    • hasRepositoryByUrl

      public boolean hasRepositoryByUrl(String url)
      Checks repository exists in repositories tag by url
      Parameters:
      url - url of the repository
      Returns:
      true if found, false otherwise.
    • getRepositoriesElement

      public Optional<Element> getRepositoriesElement()
      Checks repositories tag present in project as a child.
      Returns:
      Repository node if found, Optional.empty() otherwise.
    • addRepository

      public void addRepository(String id, String url)
      Adds repository to given pom.xml if it does not exist in the repositories. Repositories tag is added at the end of the file when it is not present.
      Parameters:
      id - id attribute of the repository
      url - url attribute of the repository
    • getPluginRepositoriesElement

      public Optional<Element> getPluginRepositoriesElement()
      Checks pluginRepositories tag present in project as a child.
      Returns:
      PluginRepository node if found, Optional.empty() otherwise.
    • addPluginRepository

      public void addPluginRepository(String id, String url)
      Adds pluginRepository to given pom.xml if it does not exist in the pluginRepositories. PluginRepositories tag is added at the end of the file when it is not present.
      Parameters:
      id - id attribute of the plugin repository
      url - url attribute of the plugin repository
    • hasPluginRepositoryByIdAndUrl

      public boolean hasPluginRepositoryByIdAndUrl(String id, String url)
      Checks repository exists in repositories tag by id and url
      Parameters:
      id - id of the repository
      url - url of the repository
      Returns:
      true if found, false otherwise.
    • hasPluginRepositoryById

      public boolean hasPluginRepositoryById(String id)
      Checks repository exists in repositories tag by id and url
      Parameters:
      id - id of the repository
      Returns:
      true if found, false otherwise.
    • hasPluginRepositoryByUrl

      public boolean hasPluginRepositoryByUrl(String url)
      Checks repository exists in repositories tag by id and url
      Parameters:
      url - url of the repository
      Returns:
      true if found, false otherwise.
    • hasParentPom

      public boolean hasParentPom()
      Checks if given pom file has parent tag.
      Returns:
      true when parent node is present under Project tag, false otherwise.
    • findPropertyByKey

      public PomFileRewriter.Property findPropertyByKey(String propertyKey)
      Checks given propertyKey is a property under properties tag.
      Parameters:
      propertyKey - Property key to search for. It should be exactly the same as it is in pom.xml
      Returns:
      Property when found, null otherwise.
    • save

      public void save() throws IOException
      Apply all modifications to a temporary file and then replaces it with the real one.
      Throws:
      IOException - when a file operation fails.
    • updateDependencyVersion

      public void updateDependencyVersion(PomFileRewriter.Dependency dependency, String newVersion)
      Saves replacement for version tag of given dependency.
      Parameters:
      dependency - dependency that will be updated
      newVersion - new version for the dependency
    • updateProperty

      public void updateProperty(PomFileRewriter.Property property, String newValue)
      Saves replacement for the text content of given property
      Parameters:
      property - property that will be updated
      newValue - new text content of the given property
    • addDependency

      public boolean addDependency(String groupId, String artifactId, String version)
      Adds dependency to the pom.xml file, if needed.

      Returns true if the dependency was added, false if it already existed.

      Parameters:
      groupId - the groupId of the dependency
      artifactId - the artifactId of the dependency
      version - the version of the dependency or null if not specified
      Returns:
      true if the dependency was added, false if it already existed