Skip to content

Commit 1bce2aa

Browse files
committed
bob addresses redundancy
1 parent f2e1138 commit 1bce2aa

1 file changed

Lines changed: 37 additions & 55 deletions

File tree

src/util/mavenUtil.ts

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as semver from "semver";
1111
* Look for a valid parent pom.xml
1212
* A valid parent contains the liberty-maven-plguin in the plugin management section
1313
* Return BuildFile object
14-
*
14+
*
1515
* @param xmlString the xmlString version of the pom.xml
1616
*/
1717
export function validParentPom(xmlString: string): BuildFileImpl {
@@ -24,35 +24,39 @@ export function validParentPom(xmlString: string): BuildFileImpl {
2424
for (let i = 0; i < result.project.build.length; i++) {
2525
const pluginManagement = result.project.build[i].pluginManagement;
2626
if (pluginManagement !== undefined) {
27-
const plugins = pluginManagement[i].plugins;
27+
// Iterate over all pluginManagement entries using j, not i (build index) -
28+
// pluginManagement is its own array and must be indexed independently.
29+
for (let j = 0; j < pluginManagement.length; j++) {
30+
const plugins = pluginManagement[j].plugins;
2831
if (plugins !== undefined) {
29-
for (let j = 0; j < plugins.length; j++) {
30-
const plugin = plugins[j].plugin;
32+
for (let k = 0; k < plugins.length; k++) {
33+
const plugin = plugins[k].plugin;
3134
if (plugin !== undefined) {
32-
for (let k = 0; k < plugin.length; k++) {
33-
if (plugin[k].artifactId[0] === "liberty-maven-plugin" && plugin[k].groupId[0] === "io.openliberty.tools") {
35+
for (let m = 0; m < plugin.length; m++) {
36+
if (plugin[m].artifactId[0] === "liberty-maven-plugin" && plugin[m].groupId[0] === "io.openliberty.tools") {
3437
console.debug("Found liberty-maven-plugin in the pom.xml plugin management");
35-
if (containerVersion(plugin[k])) {
38+
if (containerVersion(plugin[m])) {
3639
parentPom = new BuildFileImpl(true, LIBERTY_MAVEN_PROJECT_CONTAINER);
3740
return;
3841
} else {
3942
parentPom = new BuildFileImpl(true, LIBERTY_MAVEN_PROJECT);
4043
return;
4144
}
4245
}
43-
if (plugin[k].artifactId[0] === "boost-maven-plugin" && plugin[k].groupId[0] === "org.microshed.boost") {
46+
if (plugin[m].artifactId[0] === "boost-maven-plugin" && plugin[m].groupId[0] === "org.microshed.boost") {
4447
console.debug("Found boost-maven-plugin in the pom.xml");
4548
parentPom = new BuildFileImpl(true, LIBERTY_MAVEN_PROJECT);
4649
return;
4750
}
48-
}
51+
}
4952
}
5053
}
5154
}
55+
} // end pluginManagement j loop
5256
}
5357
}
5458
}
55-
59+
5660
// If no Liberty plugin found, check if this is an aggregator with modules
5761
// This allows intermediate aggregators to appear in the hierarchy
5862
if (!parentPom.isValidBuildFile() && result.project.modules !== undefined) {
@@ -79,7 +83,7 @@ export function validParentPom(xmlString: string): BuildFileImpl {
7983
* pom.xml may either match a child pom artifactId, contain the plugin in the profiles section
8084
* or define the plugin in the build section
8185
* Return BuildFile object
82-
*
86+
*
8387
* @param xmlString string representation of the pom.xml
8488
* @param childrenMap map of all the children pom.xml identified
8589
*/
@@ -89,7 +93,7 @@ export function validPom(xmlString: string, childrenMap: Map<string, string[]>):
8993
parseString(xmlString, (err: any, result: any) => {
9094

9195
// check if the artifactId matches one of the modules found in a parent pom
92-
if (result.project.arifactId !== undefined && result.project.artifactId[0] !== undefined
96+
if (result.project.artifactId !== undefined && result.project.artifactId[0] !== undefined
9397
&& result.project.parent !== undefined && result.project.parent[0].artifactId !== undefined) {
9498
if (childrenMap.has(result.project.parent[0].artifactId[0])) {
9599
const modules = childrenMap.get(result.project.parent[0].artifactId[0]);
@@ -98,7 +102,7 @@ export function validPom(xmlString: string, childrenMap: Map<string, string[]>):
98102
if (module === result.project.artifactId[0]) {
99103
// TODO: add ability to detect version of LMP once multi-module project scenarios are defined
100104
// @see https://github.com/OpenLiberty/open-liberty-tools-vscode/issues/61
101-
// @see https://github.com/OpenLiberty/open-liberty-tools-vscode/issues/26
105+
// @see https://github.com/OpenLiberty/open-liberty-tools-vscode/issues/26
102106
mavenPOM = new BuildFileImpl(true, LIBERTY_MAVEN_PROJECT);
103107
return;
104108
}
@@ -141,7 +145,7 @@ export function validPom(xmlString: string, childrenMap: Map<string, string[]>):
141145
/**
142146
* Check the build portion of a pom.xml for the liberty-maven-plugin
143147
* Return BuildFile object
144-
*
148+
*
145149
* @param build JS object of the build section in a pom.xml
146150
*/
147151
export function mavenPluginDetected(build: Array<{ plugins: Array<{ plugin: any }> }> | undefined): BuildFileImpl {
@@ -211,8 +215,8 @@ export function findChildMavenModules(xmlString: string): Map<string, string[]>
211215

212216
/**
213217
* Return true if the liberty-maven-plugin version is compatible
214-
* for dev mode with containers
215-
*
218+
* for dev mode with containers
219+
*
216220
* @param plugin JS object for liberty-maven-plugin
217221
*/
218222
function containerVersion(plugin: any): boolean {
@@ -252,11 +256,11 @@ export interface MavenProjectMetadata {
252256
export async function extractMavenMetadata(pomPath: string, xmlString?: string): Promise<MavenProjectMetadata> {
253257
const fse = require("fs-extra");
254258
const xml = xmlString || await fse.readFile(pomPath, "utf8");
255-
259+
256260
const metadata = parsePomXml(xml);
257261
metadata.buildFilePath = pomPath;
258262
metadata.xmlString = xml;
259-
263+
260264
return metadata;
261265
}
262266

@@ -276,40 +280,40 @@ function parsePomXml(xmlString: string): MavenProjectMetadata {
276280
buildFilePath: "",
277281
contextValue: LIBERTY_MAVEN_PROJECT
278282
};
279-
283+
280284
parseString(xmlString, (err: any, result: any) => {
281285
if (err) {
282286
console.error(localize("error.parsing.pom", "Error parsing the pom " + err, err));
283287
return;
284288
}
285-
289+
286290
// Extract artifactId
287291
if (result.project.artifactId && result.project.artifactId[0] !== undefined) {
288292
metadata.artifactId = result.project.artifactId[0];
289293
}
290-
294+
291295
// Extract parent artifactId
292296
if (result.project.parent && result.project.parent[0].artifactId) {
293297
metadata.parentArtifactId = result.project.parent[0].artifactId[0];
294298
}
295-
299+
296300
// Extract modules
297301
if (result.project.modules) {
298302
metadata.modules = extractModulesFromPom(result.project.modules);
299303
if (metadata.modules.length > 0) {
300304
metadata.isAggregator = true;
301305
}
302306
}
303-
307+
304308
// Check for packaging type "pom"
305309
if (result.project.packaging && result.project.packaging[0] === "pom") {
306310
metadata.isAggregator = true;
307311
}
308-
312+
309313
// Check for Liberty Maven plugin
310314
metadata.hasLibertyPlugin = checkForLibertyMavenPlugin(result);
311315
metadata.isLibertyEnabled = metadata.hasLibertyPlugin;
312-
316+
313317
// Set context value based on plugin detection
314318
if (metadata.hasLibertyPlugin) {
315319
const buildFile = mavenPluginDetected(result.project.build);
@@ -318,7 +322,7 @@ function parsePomXml(xmlString: string): MavenProjectMetadata {
318322
}
319323
}
320324
});
321-
325+
322326
return metadata;
323327
}
324328

@@ -329,7 +333,7 @@ function parsePomXml(xmlString: string): MavenProjectMetadata {
329333
*/
330334
function extractModulesFromPom(modules: any[]): string[] {
331335
const moduleNames: string[] = [];
332-
336+
333337
for (let i = 0; i < modules.length; i++) {
334338
const module = modules[i].module;
335339
if (module !== undefined) {
@@ -338,7 +342,7 @@ function extractModulesFromPom(modules: any[]): string[] {
338342
}
339343
}
340344
}
341-
345+
342346
return moduleNames;
343347
}
344348

@@ -348,15 +352,16 @@ function extractModulesFromPom(modules: any[]): string[] {
348352
* @returns true if Liberty plugin is found
349353
*/
350354
function checkForLibertyMavenPlugin(result: any): boolean {
351-
// Check in build section
355+
// Check in build section (covers <build><plugins> and <build><pluginManagement><plugins>).
356+
// mavenPluginDetected already traverses both - no need to handle pluginManagement separately.
352357
if (result.project.build !== undefined) {
353358
const buildFile = mavenPluginDetected(result.project.build);
354359
if (buildFile.isValidBuildFile()) {
355360
return true;
356361
}
357362
}
358-
359-
// Check in profiles
363+
364+
// Check in profiles - Liberty plugin may be declared inside a Maven profile.
360365
if (result.project.profiles !== undefined) {
361366
for (let i = 0; i < result.project.profiles.length; i++) {
362367
const profile = result.project.profiles[i].profile;
@@ -370,29 +375,6 @@ function checkForLibertyMavenPlugin(result: any): boolean {
370375
}
371376
}
372377
}
373-
374-
// Check in pluginManagement
375-
if (result.project.build !== undefined) {
376-
for (let i = 0; i < result.project.build.length; i++) {
377-
const pluginManagement = result.project.build[i].pluginManagement;
378-
if (pluginManagement !== undefined) {
379-
const plugins = pluginManagement[0].plugins;
380-
if (plugins !== undefined) {
381-
for (let j = 0; j < plugins.length; j++) {
382-
const plugin = plugins[j].plugin;
383-
if (plugin !== undefined) {
384-
for (let k = 0; k < plugin.length; k++) {
385-
if (plugin[k].artifactId[0] === "liberty-maven-plugin" &&
386-
plugin[k].groupId[0] === "io.openliberty.tools") {
387-
return true;
388-
}
389-
}
390-
}
391-
}
392-
}
393-
}
394-
}
395-
}
396-
378+
397379
return false;
398380
}

0 commit comments

Comments
 (0)