Skip to content

Commit cdb31db

Browse files
committed
#47 Maven plugin, fixed NPE for empty global variable value
1 parent 5046953 commit cdb31db

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<configuration>
7575
<useTestSources>true</useTestSources>
7676
<vars>
77+
<empty.null.variable></empty.null.variable>
7778
<some.test.global.test>Some Test</some.test.global.test>
7879
</vars>
7980
</configuration>

jcp-tests/jcp-test-maven/src/main/java/com/igormaznitsa/jcp/it/maven/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public Main(){
1212
throw new Error("Must be MainTwo but detected"+this.getClass().getName());
1313
}
1414

15+
//#if empty.null.variable
16+
throw new Error("Must not be presented because variable must be recognized as false");
17+
//#endif
18+
1519
final String test = /*$"\""+some.test.global+"\";"$*/ /*-*/ "";
1620

1721
if ("Some Test Global Value".equals(test)){

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ PreprocessorContext makePreprocessorContext() throws IOException {
363363

364364
if (this.project != null) {
365365
final MavenPropertiesImporter mavenPropertiesImporter =
366-
new MavenPropertiesImporter(context,
367-
this.project,
368-
this.session,
369-
isVerbose() || getLog().isDebugEnabled()
370-
);
366+
new MavenPropertiesImporter(context,
367+
this.project,
368+
this.session,
369+
isVerbose() || getLog().isDebugEnabled()
370+
);
371371
context.registerSpecialVariableProcessor(mavenPropertiesImporter);
372372
}
373373

@@ -405,12 +405,25 @@ PreprocessorContext makePreprocessorContext() throws IOException {
405405

406406
this.configFiles.forEach(x -> context.registerConfigFile(new File(x)));
407407

408-
// process global vars
409-
this.getVars().forEach((key, value) -> {
410-
getLog().debug(String.format("Register global var: '%s' <- '%s'", key, value));
411-
context.setGlobalVariable(key, Value.recognizeRawString(value));
412-
});
413-
408+
// 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+
});
414427
return context;
415428
}
416429

0 commit comments

Comments
 (0)