Skip to content

Improve variable interpolation #823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>4.0.2</version>
</dependency>
</dependencies>

<build>
Expand Down
59 changes: 59 additions & 0 deletions src/it/property-expansion/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.vafer</groupId>
<artifactId>jdeb-it</artifactId>
<version>1.0</version>
<description>description from pom</description>
<properties>
<property.upstream.version>2.0.0-SNAPSHOT</property.upstream.version>
<property.package.revision>1</property.package.revision>

<property.project.version>${release.version}-${property.package.revision}</property.project.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.vafer</groupId>
<artifactId>jdeb</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jdeb</goal>
</goals>
<configuration>
<verbose>true</verbose>
<controlDir>${basedir}/src/deb/control</controlDir>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>regex-property</id>
<goals>
<goal>regex-property</goal>
</goals>
<configuration>
<name>release.version</name>
<value>${property.upstream.version}</value>
<regex>-SNAPSHOT</regex>
<replacement></replacement>
<failIfNoMatch>false</failIfNoMatch>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>
7 changes: 7 additions & 0 deletions src/it/property-expansion/src/deb/control/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Package: [[name]]
Version: [[property.project.version]]
Section: misc
Priority: low
Architecture: all
Description: [[description]]
Maintainer: [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.vafer.jdeb.examples;

public class Main {
public static void main(String[] args) {
System.out.println("jdeb example!");
}
}
54 changes: 54 additions & 0 deletions src/it/property-expansion/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.nio.file.Files
import java.util.zip.GZIPInputStream
import org.apache.commons.compress.archivers.ar.ArArchiveInputStream
import org.apache.commons.compress.archivers.ar.ArArchiveEntry
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream

File deb = new File(basedir, 'target/jdeb-it_1.0_all.deb')
assert deb.exists()

InputStream debInput = new FileInputStream(deb)
ArArchiveInputStream arInput = new ArArchiveInputStream(debInput)
List<String> controlContent = []

ArArchiveEntry entry
while ((entry = arInput.getNextEntry()) != null) {
if (entry.getName() == 'control.tar.gz') {

// Save control.tar.gz to a temporary file
File tempControlGz = File.createTempFile("control", ".tar.gz")
tempControlGz.deleteOnExit()
tempControlGz.withOutputStream { out ->
byte[] buffer = new byte[4096]
int len
while ((len = arInput.read(buffer)) != -1) {
out.write(buffer, 0, len)
}
}

// Read the contents of control.tar.gz
GZIPInputStream gzipIn = new GZIPInputStream(new FileInputStream(tempControlGz))
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)

def tarEntry
while ((tarEntry = tarIn.nextEntry) != null) {
String name = tarEntry.name

if (name == './control' || name == 'control') {
BufferedReader reader = new BufferedReader(new InputStreamReader(tarIn, 'UTF-8'))
while (reader.readLine() != null) {
controlContent.add(reader.readLine());
}
break
}
}

tarIn.close()
gzipIn.close()
}
}

arInput.close()
debInput.close()

assert "Version: 2.0.0-1".equals(controlContent.get(0))
41 changes: 8 additions & 33 deletions src/main/java/org/vafer/jdeb/maven/DebMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,37 +414,6 @@ protected void setData( Data[] dataSet ) {
}
}

@SuppressWarnings("unchecked,rawtypes")
protected VariableResolver initializeVariableResolver( Map<String, String> variables, Long outputTimestampMs ) {
variables.putAll((Map) getProject().getProperties());
variables.putAll((Map) System.getProperties());
variables.put("name", name != null ? name : getProject().getName());
variables.put("artifactId", getProject().getArtifactId());
variables.put("groupId", getProject().getGroupId());
variables.put("version", getProjectVersion(outputTimestampMs));
variables.put("description", getProject().getDescription());
variables.put("extension", "deb");
variables.put("baseDir", getProject().getBasedir().getAbsolutePath());
variables.put("buildDir", buildDirectory.getAbsolutePath());
variables.put("project.version", getProject().getVersion());

if (getProject().getInceptionYear() != null) {
variables.put("project.inceptionYear", getProject().getInceptionYear());
}
if (getProject().getOrganization() != null) {
if (getProject().getOrganization().getName() != null) {
variables.put("project.organization.name", getProject().getOrganization().getName());
}
if (getProject().getOrganization().getUrl() != null) {
variables.put("project.organization.url", getProject().getOrganization().getUrl());
}
}

variables.put("url", getProject().getUrl());

return new MapVariableResolver(variables);
}

/**
* Doc some cleanup and conversion on the Maven project version.
* <ul>
Expand Down Expand Up @@ -521,10 +490,16 @@ public void execute() throws MojoExecutionException {
console = new MojoConsole(getLog(), verbose);

initializeSignProperties();

Long outputTimestampMs = new OutputTimestampResolver(console).resolveOutputTimestamp(outputTimestamp);

final VariableResolver resolver = initializeVariableResolver(new HashMap<String, String>(), outputTimestampMs);
final VariableResolver resolver = MapVariableResolver.builder()
.withName(this.name)
.withVersion(getProjectVersion(outputTimestampMs))
.withMavenProject(getProject())
.withSystemProperties(System.getProperties())
.withBuildDirectory(this.buildDirectory.getAbsolutePath())
.build();

final File debFile = new File(Utils.replaceVariables(resolver, deb, openReplaceToken, closeReplaceToken));
final File controlDirFile = new File(Utils.replaceVariables(resolver, controlDir, openReplaceToken, closeReplaceToken));
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
*/
package org.vafer.jdeb.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;

/**
* Resolve variables based on a Map.
Expand All @@ -34,4 +40,92 @@ public String get( String key ) {
return map.get(key);
}

public static MapVariableResolverBuilder builder() {
return new MapVariableResolverBuilder();
}

public static class MapVariableResolverBuilder {
private String name;
private MavenProject mavenProject;
private Properties systemProperties;
private String buildDirectory;
private String version;

public MapVariableResolverBuilder withName(String name) {
this.name = name;
return this;
}

public MapVariableResolverBuilder withMavenProject(MavenProject project) {
this.mavenProject = project;
return this;
}

public MapVariableResolverBuilder withSystemProperties(Properties properties) {
this.systemProperties = properties;
return this;
}

public MapVariableResolverBuilder withBuildDirectory(String directory) {
this.buildDirectory = directory;
return this;
}

public MapVariableResolverBuilder withVersion(String version) {
this.version = version;
return this;
}

public MapVariableResolver build() {
if( this.mavenProject == null || this.systemProperties == null ) {
throw new IllegalStateException("MavenProject and system properties must be set");
}

Map<String, String> variables = new HashMap<String, String>() ;

Map<String, String> combinedProperties = new HashMap<>();
combinedProperties.putAll((Map) this.mavenProject.getProperties());
combinedProperties.putAll((Map) this.systemProperties);

// Expand (interpolate) values using RegexBasedInterpolator
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
for (Map.Entry<String, String> entry : combinedProperties.entrySet()) {
interpolator.addValueSource(new org.codehaus.plexus.interpolation.MapBasedValueSource(combinedProperties));
try {
String expandedValue = interpolator.interpolate(entry.getValue(), "");
variables.put(entry.getKey(), expandedValue);
} catch (InterpolationException e) {
// Fallback to original value if interpolation fails
variables.put(entry.getKey(), entry.getValue());
}
}

variables.put("name", this.name != null ? this.name : this.mavenProject.getName());
variables.put("artifactId", this.mavenProject.getArtifactId());
variables.put("groupId", this.mavenProject.getGroupId());
variables.put("version", this.version);
variables.put("description", this.mavenProject.getDescription());
variables.put("extension", "deb");
variables.put("baseDir", this.mavenProject.getBasedir().getAbsolutePath());
variables.put("buildDir", this.buildDirectory);
variables.put("project.version", this.mavenProject.getVersion());

if (this.mavenProject.getInceptionYear() != null) {
variables.put("project.inceptionYear", this.mavenProject.getInceptionYear());
}
if (this.mavenProject.getOrganization() != null) {
if (this.mavenProject.getOrganization().getName() != null) {
variables.put("project.organization.name", this.mavenProject.getOrganization().getName());
}
if (this.mavenProject.getOrganization().getUrl() != null) {
variables.put("project.organization.url", this.mavenProject.getOrganization().getUrl());
}
}

variables.put("url", this.mavenProject.getUrl());

return new MapVariableResolver(variables);
}
}

}
Loading
Loading