Skip to content

Commit

Permalink
Improve discovering javaSource based on maven.compiler properties, de…
Browse files Browse the repository at this point in the history
…fault as 8

We can recognize based on properties:
- maven.compiler.release
- maven.compiler.source
- maven.compiler.target

fix #194
  • Loading branch information
slawekjaranowski committed Jan 1, 2024
1 parent 7518eb5 commit adc90dc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
* <p/>
* Will be discovered from project properties, in order:
* <ul>
* <li><code>maven.compiler.release</code></li>
* <li><code>maven.compiler.source</code></li>
* <li><code>maven.compiler.target</code></li>
* </ul>
*
* If all of above properties was not be set, default value as <b>8</b> will be used.
*
* @since 1.0
*/
@Parameter(defaultValue = "${maven.compiler.source}")
@Parameter
private String javaSource;

/**
Expand Down Expand Up @@ -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<String> release = () -> projectProperties.getProperty("maven.compiler.release");
Supplier<String> source = () -> projectProperties.getProperty("maven.compiler.source");
Supplier<String> target = () -> projectProperties.getProperty("maven.compiler.target");

Optional<String> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,8 @@ public void testModelloConvertersMojo() throws Exception {

mojo.setBuildContext(buildContext);

mojo.setProject(new MavenProject());

mojo.execute();

// ----------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,6 +66,7 @@ public void testModelloJavaMojo() throws Exception {
mojo.setModelloCore(modelloCore);

mojo.setBuildContext(buildContext);
mojo.setProject(new MavenProject());

mojo.execute();

Expand Down

0 comments on commit adc90dc

Please sign in to comment.