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
    • addDependency

      public boolean addDependency(String groupId, String artifactId, String version, String scope)
      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
      scope - the scope of the dependency, e.g. test, or null for the default scope
      Returns:
      true if the dependency was added, false if it already existed
    • hasParent

      public boolean hasParent(String groupId, String artifactId)
      Checks whether the pom declares the given artifact as its parent.
      Parameters:
      groupId - the expected parent groupId, e.g. org.springframework.boot
      artifactId - the expected parent artifactId, e.g. spring-boot-starter-parent
      Returns:
      true when the parent matches, false otherwise
    • hasPluginInProfile

      public boolean hasPluginInProfile(String profileId, String artifactId)
      Checks whether the build of the given profile already declares the plugin.
      Parameters:
      profileId - the id of the profile, e.g. it
      artifactId - the artifactId of the plugin
      Returns:
      true when the plugin is present, false otherwise
    • addPluginToProfileBuild

      public boolean addPluginToProfileBuild(String profileId, String artifactId, String pluginXml)
      Adds a plugin to the build of the given profile, creating the profiles, profile, build and plugins elements that are missing.

      Does nothing when the profile build already declares a plugin with the same artifactId.

      Parameters:
      profileId - the id of the profile, e.g. it
      artifactId - the artifactId of the plugin, used to detect an existing entry
      pluginXml - the full <plugin> element to add, indented relative to column zero
      Returns:
      true if the plugin was added, false if it already existed
    • addPluginsToProfileBuild

      public boolean addPluginsToProfileBuild(String profileId, Map<String,String> pluginsByArtifactId)
      Adds several plugins to the build of the given profile in one go, creating the profiles, profile, build and plugins elements that are missing.

      Plugins whose artifactId is already declared in the profile build are skipped. All added plugins share the elements created for them, so this must be preferred over repeated addPluginToProfileBuild(java.lang.String, java.lang.String, java.lang.String) calls on the same rewriter: queued changes are not visible to subsequent lookups, so separate calls would each create their own <profiles> section.

      Parameters:
      profileId - the id of the profile, e.g. it
      pluginsByArtifactId - the plugins to add, keyed by artifactId and mapped to the full <plugin> element indented relative to column zero. Iteration order is preserved.
      Returns:
      true if at least one plugin was added, false if all of them already existed