Package com.vaadin.copilot
Class PomFileRewriter
java.lang.Object
com.vaadin.copilot.PomFileRewriter
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
-
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.voidaddPluginRepository(String id, String url) Adds pluginRepository to given pom.xml if it does not exist in the pluginRepositories.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 if given pom file hasparenttag.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
-