Skip to content

Commit

Permalink
Add prop resolution for config parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cherylking committed Jul 12, 2023
1 parent a858609 commit 01e6090
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<packaging>war</packaging>

<properties>
<myBootstrapArg>someBootstrapValue</myBootstrapArg>
<myArgLine>-javaagent:/path/to/some/jar.jar</myArgLine>
<liberty.jvm.argLine>@{myArgLine}</liberty.jvm.argLine>
<liberty.jvm.undefined>@{undefined}</liberty.jvm.undefined>
Expand Down Expand Up @@ -70,6 +71,10 @@
<jvmOptions>
<param>-Xmx768m</param>
</jvmOptions>
<bootstrapProperties>
<someBootstrapVar>@{myBootstrapArg}</someBootstrapVar>
<someUndefinedBootstrapVar>@{undefinedValue}</someUndefinedBootstrapVar>
</bootstrapProperties>
<serverName>test</serverName>
<serverXmlFile>src/test/resources/server.xml</serverXmlFile>
<bootstrapPropertiesFile>src/main/liberty/config/bootstrap.properties</bootstrapPropertiesFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public abstract class StartDebugMojoSupport extends ServerFeatureSupport {
protected Map<String,String> combinedBootstrapProperties = null;
protected List<String> combinedJvmOptions = null;

// the following collections are copies of the originals with any @{xxx} references resolved in the values
protected Map<String,String> bootstrapPropertiesResolved = null; // original collection is bootstrapProperties
protected List<String> jvmOptionsResolved = null; // original collection is jvmOptions

@Component
protected BuildPluginManager pluginManager;

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -837,6 +843,30 @@ private String handleLatePropertyResolution(String value) {
return returnValue;
}

protected Map<String,String> handleLatePropertyResolution(Map<String,String> properties) {
Map<String,String> propertiesResolved = null;
if (properties != null) {
propertiesResolved = new HashMap<String,String> ();
for (Map.Entry<String, String> entry : properties.entrySet()) {
String value = handleLatePropertyResolution(entry.getValue());
propertiesResolved.put(entry.getKey(), value);
}
}
return propertiesResolved;
}

protected List<String> handleLatePropertyResolution(List<String> properties) {
List<String> propertiesResolved = null;
if (properties != null) {
propertiesResolved = new ArrayList<String> ();
for (String nextOption : properties) {
String value = handleLatePropertyResolution(nextOption);
propertiesResolved.add(value);
}
}
return propertiesResolved;
}

// The properties parameter comes from the <bootstrapProperties> configuration in pom.xml and takes precedence over
// the mavenProperties parameter, which comes from generic maven <properties> configuration.
// One of the passed in Maps must be not null and not empty
Expand Down

0 comments on commit 01e6090

Please sign in to comment.