Skip to content

Commit 912ea02

Browse files
authored
Improve variable interpolation (#823)
* test: add baseline * test: trigger issue * test: verify control file content * test: remove debug prints * fix: issue #822 * refactor: move initializeVariableResolver into MapVariableResolver builder * refactor: remove unnecessary arguments * refactor: remove unused imports * test: initial sanity check unit test * test: more comprehensive testing * test: refactor for easier readability * test: added nested meta property * test: add system properties interpolation test * test: minor fixes * style: retab * style: consistent use of "this" * style: fix copyright header year * test: add test case for optional properties * test: add test case with minimal data * test: improved naming convention * feat: add some error handling * refactor: remove getMap()
1 parent 1a0260b commit 912ea02

File tree

8 files changed

+467
-33
lines changed

8 files changed

+467
-33
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@
188188
<version>4.11.0</version>
189189
<scope>test</scope>
190190
</dependency>
191+
<dependency>
192+
<groupId>org.codehaus.plexus</groupId>
193+
<artifactId>plexus-utils</artifactId>
194+
<version>4.0.2</version>
195+
</dependency>
191196
</dependencies>
192197

193198
<build>

src/it/property-expansion/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.vafer</groupId>
5+
<artifactId>jdeb-it</artifactId>
6+
<version>1.0</version>
7+
<description>description from pom</description>
8+
<properties>
9+
<property.upstream.version>2.0.0-SNAPSHOT</property.upstream.version>
10+
<property.package.revision>1</property.package.revision>
11+
12+
<property.project.version>${release.version}-${property.package.revision}</property.project.version>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.vafer</groupId>
20+
<artifactId>jdeb</artifactId>
21+
<version>@project.version@</version>
22+
<executions>
23+
<execution>
24+
<phase>package</phase>
25+
<goals>
26+
<goal>jdeb</goal>
27+
</goals>
28+
<configuration>
29+
<verbose>true</verbose>
30+
<controlDir>${basedir}/src/deb/control</controlDir>
31+
</configuration>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
36+
<plugin>
37+
<groupId>org.codehaus.mojo</groupId>
38+
<artifactId>build-helper-maven-plugin</artifactId>
39+
<version>3.6.0</version>
40+
<executions>
41+
<execution>
42+
<id>regex-property</id>
43+
<goals>
44+
<goal>regex-property</goal>
45+
</goals>
46+
<configuration>
47+
<name>release.version</name>
48+
<value>${property.upstream.version}</value>
49+
<regex>-SNAPSHOT</regex>
50+
<replacement></replacement>
51+
<failIfNoMatch>false</failIfNoMatch>
52+
</configuration>
53+
</execution>
54+
</executions>
55+
</plugin>
56+
57+
</plugins>
58+
</build>
59+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Package: [[name]]
2+
Version: [[property.project.version]]
3+
Section: misc
4+
Priority: low
5+
Architecture: all
6+
Description: [[description]]
7+
Maintainer: [email protected]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.vafer.jdeb.examples;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
System.out.println("jdeb example!");
6+
}
7+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.nio.file.Files
2+
import java.util.zip.GZIPInputStream
3+
import org.apache.commons.compress.archivers.ar.ArArchiveInputStream
4+
import org.apache.commons.compress.archivers.ar.ArArchiveEntry
5+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream
6+
7+
File deb = new File(basedir, 'target/jdeb-it_1.0_all.deb')
8+
assert deb.exists()
9+
10+
InputStream debInput = new FileInputStream(deb)
11+
ArArchiveInputStream arInput = new ArArchiveInputStream(debInput)
12+
List<String> controlContent = []
13+
14+
ArArchiveEntry entry
15+
while ((entry = arInput.getNextEntry()) != null) {
16+
if (entry.getName() == 'control.tar.gz') {
17+
18+
// Save control.tar.gz to a temporary file
19+
File tempControlGz = File.createTempFile("control", ".tar.gz")
20+
tempControlGz.deleteOnExit()
21+
tempControlGz.withOutputStream { out ->
22+
byte[] buffer = new byte[4096]
23+
int len
24+
while ((len = arInput.read(buffer)) != -1) {
25+
out.write(buffer, 0, len)
26+
}
27+
}
28+
29+
// Read the contents of control.tar.gz
30+
GZIPInputStream gzipIn = new GZIPInputStream(new FileInputStream(tempControlGz))
31+
TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)
32+
33+
def tarEntry
34+
while ((tarEntry = tarIn.nextEntry) != null) {
35+
String name = tarEntry.name
36+
37+
if (name == './control' || name == 'control') {
38+
BufferedReader reader = new BufferedReader(new InputStreamReader(tarIn, 'UTF-8'))
39+
while (reader.readLine() != null) {
40+
controlContent.add(reader.readLine());
41+
}
42+
break
43+
}
44+
}
45+
46+
tarIn.close()
47+
gzipIn.close()
48+
}
49+
}
50+
51+
arInput.close()
52+
debInput.close()
53+
54+
assert "Version: 2.0.0-1".equals(controlContent.get(0))

src/main/java/org/vafer/jdeb/maven/DebMojo.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -414,37 +414,6 @@ protected void setData( Data[] dataSet ) {
414414
}
415415
}
416416

417-
@SuppressWarnings("unchecked,rawtypes")
418-
protected VariableResolver initializeVariableResolver( Map<String, String> variables, Long outputTimestampMs ) {
419-
variables.putAll((Map) getProject().getProperties());
420-
variables.putAll((Map) System.getProperties());
421-
variables.put("name", name != null ? name : getProject().getName());
422-
variables.put("artifactId", getProject().getArtifactId());
423-
variables.put("groupId", getProject().getGroupId());
424-
variables.put("version", getProjectVersion(outputTimestampMs));
425-
variables.put("description", getProject().getDescription());
426-
variables.put("extension", "deb");
427-
variables.put("baseDir", getProject().getBasedir().getAbsolutePath());
428-
variables.put("buildDir", buildDirectory.getAbsolutePath());
429-
variables.put("project.version", getProject().getVersion());
430-
431-
if (getProject().getInceptionYear() != null) {
432-
variables.put("project.inceptionYear", getProject().getInceptionYear());
433-
}
434-
if (getProject().getOrganization() != null) {
435-
if (getProject().getOrganization().getName() != null) {
436-
variables.put("project.organization.name", getProject().getOrganization().getName());
437-
}
438-
if (getProject().getOrganization().getUrl() != null) {
439-
variables.put("project.organization.url", getProject().getOrganization().getUrl());
440-
}
441-
}
442-
443-
variables.put("url", getProject().getUrl());
444-
445-
return new MapVariableResolver(variables);
446-
}
447-
448417
/**
449418
* Doc some cleanup and conversion on the Maven project version.
450419
* <ul>
@@ -521,10 +490,16 @@ public void execute() throws MojoExecutionException {
521490
console = new MojoConsole(getLog(), verbose);
522491

523492
initializeSignProperties();
524-
493+
525494
Long outputTimestampMs = new OutputTimestampResolver(console).resolveOutputTimestamp(outputTimestamp);
526495

527-
final VariableResolver resolver = initializeVariableResolver(new HashMap<String, String>(), outputTimestampMs);
496+
final VariableResolver resolver = MapVariableResolver.builder()
497+
.withName(this.name)
498+
.withVersion(getProjectVersion(outputTimestampMs))
499+
.withMavenProject(getProject())
500+
.withSystemProperties(System.getProperties())
501+
.withBuildDirectory(this.buildDirectory.getAbsolutePath())
502+
.build();
528503

529504
final File debFile = new File(Utils.replaceVariables(resolver, deb, openReplaceToken, closeReplaceToken));
530505
final File controlDirFile = new File(Utils.replaceVariables(resolver, controlDir, openReplaceToken, closeReplaceToken));

src/main/java/org/vafer/jdeb/utils/MapVariableResolver.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
*/
1616
package org.vafer.jdeb.utils;
1717

18+
import java.util.HashMap;
1819
import java.util.Map;
20+
import java.util.Properties;
21+
22+
import org.apache.maven.project.MavenProject;
23+
import org.codehaus.plexus.interpolation.InterpolationException;
24+
import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
1925

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

43+
public static MapVariableResolverBuilder builder() {
44+
return new MapVariableResolverBuilder();
45+
}
46+
47+
public static class MapVariableResolverBuilder {
48+
private String name;
49+
private MavenProject mavenProject;
50+
private Properties systemProperties;
51+
private String buildDirectory;
52+
private String version;
53+
54+
public MapVariableResolverBuilder withName(String name) {
55+
this.name = name;
56+
return this;
57+
}
58+
59+
public MapVariableResolverBuilder withMavenProject(MavenProject project) {
60+
this.mavenProject = project;
61+
return this;
62+
}
63+
64+
public MapVariableResolverBuilder withSystemProperties(Properties properties) {
65+
this.systemProperties = properties;
66+
return this;
67+
}
68+
69+
public MapVariableResolverBuilder withBuildDirectory(String directory) {
70+
this.buildDirectory = directory;
71+
return this;
72+
}
73+
74+
public MapVariableResolverBuilder withVersion(String version) {
75+
this.version = version;
76+
return this;
77+
}
78+
79+
public MapVariableResolver build() {
80+
if( this.mavenProject == null || this.systemProperties == null ) {
81+
throw new IllegalStateException("MavenProject and system properties must be set");
82+
}
83+
84+
Map<String, String> variables = new HashMap<String, String>() ;
85+
86+
Map<String, String> combinedProperties = new HashMap<>();
87+
combinedProperties.putAll((Map) this.mavenProject.getProperties());
88+
combinedProperties.putAll((Map) this.systemProperties);
89+
90+
// Expand (interpolate) values using RegexBasedInterpolator
91+
RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
92+
for (Map.Entry<String, String> entry : combinedProperties.entrySet()) {
93+
interpolator.addValueSource(new org.codehaus.plexus.interpolation.MapBasedValueSource(combinedProperties));
94+
try {
95+
String expandedValue = interpolator.interpolate(entry.getValue(), "");
96+
variables.put(entry.getKey(), expandedValue);
97+
} catch (InterpolationException e) {
98+
// Fallback to original value if interpolation fails
99+
variables.put(entry.getKey(), entry.getValue());
100+
}
101+
}
102+
103+
variables.put("name", this.name != null ? this.name : this.mavenProject.getName());
104+
variables.put("artifactId", this.mavenProject.getArtifactId());
105+
variables.put("groupId", this.mavenProject.getGroupId());
106+
variables.put("version", this.version);
107+
variables.put("description", this.mavenProject.getDescription());
108+
variables.put("extension", "deb");
109+
variables.put("baseDir", this.mavenProject.getBasedir().getAbsolutePath());
110+
variables.put("buildDir", this.buildDirectory);
111+
variables.put("project.version", this.mavenProject.getVersion());
112+
113+
if (this.mavenProject.getInceptionYear() != null) {
114+
variables.put("project.inceptionYear", this.mavenProject.getInceptionYear());
115+
}
116+
if (this.mavenProject.getOrganization() != null) {
117+
if (this.mavenProject.getOrganization().getName() != null) {
118+
variables.put("project.organization.name", this.mavenProject.getOrganization().getName());
119+
}
120+
if (this.mavenProject.getOrganization().getUrl() != null) {
121+
variables.put("project.organization.url", this.mavenProject.getOrganization().getUrl());
122+
}
123+
}
124+
125+
variables.put("url", this.mavenProject.getUrl());
126+
127+
return new MapVariableResolver(variables);
128+
}
129+
}
130+
37131
}

0 commit comments

Comments
 (0)