Class PomFileRewriter
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordDependency that presents in the pom.xmlstatic final recordProperty that presents in the pom.xml -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddDependency(String groupId, String artifactId, String version) Adds dependency to the pom.xml file, if needed.booleanaddDependency(String groupId, String artifactId, String version, String scope) Adds dependency to the pom.xml file, if needed.voidaddPluginRepository(String id, String url) Adds pluginRepository to given pom.xml if it does not exist in the pluginRepositories.booleanaddPluginsToProfileBuild(String profileId, Map<String, String> pluginsByArtifactId) Adds several plugins to the build of the given profile in one go, creating theprofiles,profile,buildandpluginselements that are missing.booleanaddPluginToProfileBuild(String profileId, String artifactId, String pluginXml) Adds a plugin to the build of the given profile, creating theprofiles,profile,buildandpluginselements that are missing.voidaddRepository(String id, String url) Adds repository to given pom.xml if it does not exist in the repositories.findDependency(String groupId, String artifactId) Finds the dependency with the given groupId and artifactId.findDependencyByGroupIdAndArtifactId(String groupId, String artifactId) Finds dependency under both dependencies and dependencyManagement tags by groupId and artifactIdfindPropertyByKey(String propertyKey) Checks givenpropertyKeyis a property under properties tag.Checks pluginRepositories tag present in project as a child.Checks repositories tag present in project as a child.booleanChecks whether the pom declares the given artifact as its parent.booleanChecks if given pom file hasparenttag.booleanhasPluginInProfile(String profileId, String artifactId) Checks whether the build of the given profile already declares the plugin.booleanChecks repository exists in repositories tag by id and urlbooleanhasPluginRepositoryByIdAndUrl(String id, String url) Checks repository exists in repositories tag by id and urlbooleanChecks repository exists in repositories tag by id and urlbooleanChecks repository exists in repositories tag by idbooleanhasRepositoryByIdAndUrl(String id, String url) Checks repository exists in repositories tag by id and urlbooleanhasRepositoryByUrl(String url) Checks repository exists in repositories tag by urlvoidsave()Apply all modifications to a temporary file and then replaces it with the real one.voidupdateDependencyVersion(PomFileRewriter.Dependency dependency, String newVersion) Saves replacement for version tag of given dependency.voidupdateProperty(PomFileRewriter.Property property, String newValue) Saves replacement for the text content of given property
-
Constructor Details
-
PomFileRewriter
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 readSAXException- 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.vaadinartifactId- Artifact ID to look for e.g. flow-server- Returns:
- Dependency if found,
nullotherwise
-
findDependency
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. -
hasRepositoryByIdAndUrl
Checks repository exists in repositories tag by id and url- Parameters:
id- id of the repositoryurl- url of the repository- Returns:
trueif found,falseotherwise.
-
hasRepositoryById
Checks repository exists in repositories tag by id- Parameters:
id- id of the repository- Returns:
trueif found,falseotherwise.
-
hasRepositoryByUrl
Checks repository exists in repositories tag by url- Parameters:
url- url of the repository- Returns:
trueif found,falseotherwise.
-
getRepositoriesElement
Checks repositories tag present in project as a child.- Returns:
- Repository node if found,
Optional.empty()otherwise.
-
addRepository
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 repositoryurl- url attribute of the repository
-
getPluginRepositoriesElement
Checks pluginRepositories tag present in project as a child.- Returns:
- PluginRepository node if found,
Optional.empty()otherwise.
-
addPluginRepository
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 repositoryurl- url attribute of the plugin repository
-
hasPluginRepositoryByIdAndUrl
Checks repository exists in repositories tag by id and url- Parameters:
id- id of the repositoryurl- url of the repository- Returns:
trueif found,falseotherwise.
-
hasPluginRepositoryById
Checks repository exists in repositories tag by id and url- Parameters:
id- id of the repository- Returns:
trueif found,falseotherwise.
-
hasPluginRepositoryByUrl
Checks repository exists in repositories tag by id and url- Parameters:
url- url of the repository- Returns:
trueif found,falseotherwise.
-
hasParentPom
public boolean hasParentPom()Checks if given pom file hasparenttag.- Returns:
truewhen parent node is present under Project tag,falseotherwise.
-
findPropertyByKey
Checks givenpropertyKeyis 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,
nullotherwise.
-
save
Apply all modifications to a temporary file and then replaces it with the real one.- Throws:
IOException- when a file operation fails.
-
updateDependencyVersion
Saves replacement for version tag of given dependency.- Parameters:
dependency- dependency that will be updatednewVersion- new version for the dependency
-
updateProperty
Saves replacement for the text content of given property- Parameters:
property- property that will be updatednewValue- new text content of the given property
-
addDependency
Adds dependency to the pom.xml file, if needed.Returns true if the dependency was added,
falseif it already existed.- Parameters:
groupId- the groupId of the dependencyartifactId- the artifactId of the dependencyversion- the version of the dependency ornullif not specified- Returns:
trueif the dependency was added,falseif it already existed
-
addDependency
Adds dependency to the pom.xml file, if needed.Returns true if the dependency was added,
falseif it already existed.- Parameters:
groupId- the groupId of the dependencyartifactId- the artifactId of the dependencyversion- the version of the dependency ornullif not specifiedscope- the scope of the dependency, e.g.test, ornullfor the default scope- Returns:
trueif the dependency was added,falseif it already existed
-
hasParent
Checks whether the pom declares the given artifact as its parent.- Parameters:
groupId- the expected parent groupId, e.g.org.springframework.bootartifactId- the expected parent artifactId, e.g.spring-boot-starter-parent- Returns:
truewhen the parent matches,falseotherwise
-
hasPluginInProfile
Checks whether the build of the given profile already declares the plugin.- Parameters:
profileId- the id of the profile, e.g.itartifactId- the artifactId of the plugin- Returns:
truewhen the plugin is present,falseotherwise
-
addPluginToProfileBuild
Adds a plugin to the build of the given profile, creating theprofiles,profile,buildandpluginselements 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.itartifactId- the artifactId of the plugin, used to detect an existing entrypluginXml- the full<plugin>element to add, indented relative to column zero- Returns:
trueif the plugin was added,falseif it already existed
-
addPluginsToProfileBuild
Adds several plugins to the build of the given profile in one go, creating theprofiles,profile,buildandpluginselements 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.itpluginsByArtifactId- the plugins to add, keyed by artifactId and mapped to the full<plugin>element indented relative to column zero. Iteration order is preserved.- Returns:
trueif at least one plugin was added,falseif all of them already existed
-