Skip to content

Commit 58ffe15

Browse files
committed
review
1 parent b4a20f2 commit 58ffe15

File tree

2 files changed

+91
-14
lines changed

2 files changed

+91
-14
lines changed

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/RenderDependenciesMojo.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
31+
import java.nio.file.Paths;
3132
import java.util.Collections;
3233
import java.util.Comparator;
3334
import java.util.List;
@@ -49,6 +50,7 @@
4950
import org.apache.maven.project.MavenProjectHelper;
5051
import org.apache.maven.project.ProjectBuilder;
5152
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
53+
import org.apache.velocity.Template;
5254
import org.apache.velocity.VelocityContext;
5355
import org.apache.velocity.app.VelocityEngine;
5456
import org.apache.velocity.tools.generic.CollectionTool;
@@ -59,37 +61,45 @@
5961
/**
6062
* This goal renders dependencies based on a velocity template.
6163
*
62-
* @since 3.8.2
64+
* @since 3.9.0
6365
*/
6466
@Mojo(
6567
name = "render-dependencies",
6668
requiresDependencyResolution = ResolutionScope.TEST,
6769
defaultPhase = LifecyclePhase.GENERATE_SOURCES,
6870
threadSafe = true)
6971
public class RenderDependenciesMojo extends AbstractDependencyFilterMojo {
72+
/**
73+
* Encoding to write the rendered template.
74+
* @since 3.9.0
75+
*/
7076
@Parameter(property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}")
7177
private String outputEncoding;
7278

7379
/**
7480
* The file to write the rendered template string. If undefined, it just prints the classpath as [INFO].
81+
* @since 3.9.0
7582
*/
7683
@Parameter(property = "mdep.outputFile")
7784
private File outputFile;
7885

7986
/**
8087
* If not null or empty it will attach the artifact with this classifier.
88+
* @since 3.9.0
8189
*/
8290
@Parameter(property = "mdep.classifier", defaultValue = "template")
8391
private String classifier;
8492

8593
/**
8694
* Extension to use for the attached file if classifier is not null/empty.
95+
* @since 3.9.0
8796
*/
8897
@Parameter(property = "mdep.extension", defaultValue = "txt")
8998
private String extension;
9099

91100
/**
92101
* velocity template to use to render the output file.
102+
* @since 3.9.0
93103
*/
94104
@Parameter(property = "mdep.template", defaultValue = "<set the template>")
95105
private String template;
@@ -148,8 +158,16 @@ protected void doExecute() throws MojoExecutionException {
148158
* @return the template rendered.
149159
*/
150160
private String render(final List<Artifact> artifacts) {
161+
final Path templatePath = getTemplatePath();
162+
final boolean fromFile = templatePath != null && Files.exists(templatePath);
163+
151164
final Properties props = new Properties();
152165
props.setProperty("runtime.strict_mode.enable", "true");
166+
if (fromFile) {
167+
props.setProperty(
168+
"resource.loader.file.path",
169+
templatePath.toAbsolutePath().getParent().toString());
170+
}
153171

154172
final VelocityEngine ve = new VelocityEngine(props);
155173
ve.init();
@@ -160,19 +178,29 @@ private String render(final List<Artifact> artifacts) {
160178

161179
// Merge template + context
162180
final StringWriter writer = new StringWriter();
163-
try {
164-
ve.evaluate(context, writer, "tpl-" + Math.abs(hashCode()), template);
165-
} finally {
166-
try {
167-
writer.close();
168-
} catch (final IOException e) {
169-
// no-op, not possible
181+
try (final StringWriter ignored = writer) {
182+
if (fromFile) {
183+
final Template template =
184+
ve.getTemplate(templatePath.getFileName().toString());
185+
template.merge(context, writer);
186+
} else {
187+
ve.evaluate(context, writer, "tpl-" + Math.abs(hashCode()), template);
170188
}
189+
} catch (final IOException e) {
190+
// no-op, not possible
171191
}
172192

173193
return writer.toString();
174194
}
175195

196+
private Path getTemplatePath() {
197+
try {
198+
return Paths.get(template);
199+
} catch (final RuntimeException re) {
200+
return null;
201+
}
202+
}
203+
176204
/**
177205
* Trivial null protection impl for comparing callback.
178206
* @param getter nominal getter.
@@ -227,6 +255,10 @@ protected ArtifactsFilter getMarkedArtifactFilter() {
227255
return null;
228256
}
229257

258+
public void setExtension(final String extension) {
259+
this.extension = extension;
260+
}
261+
230262
public void setOutputEncoding(final String outputEncoding) {
231263
this.outputEncoding = outputEncoding;
232264
}

src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestRenderDependenciesMojo.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
package org.apache.maven.plugins.dependency.fromDependencies;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
import java.nio.file.Files;
2225
import java.util.Set;
2326

2427
import org.apache.maven.artifact.Artifact;
@@ -66,12 +69,7 @@ protected void setUp() throws Exception {
6669
public void testRender() throws Exception {
6770
final File rendered = new File(testDir, "render-dependencies.testRender.txt");
6871

69-
final MavenProject project = mojo.getProject();
70-
final Set<Artifact> artifacts = stubFactory.getScopedArtifacts();
71-
final Set<Artifact> directArtifacts = stubFactory.getReleaseAndSnapshotArtifacts();
72-
artifacts.addAll(directArtifacts);
73-
project.setArtifacts(artifacts);
74-
project.setDependencyArtifacts(directArtifacts);
72+
setupProject();
7573

7674
mojo.setTemplate("deps:\n"
7775
+ " jars:\n"
@@ -101,4 +99,51 @@ public void testRender() throws Exception {
10199
+ " - local:///opt/test/libs/system-1.0.jar\n"
102100
+ " - local:///opt/test/libs/test-1.0.jar\n");
103101
}
102+
103+
/**
104+
* Tests the rendering with a file template.
105+
*/
106+
public void testRenderFromFile() throws Exception {
107+
final File rendered = new File(testDir, "render-dependencies.testRenderFromFile.txt");
108+
final File template = new File(testDir, "render-dependencies.testRenderFromFile.template.vm");
109+
Files.write(
110+
template.toPath(),
111+
("deps:\n"
112+
+ " jars:\n"
113+
+ " - local:///opt/test/libs/compile-1.0.jar\n"
114+
+ " - local:///opt/test/libs/provided-1.0.jar\n"
115+
+ " - local:///opt/test/libs/release-1.0.jar\n"
116+
+ " - local:///opt/test/libs/runtime-1.0.jar\n"
117+
+ " - local:///opt/test/libs/snapshot-2.0-SNAPSHOT.jar\n"
118+
+ " - local:///opt/test/libs/system-1.0.jar\n"
119+
+ " - local:///opt/test/libs/test-1.0.jar\n")
120+
.getBytes(StandardCharsets.UTF_8));
121+
122+
setupProject();
123+
124+
mojo.setTemplate(template.getAbsolutePath());
125+
mojo.setOutputFile(rendered);
126+
mojo.execute();
127+
128+
assertThat(rendered)
129+
.content()
130+
.isEqualTo("deps:\n"
131+
+ " jars:\n"
132+
+ " - local:///opt/test/libs/compile-1.0.jar\n"
133+
+ " - local:///opt/test/libs/provided-1.0.jar\n"
134+
+ " - local:///opt/test/libs/release-1.0.jar\n"
135+
+ " - local:///opt/test/libs/runtime-1.0.jar\n"
136+
+ " - local:///opt/test/libs/snapshot-2.0-SNAPSHOT.jar\n"
137+
+ " - local:///opt/test/libs/system-1.0.jar\n"
138+
+ " - local:///opt/test/libs/test-1.0.jar\n");
139+
}
140+
141+
private void setupProject() throws IOException {
142+
final MavenProject project = mojo.getProject();
143+
final Set<Artifact> artifacts = stubFactory.getScopedArtifacts();
144+
final Set<Artifact> directArtifacts = stubFactory.getReleaseAndSnapshotArtifacts();
145+
artifacts.addAll(directArtifacts);
146+
project.setArtifacts(artifacts);
147+
project.setDependencyArtifacts(directArtifacts);
148+
}
104149
}

0 commit comments

Comments
 (0)