From b8f59cf6db1bddf2f65cbb8d340fa3784978c109 Mon Sep 17 00:00:00 2001 From: Tine Kondo Date: Sun, 3 Jul 2022 07:14:32 +0000 Subject: [PATCH] fix(common): improve the checking/adding of a maven plugin in `pom.xml` --- packages/common/src/lib/core/jvm/maven-utils.ts | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/packages/common/src/lib/core/jvm/maven-utils.ts b/packages/common/src/lib/core/jvm/maven-utils.ts index 3442df52..b4d9246a 100644 --- a/packages/common/src/lib/core/jvm/maven-utils.ts +++ b/packages/common/src/lib/core/jvm/maven-utils.ts @@ -9,24 +9,19 @@ export function hasMavenPlugin(tree: Tree, rootFolder: string, groupId: string, const pomXmlStr = tree.read(`${rootFolder}/pom.xml`, 'utf-8'); const pomXml = readXml(pomXmlStr); - const pluginGrouIdNode = findXmlMatching(pomXml, `/project/build/plugins/plugin/groupId/text()[.="${groupId}"]`); - const pluginArtifactIdNode = findXmlMatching(pomXml, `/project/build/plugins/plugin/artifactId/text()[.="${artifactId}"]`); - if (pluginGrouIdNode && pluginArtifactIdNode) { - const pluginVersionNode = findXmlMatching(pomXml, `/project/build/plugins/plugin/version/text()[.="${version}"]`); - - return version ? !!pluginVersionNode : true; + let pluginXPath = `/project/build/plugins/plugin/groupId/text()[.="${groupId}"]/../../artifactId/text()[.="${artifactId}"]`; + if (version) { + pluginXPath += `/../../version/text()[.="${version}"]`; } - return false; + return !! findXmlMatching(pomXml, pluginXPath)?.up()?.up(); } export function addMavenPlugin(tree: Tree, rootFolder: string, groupId: string, artifactId: string, version?: string, configuration?: { [key: string]: any } | string): boolean { const pomXmlStr = tree.read(`${rootFolder}/pom.xml`, 'utf-8'); const pomXml = readXml(pomXmlStr); - const pluginGrouIdNode = findXmlMatching(pomXml, `/project/build/plugins/plugin/groupId/text()[.="${groupId}"]`); - const pluginArtifactIdNode = findXmlMatching(pomXml, `/project/build/plugins/plugin/artifactId/text()[.="${artifactId}"]`); - if (pluginGrouIdNode && pluginArtifactIdNode) { + if (hasMavenPlugin(tree, rootFolder, groupId, artifactId, version)) { return false;// plugin already exists }