From 01e60902302939de6067044d51cbb0276a25d62d Mon Sep 17 00:00:00 2001 From: Cheryl King Date: Wed, 12 Jul 2023 08:31:11 -0500 Subject: [PATCH] Add prop resolution for config parameters --- .../it/server-param-pom-override-it/pom.xml | 5 +++ .../wlp/maven/test/app/PluginConfigXmlIT.java | 29 ++++++++++++++-- .../maven/server/PluginConfigSupport.java | 10 ++++-- .../maven/server/StartDebugMojoSupport.java | 34 +++++++++++++++++-- 4 files changed, 72 insertions(+), 6 deletions(-) diff --git a/liberty-maven-plugin/src/it/server-param-pom-override-it/pom.xml b/liberty-maven-plugin/src/it/server-param-pom-override-it/pom.xml index 970839cc4..949225f7c 100644 --- a/liberty-maven-plugin/src/it/server-param-pom-override-it/pom.xml +++ b/liberty-maven-plugin/src/it/server-param-pom-override-it/pom.xml @@ -13,6 +13,7 @@ war + someBootstrapValue -javaagent:/path/to/some/jar.jar @{myArgLine} @{undefined} @@ -70,6 +71,10 @@ -Xmx768m + + @{myBootstrapArg} + @{undefinedValue} + test src/test/resources/server.xml src/main/liberty/config/bootstrap.properties diff --git a/liberty-maven-plugin/src/it/server-param-pom-override-it/src/test/java/net/wasdev/wlp/maven/test/app/PluginConfigXmlIT.java b/liberty-maven-plugin/src/it/server-param-pom-override-it/src/test/java/net/wasdev/wlp/maven/test/app/PluginConfigXmlIT.java index cb6e0f7a4..172ce7a03 100644 --- a/liberty-maven-plugin/src/it/server-param-pom-override-it/src/test/java/net/wasdev/wlp/maven/test/app/PluginConfigXmlIT.java +++ b/liberty-maven-plugin/src/it/server-param-pom-override-it/src/test/java/net/wasdev/wlp/maven/test/app/PluginConfigXmlIT.java @@ -96,8 +96,33 @@ public void testBootstrapPropertiesFileElements() throws Exception { nodes = (NodeList) xPath.compile(expression).evaluate(inputDoc, XPathConstants.NODESET); assertEquals("Number of bootstrapProperties element ==>", 1, nodes.getLength()); - assertEquals("verify target server bootstrap.properties", "# Generated by liberty-maven-plugin\nlocation=pom.xml\n", - FileUtils.fileRead(TARGET_BOOTSTRAP_PROPERTIES).replaceAll("\r","")); + String fileContents = FileUtils.fileRead(TARGET_BOOTSTRAP_PROPERTIES).replaceAll("\r",""); + + String[] fileContentsArray = fileContents.split("\\n"); + assertTrue("fileContents", fileContentsArray.length == 4); + + boolean someBootstrapVarFound = false; + boolean locationFound = false; + boolean someUndefinedBootstrapVarFound = false; + + for (int i=0; i < fileContentsArray.length; i++) { + String nextLine = fileContentsArray[i]; + if (i == 0) { + assertTrue("comment not found on first line", nextLine.equals("# Generated by liberty-maven-plugin")); + } else { + if (nextLine.equals("someBootstrapVar=someBootstrapValue")) { + someBootstrapVarFound = true; + } else if (nextLine.equals("location=pom.xml")) { + locationFound = true; + } else if (nextLine.equals("someUndefinedBootstrapVar=@{undefinedValue}")) { + someUndefinedBootstrapVarFound = true; + } + } + } + + assertTrue("someBootstrapVar=someBootstrapValue not found", someBootstrapVarFound); + assertTrue("location=pom.xml not found", locationFound); + assertTrue("someUndefinedBootstrapVar=@{undefinedValue} not found", someUndefinedBootstrapVarFound); } @Test diff --git a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/PluginConfigSupport.java b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/PluginConfigSupport.java index 1b16fe83a..becefe411 100644 --- a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/PluginConfigSupport.java +++ b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/PluginConfigSupport.java @@ -120,7 +120,10 @@ protected File exportParametersToXml() throws IOException, ParserConfigurationEx if (combinedBootstrapProperties != null) { configDocument.createElement("bootstrapProperties", combinedBootstrapProperties); } else if (bootstrapProperties != null) { - configDocument.createElement("bootstrapProperties", bootstrapProperties); + if (bootstrapPropertiesResolved == null) { + bootstrapPropertiesResolved = handleLatePropertyResolution(bootstrapProperties); + } + configDocument.createElement("bootstrapProperties", bootstrapPropertiesResolved); } else { configFile = findConfigFile("bootstrap.properties", bootstrapPropertiesFile); if (configFile != null) { @@ -131,7 +134,10 @@ protected File exportParametersToXml() throws IOException, ParserConfigurationEx if (combinedJvmOptions != null) { configDocument.createElement("jvmOptions", combinedJvmOptions); } else if (jvmOptions != null) { - configDocument.createElement("jvmOptions", jvmOptions); + if (jvmOptionsResolved == null) { + jvmOptionsResolved = handleLatePropertyResolution(jvmOptions); + } + configDocument.createElement("jvmOptions", jvmOptionsResolved); } else { configFile = findConfigFile("jvm.options", jvmOptionsFile); if (configFile != null) { diff --git a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java index a75d3f566..851b24505 100644 --- a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java +++ b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java @@ -99,6 +99,10 @@ public abstract class StartDebugMojoSupport extends ServerFeatureSupport { protected Map combinedBootstrapProperties = null; protected List combinedJvmOptions = null; + // the following collections are copies of the originals with any @{xxx} references resolved in the values + protected Map bootstrapPropertiesResolved = null; // original collection is bootstrapProperties + protected List jvmOptionsResolved = null; // original collection is jvmOptions + @Component protected BuildPluginManager pluginManager; @@ -555,7 +559,8 @@ protected void copyConfigFiles() throws IOException, MojoExecutionException { if (jvmOptionsPath != null) { getLog().warn("The " + jvmOptionsPath + " file is overwritten by inlined configuration."); } - writeJvmOptions(optionsFile, jvmOptions, jvmMavenProps); + jvmOptionsResolved = handleLatePropertyResolution(jvmOptions); + writeJvmOptions(optionsFile, jvmOptionsResolved, jvmMavenProps); jvmOptionsPath = "inlined configuration"; } else if (jvmOptionsFile != null && jvmOptionsFile.exists()) { if (jvmOptionsPath != null) { @@ -582,7 +587,8 @@ protected void copyConfigFiles() throws IOException, MojoExecutionException { if (bootStrapPropertiesPath != null) { getLog().warn("The " + bootStrapPropertiesPath + " file is overwritten by inlined configuration."); } - writeBootstrapProperties(bootstrapFile, bootstrapProperties, bootstrapMavenProps); + bootstrapPropertiesResolved = handleLatePropertyResolution(bootstrapProperties); + writeBootstrapProperties(bootstrapFile, bootstrapPropertiesResolved, bootstrapMavenProps); bootStrapPropertiesPath = "inlined configuration"; } else if (bootstrapPropertiesFile != null && bootstrapPropertiesFile.exists()) { if (bootStrapPropertiesPath != null) { @@ -837,6 +843,30 @@ private String handleLatePropertyResolution(String value) { return returnValue; } + protected Map handleLatePropertyResolution(Map properties) { + Map propertiesResolved = null; + if (properties != null) { + propertiesResolved = new HashMap (); + for (Map.Entry entry : properties.entrySet()) { + String value = handleLatePropertyResolution(entry.getValue()); + propertiesResolved.put(entry.getKey(), value); + } + } + return propertiesResolved; + } + + protected List handleLatePropertyResolution(List properties) { + List propertiesResolved = null; + if (properties != null) { + propertiesResolved = new ArrayList (); + for (String nextOption : properties) { + String value = handleLatePropertyResolution(nextOption); + propertiesResolved.add(value); + } + } + return propertiesResolved; + } + // The properties parameter comes from the configuration in pom.xml and takes precedence over // the mavenProperties parameter, which comes from generic maven configuration. // One of the passed in Maps must be not null and not empty