Skip to content

Commit

Permalink
Merge pull request #1759 from cherylking/supportPropDepCoordinates
Browse files Browse the repository at this point in the history
Resolve maven prop in dep coordinates for user features
  • Loading branch information
cherylking authored Nov 28, 2023
2 parents 3817695 + 1883c1c commit 21fd74f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
<artifactId>prepare-multiple-features-it</artifactId>
<packaging>jar</packaging>

<properties>
<bom.group.id>test.user.test.osgi</bom.group.id>
<esa.artifact.id>hello-esa-plugin</esa.artifact.id>
</properties>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -153,7 +157,7 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>test.user.test.osgi</groupId>
<groupId>${bom.group.id}</groupId>
<artifactId>hello-bom</artifactId>
<version>1.0</version>
<type>pom</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
<dependencies>
<dependency>
<groupId>test.user.test.osgi</groupId>
<artifactId>hello-esa-plugin</artifactId>
<!--artifactId>hello-esa-plugin</artifactId-->
<artifactId>${esa.artifact.id}</artifactId>
<version>1.0</version>
<type>esa</type>
<scope>runtime</scope>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.HashSet;

import org.apache.maven.artifact.Artifact;
Expand Down Expand Up @@ -52,6 +54,9 @@
*
*/
public abstract class AbstractLibertySupport extends AbstractMojo {
private static final String PROP_RESOLUTION_SYNTAX = "\\$\\{(.+?)\\}";
private static final Pattern PROP_PATTERN = Pattern.compile(PROP_RESOLUTION_SYNTAX);

/**
* Maven Project
*/
Expand Down Expand Up @@ -132,6 +137,29 @@ public boolean include(Artifact artifact) {
return null;
}

// Search the value parameter for any properties referenced with ${xxx} syntax and replace those with their property value if defined.
protected String resolvePropertyReferences(String value) {
String returnValue = value;

if (value != null) {
Matcher m = PROP_PATTERN.matcher(value);
while (m.find()) {
String varName = m.group(1);
if (project.getProperties().containsKey(varName)) {
String replacementValue = project.getProperties().getProperty(varName);
if (replacementValue != null) {
returnValue = returnValue.replace("${"+varName+"}", replacementValue);
getLog().info("Replaced Liberty configuration property reference ${"+varName+"} with value "+replacementValue);
}
} else {
getLog().debug("Could not replace property reference: "+varName+" in value: "+value);
}
}
}

return returnValue;
}

/**
* Resolves the Dependency from the remote repository if necessary. If no version is specified, it will
* be retrieved from the dependency list or from the DependencyManagement section of the pom.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public void error(String msg, Throwable e) {
@Override
public File downloadArtifact(String groupId, String artifactId, String type, String version) throws PluginExecutionException {
try {
return getArtifact(groupId, artifactId, type, version).getFile();
// Look for and replace maven props in the coordinates. This is necessary when called from
// PrepareFeatureUtil.downloadArtifactsFromBOM(File) because that bom file will not have been processed by maven.
return getArtifact(resolvePropertyReferences(groupId),
resolvePropertyReferences(artifactId),
type,
resolvePropertyReferences(version)).getFile();
} catch (MojoExecutionException e) {
throw new PluginExecutionException(e);
}
Expand Down

0 comments on commit 21fd74f

Please sign in to comment.