Skip to content

Commit a7b042c

Browse files
committed
Improve Maven targetVersion default/auto-detection/logging
1 parent 05d6433 commit a7b042c

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

jsonschema2pojo-gradle-plugin/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ dependencies {
5656
implementation 'joda-time:joda-time:2.2'
5757
}
5858
59-
// Each configuration is set to the default value
6059
jsonSchema2Pojo {
6160
6261
// Location of the JSON Schema file(s). This may refer to a single file or a directory of files.
@@ -292,8 +291,10 @@ jsonSchema2Pojo {
292291
// properties to exclude from generated toString
293292
toStringExcludes = ["someProperty"]
294293
295-
// What java source version to target with generated output (1.6, 1.8, 9, 11, etc)
296-
targetVersion = "1.6"
294+
// What Java version to target with generated source code (1.6, 1.8, 9, 11, etc).
295+
// By default, the version will be taken from the Gradle Java plugin's 'sourceCompatibility',
296+
// which (if unset) itself defaults to the current JVM version
297+
targetVersion = "1.8"
297298
298299
// deprecated, since we no longer use commons-lang for equals, hashCode, toString
299300
useCommonsLang3 = false

jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/TestableJsonschema2PojoMojo.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,32 @@
1616

1717
package org.jsonschema2pojo.integration.util;
1818

19+
import org.jsonschema2pojo.maven.Jsonschema2PojoMojo;
20+
1921
import java.lang.reflect.Field;
2022
import java.util.Map;
2123
import java.util.Map.Entry;
22-
23-
import org.jsonschema2pojo.maven.Jsonschema2PojoMojo;
24+
import org.apache.maven.plugin.logging.Log;
25+
import org.apache.maven.plugin.logging.SystemStreamLog;
2426

2527
/**
2628
* A plugin mojo that allows the private property values usually only set by
2729
* Maven to be set programatically.
2830
*/
2931
public class TestableJsonschema2PojoMojo extends Jsonschema2PojoMojo {
3032

33+
private static final Log LOGGER_NO_DEBUG = new SystemStreamLog() {
34+
@Override
35+
public void debug(CharSequence content) {
36+
}
37+
@Override
38+
public void debug(Throwable error) {
39+
}
40+
@Override
41+
public void debug(CharSequence content, Throwable error) {
42+
}
43+
};
44+
3145
public TestableJsonschema2PojoMojo configure(Map<String, Object> configValues) {
3246

3347
// this could be done with reflection, if the plugin used real annotations.
@@ -37,6 +51,8 @@ public TestableJsonschema2PojoMojo configure(Map<String, Object> configValues) {
3751
setPrivateField(value.getKey(), value.getValue());
3852
}
3953

54+
this.setLog(LOGGER_NO_DEBUG);
55+
4056
return this;
4157
}
4258

jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,26 @@ public class Jsonschema2PojoMojo extends AbstractMojo implements GenerationConfi
602602
* create public fields instead
603603
*
604604
*/
605-
@Parameter(property = "jsonschame2pojo.includeSetters", defaultValue = "true")
605+
@Parameter(property = "jsonschema2pojo.includeSetters", defaultValue = "true")
606606
private boolean includeSetters = true;
607607

608608
/**
609-
* The target version for generated source files.
610-
*
611-
* @since 0.4.17
609+
* The target version for generated source files, used whenever decisions are made
610+
* about generating source code that may be incompatible with older JVMs. Acceptable values
611+
* include e.g. 1.6, 1.8, 8, 9, 10, 11.
612+
* <p/>
613+
* If not set, the value of targetVersion is auto-detected. For auto-detection, the first
614+
* value found in the following list will be used:
615+
* <ol>
616+
* <li>maven.compiler.source property</li>
617+
* <li>maven.compiler.release property</li>
618+
* <li>maven-compiler-plugin 'source' configuration option</li>
619+
* <li>maven-compiler-plugin 'release' configuration option</li>
620+
* <li>the current JVM version</li>
621+
* </ol>
612622
*/
613-
@Parameter(property = "jsonschema2pojo.targetJavaVersion", defaultValue = "${maven.compiler.target}")
614-
private String targetVersion = "";
623+
@Parameter(property = "jsonschema2pojo.targetVersion")
624+
private String targetVersion;
615625

616626
/**
617627
* Whether to include dynamic getters, setters, and builders or to omit
@@ -1125,11 +1135,13 @@ private void setTargetVersion() {
11251135

11261136
if (project.getProperties() != null && project.getProperties().containsKey("maven.compiler.source")) {
11271137
this.targetVersion = project.getProperties().get("maven.compiler.source").toString();
1138+
getLog().debug("Using maven.compiler.source to set targetVersion for generated sources (" + this.targetVersion + ")");
11281139
return;
11291140
}
11301141

11311142
if (project.getProperties() != null && project.getProperties().containsKey("maven.compiler.release")) {
11321143
this.targetVersion = project.getProperties().get("maven.compiler.release").toString();
1144+
getLog().debug("Using maven.compiler.release to set targetVersion for generated sources (" + this.targetVersion + ")");
11331145
return;
11341146
}
11351147

@@ -1138,18 +1150,21 @@ private void setTargetVersion() {
11381150
final Xpp3Dom compilerSourceConfig = ((Xpp3Dom) p.getConfiguration()).getChild("source");
11391151
if (compilerSourceConfig != null) {
11401152
this.targetVersion = compilerSourceConfig.getValue();
1153+
getLog().debug("Using maven-compiler-plugin 'source' to set targetVersion for generated sources (" + this.targetVersion + ")");
11411154
return;
11421155
}
11431156

11441157
final Xpp3Dom compilerReleaseConfig = ((Xpp3Dom) p.getConfiguration()).getChild("release");
11451158
if (compilerReleaseConfig != null) {
11461159
this.targetVersion = compilerReleaseConfig.getValue();
1160+
getLog().debug("Using maven-compiler-plugin 'release' to set targetVersion for generated sources (" + this.targetVersion + ")");
11471161
return;
11481162
}
11491163
}
11501164
}
11511165

11521166
this.targetVersion = JavaVersion.parse(System.getProperty("java.version"));
1167+
getLog().debug("Using JVM to set targetVersion for generated sources (" + this.targetVersion + ")");
11531168
}
11541169

11551170
@Override

0 commit comments

Comments
 (0)