Skip to content

Commit 0c0e05e

Browse files
committed
#47 Refactoring of global variable map process for maven and gradle
1 parent 24dddd2 commit 0c0e05e

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

jcp-tests/jcp-test-maven/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
</goals>
7474
<configuration>
7575
<useTestSources>true</useTestSources>
76+
<unknownVarAsFalse>true</unknownVarAsFalse>
7677
<vars>
7778
<empty.null.variable></empty.null.variable>
7879
<some.test.global.test>Some Test</some.test.global.test>

jcp/src/main/java/com/igormaznitsa/jcp/gradle/JcpTask.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class JcpTask extends DefaultTask {
113113
/**
114114
* List of variables to be registered in preprocessor as global ones.
115115
*/
116-
private final MapProperty<String, String> vars;
116+
private final MapProperty<String, Object> vars;
117117
/**
118118
* List of patterns of folder paths to be excluded from preprocessing, It uses
119119
* ANT path pattern format.
@@ -174,7 +174,7 @@ public JcpTask(final ObjectFactory factory) {
174174
this.sourceEncoding = factory.property(String.class).convention(StandardCharsets.UTF_8.name());
175175
this.eol = factory.property(String.class).convention(System.lineSeparator());
176176

177-
this.vars = factory.mapProperty(String.class, String.class);
177+
this.vars = factory.mapProperty(String.class, Object.class);
178178

179179
this.sources = factory.listProperty(File.class);
180180
this.configFiles = factory.listProperty(String.class);
@@ -289,7 +289,7 @@ public Property<Object> getKeepComments() {
289289
}
290290

291291
@Input
292-
public MapProperty<String, String> getVars() {
292+
public MapProperty<String, Object> getVars() {
293293
return vars;
294294
}
295295

@@ -415,10 +415,29 @@ public void warning(final String message) {
415415
preprocessorContext.setUnknownVariableAsFalse(this.unknownVarAsFalse.get());
416416
preprocessorContext.setVerbose(this.verbose.get());
417417

418-
this.vars.getOrElse(emptyMap()).forEach((key, value) -> {
419-
logger.debug(String.format("Registering global variable: %s=%s", key, value));
420-
preprocessorContext.setGlobalVariable(key, Value.recognizeRawString(value));
421-
});
418+
this.vars.getOrElse(emptyMap()).entrySet().stream()
419+
.filter(e -> {
420+
if (e.getValue() == null) {
421+
if (this.unknownVarAsFalse.get()) {
422+
logger.warn(String.format(
423+
"Global var '%s' ignored for null value (may be its content is empty in POM)",
424+
e.getKey()));
425+
return false;
426+
} else {
427+
throw new IllegalStateException(String.format(
428+
"Global var '%s' has null value, to ignore it set flag unknownVarAsFalse",
429+
e.getKey()));
430+
}
431+
} else {
432+
return true;
433+
}
434+
})
435+
.forEach(e -> {
436+
logger.debug(
437+
String.format("Registering global variable: %s=%s", e.getKey(), e.getValue()));
438+
preprocessorContext.setGlobalVariable(e.getKey(),
439+
Value.recognizeRawString(String.valueOf(e.getValue())));
440+
});
422441

423442
final JcpPreprocessor preprocessor = new JcpPreprocessor(preprocessorContext);
424443

jcp/src/main/java/com/igormaznitsa/jcp/maven/PreprocessMojo.java

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.commons.text.StringEscapeUtils;
5050
import org.apache.maven.execution.MavenSession;
5151
import org.apache.maven.plugin.AbstractMojo;
52+
import org.apache.maven.plugin.AbstractMojoExecutionException;
5253
import org.apache.maven.plugin.MojoExecutionException;
5354
import org.apache.maven.plugin.MojoFailureException;
5455
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -357,7 +358,7 @@ private void replaceSourceRootByPreprocessingDestinationFolder(final Preprocesso
357358
}
358359

359360

360-
PreprocessorContext makePreprocessorContext() throws IOException {
361+
PreprocessorContext makePreprocessorContext() throws AbstractMojoExecutionException {
361362
final PreprocessorContext context = new PreprocessorContext(this.getBaseDir());
362363
context.setPreprocessorLogger(this);
363364

@@ -406,24 +407,35 @@ PreprocessorContext makePreprocessorContext() throws IOException {
406407
this.configFiles.forEach(x -> context.registerConfigFile(new File(x)));
407408

408409
// register global vars
409-
this.getVars().entrySet().stream()
410-
.filter(e -> {
411-
final String key = e.getKey();
412-
final String value = e.getValue();
413-
if (value == null) {
414-
getLog().warn(String.format(
415-
"Global var '%s' ignored for null value (may be its content is empty in POM)",
416-
key));
417-
return false;
418-
} else {
419-
return true;
420-
}
421-
})
422-
.forEach(e -> {
423-
getLog().debug(
424-
String.format("Register global var: '%s' <- '%s'", e.getKey(), e.getValue()));
425-
context.setGlobalVariable(e.getKey(), Value.recognizeRawString(e.getValue()));
426-
});
410+
try {
411+
this.getVars().entrySet().stream()
412+
.filter(e -> {
413+
final String key = e.getKey();
414+
final String value = e.getValue();
415+
if (value == null) {
416+
if (this.isUnknownVarAsFalse()) {
417+
getLog().warn(String.format(
418+
"Global var '%s' ignored for null value (may be its content is empty in POM)",
419+
key));
420+
return false;
421+
} else {
422+
throw new IllegalStateException(String.format(
423+
"Global var '%s' has null value (may be its content is empty in POM), to ignore it set unknownVarAsFalse as true",
424+
key));
425+
}
426+
} else {
427+
return true;
428+
}
429+
})
430+
.forEach(e -> {
431+
getLog().debug(
432+
String.format("Register global var: '%s' <- '%s'", e.getKey(), e.getValue()));
433+
context.setGlobalVariable(e.getKey(), Value.recognizeRawString(e.getValue()));
434+
});
435+
} catch (final IllegalStateException ex) {
436+
getLog().error(ex.getMessage());
437+
throw new MojoFailureException(ex.getMessage());
438+
}
427439
return context;
428440
}
429441

0 commit comments

Comments
 (0)