diff --git a/modello-core/src/main/java/org/codehaus/modello/ModelloParameterConstants.java b/modello-core/src/main/java/org/codehaus/modello/ModelloParameterConstants.java
index 0c80a98f5..38fc0b711 100644
--- a/modello-core/src/main/java/org/codehaus/modello/ModelloParameterConstants.java
+++ b/modello-core/src/main/java/org/codehaus/modello/ModelloParameterConstants.java
@@ -51,6 +51,8 @@ public class ModelloParameterConstants {
*/
public static final String OUTPUT_JAVA_SOURCE = "modello.output.java.source";
+ public static final String OUTPUT_JAVA_SOURCE_DEFAULT = "8";
+
public static final String OUTPUT_XDOC_FILE_NAME = "modello.output.xdoc.file";
public static final String OUTPUT_XSD_FILE_NAME = "modello.output.xsd.file";
diff --git a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojo.java b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojo.java
index e6021ed62..a107bd851 100644
--- a/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojo.java
+++ b/modello-maven-plugin/src/main/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojo.java
@@ -23,7 +23,10 @@
*/
import java.io.File;
+import java.util.Optional;
import java.util.Properties;
+import java.util.function.Supplier;
+import java.util.stream.Stream;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.modello.ModelloParameterConstants;
@@ -47,10 +50,20 @@ public abstract class AbstractModelloSourceGeneratorMojo extends AbstractModello
private String encoding;
/**
- * Generate Java 5 sources, with generic collections.
+ * The java source level used for generating outputs classes.
+ *
+ * Will be discovered from project properties, in order:
+ *
+ * maven.compiler.release
+ * maven.compiler.source
+ * maven.compiler.target
+ *
+ *
+ * If all of above properties was not be set, default value as 8 will be used.
+ *
* @since 1.0
*/
- @Parameter(defaultValue = "${maven.compiler.source}")
+ @Parameter
private String javaSource;
/**
@@ -85,9 +98,33 @@ protected void customizeParameters(Properties parameters) {
if (javaSource.startsWith("1.")) {
javaSource = javaSource.substring("1.".length());
}
- parameters.setProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, javaSource);
+ } else {
+ javaSource = discoverJavaSource();
}
+ getLog().debug("javaSource=" + javaSource);
+ parameters.setProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, javaSource);
parameters.setProperty(ModelloParameterConstants.DOM_AS_XPP3, Boolean.toString(domAsXpp3));
}
+
+ private String discoverJavaSource() {
+ Properties projectProperties = getProject().getProperties();
+
+ Supplier release = () -> projectProperties.getProperty("maven.compiler.release");
+ Supplier source = () -> projectProperties.getProperty("maven.compiler.source");
+ Supplier target = () -> projectProperties.getProperty("maven.compiler.target");
+
+ Optional jSource = Stream.of(release, source, target)
+ .map(Supplier::get)
+ .filter(s -> s != null && !s.isEmpty())
+ .findFirst();
+
+ if (jSource.isPresent()) {
+ return jSource.get();
+ } else {
+ getLog().warn("javaSource was not discovered - use default value "
+ + ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT);
+ return ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT;
+ }
+ }
}
diff --git a/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojoTest.java b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojoTest.java
new file mode 100644
index 000000000..b2e5de849
--- /dev/null
+++ b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/AbstractModelloSourceGeneratorMojoTest.java
@@ -0,0 +1,78 @@
+package org.codehaus.modello.maven;
+
+import java.util.Properties;
+
+import org.apache.maven.model.Model;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.modello.ModelloParameterConstants;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class AbstractModelloSourceGeneratorMojoTest {
+
+ private class ModelloSourceGeneratorMojoTest extends AbstractModelloSourceGeneratorMojo {
+
+ private final Properties projectProperties;
+
+ ModelloSourceGeneratorMojoTest(Properties projectProperties) {
+ this.projectProperties = projectProperties;
+ }
+
+ @Override
+ protected String getGeneratorType() {
+ return null;
+ }
+
+ @Override
+ public MavenProject getProject() {
+ Model model = new Model();
+ model.setProperties(projectProperties);
+ MavenProject project = new MavenProject();
+ project.setModel(model);
+ return project;
+ }
+ }
+
+ private void executeJavaSourceTest(Properties projectProperties, String expexted) {
+ ModelloSourceGeneratorMojoTest modelloSourceGeneratorMojoTest =
+ new ModelloSourceGeneratorMojoTest(projectProperties);
+ Properties properties = new Properties();
+
+ modelloSourceGeneratorMojoTest.customizeParameters(properties);
+
+ assertEquals(properties.getProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE), expexted);
+ }
+
+ @Test
+ public void testJavaSourceDefault() {
+ executeJavaSourceTest(new Properties(), ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT);
+ }
+
+ @Test
+ public void testJavaSourceFromRelease() {
+ Properties projectProperties = new Properties();
+ projectProperties.setProperty("maven.compiler.release", "11");
+ projectProperties.setProperty("maven.compiler.source", "xxx");
+ projectProperties.setProperty("maven.compiler.target", "xxx");
+
+ executeJavaSourceTest(projectProperties, "11");
+ }
+
+ @Test
+ public void testJavaSourceFromSource() {
+ Properties projectProperties = new Properties();
+ projectProperties.setProperty("maven.compiler.source", "11");
+ projectProperties.setProperty("maven.compiler.target", "xxx");
+
+ executeJavaSourceTest(projectProperties, "11");
+ }
+
+ @Test
+ public void testJavaSourceFromTarget() {
+ Properties projectProperties = new Properties();
+ projectProperties.setProperty("maven.compiler.target", "11");
+
+ executeJavaSourceTest(projectProperties, "11");
+ }
+}
diff --git a/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloConvertersMojoTest.java b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloConvertersMojoTest.java
index 779f178dd..92adde1f5 100644
--- a/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloConvertersMojoTest.java
+++ b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloConvertersMojoTest.java
@@ -25,6 +25,7 @@
import java.io.File;
import java.util.Arrays;
+import org.apache.maven.project.MavenProject;
import org.codehaus.modello.core.ModelloCore;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.PlexusTestCase;
@@ -66,6 +67,8 @@ public void testModelloConvertersMojo() throws Exception {
mojo.setBuildContext(buildContext);
+ mojo.setProject(new MavenProject());
+
mojo.execute();
// ----------------------------------------------------------------------
diff --git a/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloJavaMojoTest.java b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloJavaMojoTest.java
index f15b4031a..ef234cac5 100644
--- a/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloJavaMojoTest.java
+++ b/modello-maven-plugin/src/test/java/org/codehaus/modello/maven/ModelloJavaMojoTest.java
@@ -25,6 +25,7 @@
import java.io.File;
import java.util.Arrays;
+import org.apache.maven.project.MavenProject;
import org.codehaus.modello.core.ModelloCore;
import org.codehaus.plexus.ContainerConfiguration;
import org.codehaus.plexus.PlexusTestCase;
@@ -65,6 +66,7 @@ public void testModelloJavaMojo() throws Exception {
mojo.setModelloCore(modelloCore);
mojo.setBuildContext(buildContext);
+ mojo.setProject(new MavenProject());
mojo.execute();
diff --git a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java
index 1446a0131..4e1f39514 100644
--- a/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java
+++ b/modello-plugins/modello-plugin-java/src/main/java/org/codehaus/modello/plugin/java/AbstractJavaModelloGenerator.java
@@ -63,15 +63,20 @@
* @author Joakim Erdfelt
*/
public abstract class AbstractJavaModelloGenerator extends AbstractModelloGenerator {
- private Optional javaSource;
+ private Integer javaSource;
protected boolean domAsXpp3 = true;
+ @Override
protected void initialize(Model model, Properties parameters) throws ModelloException {
super.initialize(model, parameters);
- javaSource = Optional.ofNullable(getParameter(parameters, ModelloParameterConstants.OUTPUT_JAVA_SOURCE, null))
- .map(Integer::valueOf);
+ javaSource = Optional.ofNullable(getParameter(
+ parameters,
+ ModelloParameterConstants.OUTPUT_JAVA_SOURCE,
+ ModelloParameterConstants.OUTPUT_JAVA_SOURCE_DEFAULT))
+ .map(Integer::valueOf)
+ .orElseThrow(() -> new ModelloException("javaSource must be set"));
domAsXpp3 = !"false".equals(parameters.getProperty(ModelloParameterConstants.DOM_AS_XPP3));
}
@@ -111,7 +116,7 @@ protected void initHeader(JInterface interfaze) {
}
protected final boolean hasJavaSourceSupport(int source) {
- return javaSource.map(i -> i >= source).orElse(false);
+ return javaSource >= source;
}
protected void suppressAllWarnings(Model objectModel, JStructure structure) {
diff --git a/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/AnnotationsJava4GeneratorTest.java b/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/AnnotationsJava4GeneratorTest.java
index 7ed30ebdf..e7b55f000 100644
--- a/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/AnnotationsJava4GeneratorTest.java
+++ b/modello-plugins/modello-plugin-java/src/test/java/org/codehaus/modello/plugin/java/AnnotationsJava4GeneratorTest.java
@@ -25,6 +25,7 @@
import java.util.Properties;
import org.codehaus.modello.AbstractModelloJavaGeneratorTest;
+import org.codehaus.modello.ModelloParameterConstants;
import org.codehaus.modello.core.ModelloCore;
import org.codehaus.modello.model.Model;
@@ -42,6 +43,8 @@ public void testJava4GeneratorWithAnnotations() throws Throwable {
Model model = modello.loadModel(getXmlResourceReader("/models/annotations.mdo"));
Properties parameters = getModelloParameters("1.0.0");
+ // test assume that javaSource below 5 is used
+ parameters.setProperty(ModelloParameterConstants.OUTPUT_JAVA_SOURCE, "4");
modello.generate(model, "java", parameters);