Skip to content

Commit

Permalink
[sarl-maven-plugin] Add parameters for selecting the Java compiler an…
Browse files Browse the repository at this point in the history
…d setting the optimization level.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 19, 2018
1 parent fa40cf7 commit 9baec57
Show file tree
Hide file tree
Showing 11 changed files with 612 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.maven.plugins.annotations.Parameter;

import io.sarl.lang.SARLVersion;
import io.sarl.lang.compiler.batch.OptimizationLevel;

/** Abstract Mojo for compiling SARL (standard en test).
*
Expand Down Expand Up @@ -71,10 +72,19 @@ public abstract class AbstractCompileMojo extends AbstractSarlBatchCompilerMojo
private File tempDirectory;

/** Indicates if the Java compiler must be invoked by the SARL maven plugin.
* @deprecated see {@link #javaCompiler} for replacement.
*/
@Parameter(defaultValue = "true", required = false)
@Deprecated
private boolean runJavaCompiler;

/** Indicates the Java compiler to be invoked by the SARL maven plugin.
*/
@Parameter(required = false)
private String javaCompiler;

private JavaCompiler javaCompilerInstance;

/** Indicates if the inline annotations must be generated by the SARL maven plugin.
*/
@Parameter(defaultValue = "false", required = false)
Expand Down Expand Up @@ -131,6 +141,15 @@ public abstract class AbstractCompileMojo extends AbstractSarlBatchCompilerMojo
@Parameter(required = false)
private String[] extraGenerators;

/** Indicates the level of optimization.
*
* @since 0.8
*/
@Parameter(required = false)
private String optimization;

private OptimizationLevel optimizationInstance;

@Override
protected String[] getExtraGenerators() {
if (this.extraGenerators == null) {
Expand Down Expand Up @@ -159,8 +178,53 @@ protected File getTempDirectory() {
}

@Override
protected boolean getPostRunningOfJavaCompiler() {
return this.runJavaCompiler;
protected JavaCompiler getJavaCompiler() {
if (this.javaCompilerInstance == null) {
if (!Strings.isNullOrEmpty(this.javaCompiler)) {
this.javaCompilerInstance = JavaCompiler.valueOfCaseInsensitive(this.javaCompiler);
if (this.javaCompilerInstance == null) {
String values = null;
for (final JavaCompiler comp : JavaCompiler.values()) {
if (values == null) {
values = comp.getNameInMavenConfiguration();
} else {
values = MessageFormat.format(Messages.AbstractCompileMojo_1, values,
comp.getNameInMavenConfiguration());
}
}
getLog().warn(MessageFormat.format(Messages.AbstractCompileMojo_0, this.javaCompiler,
values, JavaCompiler.getDefault().getNameInMavenConfiguration()));
}
}
if (this.javaCompilerInstance == null) {
this.javaCompilerInstance = JavaCompiler.getDefault();
}
}
return this.javaCompilerInstance;
}

@Override
protected OptimizationLevel getOptimization() {
if (this.optimizationInstance == null) {
this.optimizationInstance = OptimizationLevel.valueOfCaseInsensitive(this.optimization);
if (this.optimizationInstance == null) {
String values = null;
for (final OptimizationLevel lvl : OptimizationLevel.values()) {
if (values == null) {
values = lvl.getCaseInsensitiveName();
} else {
values = MessageFormat.format(Messages.AbstractCompileMojo_1, values,
lvl.getCaseInsensitiveName());
}
}
getLog().warn(MessageFormat.format(Messages.AbstractCompileMojo_2, this.optimization,
values, OptimizationLevel.getDefault().getCaseInsensitiveName()));
}
if (this.optimizationInstance == null) {
this.optimizationInstance = OptimizationLevel.getDefault();
}
}
return this.optimizationInstance;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Binder;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -60,6 +64,8 @@
import org.slf4j.impl.StaticLoggerBinder;

import io.sarl.lang.SARLStandaloneSetup;
import io.sarl.lang.compiler.batch.IJavaBatchCompiler;
import io.sarl.lang.compiler.batch.OptimizationLevel;
import io.sarl.lang.compiler.batch.SarlBatchCompiler;

/** Abstract mojo that is able to use the SARL batch compiler.
Expand All @@ -86,7 +92,8 @@ public abstract class AbstractSarlBatchCompilerMojo extends AbstractSarlMojo {
@Override
public void prepareExecution() throws MojoExecutionException {
if (this.injector == null) {
this.injector = SARLStandaloneSetup.doSetup();
final Injector mainInjector = SARLStandaloneSetup.doSetup();
this.injector = mainInjector.createChildInjector(Arrays.asList(new MavenPrivateModule()));
}
if (this.sarlBatchCompilerProvider == null) {
this.sarlBatchCompilerProvider = this.injector.getProvider(SarlBatchCompiler.class);
Expand Down Expand Up @@ -127,12 +134,6 @@ protected MavenProject getProject() {
*/
protected abstract String getEncoding();

/** Replies if the Java compiler must be called at post-running stage.
*
* @return <code>true</code> for running the java compiler.
*/
protected abstract boolean getPostRunningOfJavaCompiler();

/** Replies if the inline annotations must be generated by the SARL compiler.
*
* @return <code>true</code> for generating the inline annotations.
Expand Down Expand Up @@ -192,6 +193,27 @@ protected MavenProject getProject() {
*/
protected abstract String[] getExtraGenerators();

/** Replies the Java compiler to use.
*
* @return the Java compiler, never {@code null}.
* @since 0.8
*/
protected abstract JavaCompiler getJavaCompiler();

/** Replies the optimization level to use.
*
* @return the optimization level, never {@code null}.
* @since 0.8
*/
protected abstract OptimizationLevel getOptimization();

/** Replies if the mojo is used within a test code compilation context.
*
* @return {@code true} if this mojo is used within a test phase.
* @since 0.8
*/
protected abstract boolean isTestContext();

/** Run compilation.
*
* @param classPath the classpath
Expand All @@ -214,7 +236,9 @@ protected void compile(List<File> classPath, List<File> sourcePaths, File sarlOu
return;
}
final String baseDir = project.getBasedir().getAbsolutePath();
compiler.setJavaPostCompilationEnable(getPostRunningOfJavaCompiler());
final JavaCompiler compilerType = getJavaCompiler();
compiler.setJavaPostCompilationEnable(compilerType != JavaCompiler.NONE);
compiler.setOptimizationLevel(getOptimization());
compiler.setClassOutputPath(classOutputPath);
compiler.setJavaSourceVersion(getSourceVersion());
compiler.setBasePath(baseDir);
Expand Down Expand Up @@ -444,4 +468,39 @@ protected List<File> getTestClassPath() throws MojoExecutionException {
return files;
}

/** Child injection module for the SARL maven plugin.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
private class MavenPrivateModule implements Module {

/**
* Constructor.
*/
MavenPrivateModule() {
//
}

@Override
public void configure(Binder binder) {
//
}

@Provides
@Singleton
public IJavaBatchCompiler providesJavaBatchCompiler(Injector injector) {
final JavaCompiler cmp = getJavaCompiler();
final IJavaBatchCompiler compiler = cmp.newCompilerInstance(getProject(),
AbstractSarlBatchCompilerMojo.this.mavenHelper,
isTestContext());
injector.injectMembers(compiler);
return compiler;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
package io.sarl.maven.compiler;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -47,12 +45,9 @@
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import org.codehaus.plexus.configuration.PlexusConfigurationException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.Xpp3DomUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import io.sarl.lang.SARLConfig;

Expand Down Expand Up @@ -87,27 +82,24 @@ public abstract class AbstractSarlMojo extends AbstractMojo {
@Component
private ResolutionErrorHandler resolutionErrorHandler;

/** Output directory.
/** The directory in which the Java code files are generated from the standard SARL code files.
*/
@Parameter
@Parameter(defaultValue = SARLConfig.FOLDER_SOURCE_GENERATED)
private File output;

/**
* Input directory.
/** The directory in which the standard SARL code files are located.
*/
@Parameter
@Parameter(defaultValue = SARLConfig.FOLDER_SOURCE_SARL)
private File input;

/**
* Output directory for tests.
/** The directory in which the Java code files are generated from the test SARL code files.
*/
@Parameter
@Parameter(defaultValue = SARLConfig.FOLDER_TEST_SOURCE_GENERATED)
private File testOutput;

/**
* Input directory for tests.
/** The directory in which the test SARL code files are located.
*/
@Parameter
@Parameter(defaultValue = SARLConfig.FOLDER_TEST_SOURCE_SARL)
private File testInput;

@Override
Expand Down Expand Up @@ -260,20 +252,11 @@ protected void executeMojo(

final Xpp3Dom mojoXml;
try {
mojoXml = toXpp3Dom(mojoDescriptor.getMojoConfiguration());
mojoXml = this.mavenHelper.toXpp3Dom(mojoDescriptor.getMojoConfiguration());
} catch (PlexusConfigurationException e1) {
throw new MojoExecutionException(e1.getLocalizedMessage(), e1);
}
Xpp3Dom configurationXml = null;
if (configuration != null && !configuration.isEmpty()) {
try (StringReader sr = new StringReader(configuration)) {
try {
configurationXml = Xpp3DomBuilder.build(sr);
} catch (XmlPullParserException | IOException e) {
getLog().debug(e);
}
}
}
Xpp3Dom configurationXml = this.mavenHelper.toXpp3Dom(configuration, getLog());
if (configurationXml != null) {
configurationXml = Xpp3DomUtils.mergeXpp3Dom(
configurationXml,
Expand All @@ -289,18 +272,6 @@ protected void executeMojo(
this.mavenHelper.executeMojo(execution);
}

private Xpp3Dom toXpp3Dom(PlexusConfiguration config) throws PlexusConfigurationException {
final Xpp3Dom result = new Xpp3Dom(config.getName());
result.setValue(config.getValue(null));
for (final String name : config.getAttributeNames()) {
result.setAttribute(name, config.getAttribute(name));
}
for (final PlexusConfiguration child : config.getChildren()) {
result.addChild(toXpp3Dom(child));
}
return result;
}

/** Extract the dependencies that are declared for a Maven plugin.
* This function reads the list of the dependencies in the configuration
* resource file with {@link MavenHelper#getConfig(String)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
requiresDependencyResolution = ResolutionScope.COMPILE)
public class CompileMojo extends AbstractCompileMojo {

@Override
protected boolean isTestContext() {
return false;
}

private boolean isValidSourceDirectory(File file, File outputDirectory) {
return !file.equals(outputDirectory) && !file.equals(getTestInput());
}
Expand Down
Loading

0 comments on commit 9baec57

Please sign in to comment.