Skip to content

Commit

Permalink
Add ability to add module-info.java to test code
Browse files Browse the repository at this point in the history
Add a test goal to the module-info-compiler plugin.
Rename goals to be consistent with maven-compiler-plugin
  • Loading branch information
jduo committed Dec 11, 2023
1 parent aae8aa7 commit 0aa8605
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.maven.plugins;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.glavo.mic.ModuleInfoCompiler;

/**
* Compiles the first module-info.java file in the project purely syntactically.
*/
public abstract class BaseModuleInfoCompilerPlugin extends AbstractMojo {
protected abstract List<String> getSourceRoots();

protected abstract boolean skip();

protected abstract String getOutputDirectory();

@Override
public void execute() throws MojoExecutionException {
if (skip()) {
getLog().info("Skipping module-info-compiler-maven-plugin");
return;
}

Optional<File> moduleInfoFile = findFirstModuleInfo(getSourceRoots());
if (moduleInfoFile.isPresent()) {
// The compiled module-info.class file goes into target/classes/module-info/main
Path outputDir = Paths.get(getOutputDirectory());

outputDir.toFile().mkdirs();
Path targetPath = outputDir.resolve("module-info.class");

// Invoke the compiler,
ModuleInfoCompiler compiler = new ModuleInfoCompiler();
try (Reader reader = new InputStreamReader(Files.newInputStream(moduleInfoFile.get().toPath()),
StandardCharsets.UTF_8);
OutputStream output = Files.newOutputStream(targetPath)) {
compiler.compile(reader, output);
getLog().info("Successfully wrote module-info.class file.");
} catch (IOException ex) {
throw new MojoExecutionException("Error compiling module-info.java", ex);
}
} else {
getLog().info("No module-info.java file found. module-info.class file was not generated.");
}
}

/**
* Finds the first module-info.java file in the set of source directories.
*/
private Optional<File> findFirstModuleInfo(List<String> sourceDirectories) {
if (sourceDirectories == null) {
return Optional.empty();
}

return sourceDirectories.stream().map(Paths::get)
.map(sourcePath ->
sourcePath.toFile().listFiles(file ->
file.getName().equals("module-info.java")))
.filter(matchingFiles -> matchingFiles != null && matchingFiles.length != 0)
.map(matchingFiles -> matchingFiles[0])
.findAny();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,20 @@

package org.apache.arrow.maven.plugins;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.glavo.mic.ModuleInfoCompiler;

/**
* Compiles the first module-info.java file in the project purely syntactically.
* A maven plugin for compiler module-info files in main code with JDK8.
*/
@Mojo(name = "module-info-compile", defaultPhase = LifecyclePhase.COMPILE)
public class ModuleInfoCompilerPlugin extends AbstractMojo {
/**
* Source directories.
*/
@Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE)
public class ModuleInfoCompilerPlugin extends BaseModuleInfoCompilerPlugin {

@Parameter(defaultValue = "${project.compileSourceRoots}", property = "compileSourceRoots",
required = true)
private final List<String> compileSourceRoots = new ArrayList<>();
Expand All @@ -57,49 +42,17 @@ public class ModuleInfoCompilerPlugin extends AbstractMojo {
private MavenProject project;

@Override
public void execute() throws MojoExecutionException {
if (skip) {
getLog().info("Skipping module-info-compiler-maven-plugin");
return;
}

Optional<File> moduleInfoFile = findFirstModuleInfo(compileSourceRoots);
if (moduleInfoFile.isPresent()) {
// The compiled module-info.class file goes into target/classes/module-info/main
Path outputDir = Paths.get(project.getBuild().getOutputDirectory());

outputDir.toFile().mkdirs();
Path targetPath = outputDir.resolve("module-info.class");

// Invoke the compiler,
ModuleInfoCompiler compiler = new ModuleInfoCompiler();
try (Reader reader = new InputStreamReader(Files.newInputStream(moduleInfoFile.get().toPath()),
StandardCharsets.UTF_8);
OutputStream output = Files.newOutputStream(targetPath)) {
compiler.compile(reader, output);
getLog().info("Successfully wrote module-info.class file.");
} catch (IOException ex) {
throw new MojoExecutionException("Error compiling module-info.java", ex);
}
} else {
getLog().info("No module-info.java file found. module-info.class file was not generated.");
}
protected List<String> getSourceRoots() {
return compileSourceRoots;
}

/**
* Finds the first module-info.java file in the set of source directories.
*/
private Optional<File> findFirstModuleInfo(List<String> sourceDirectories) {
if (sourceDirectories == null) {
return Optional.empty();
}
@Override
protected boolean skip() {
return skip;
}

return sourceDirectories.stream().map(Paths::get)
.map(sourcePath ->
sourcePath.toFile().listFiles(file ->
file.getName().equals("module-info.java")))
.filter(matchingFiles -> matchingFiles != null && matchingFiles.length != 0)
.map(matchingFiles -> matchingFiles[0])
.findAny();
@Override
protected String getOutputDirectory() {
return project.getBuild().getOutputDirectory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.arrow.maven.plugins;

import java.util.List;

import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

/**
* A maven plugin for compiler module-info files in unit tests with JDK8.
*/
@Mojo(name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE)
public class ModuleInfoTestCompilerPlugin extends BaseModuleInfoCompilerPlugin {

@Parameter(defaultValue = "false", property = "skip", required = false)
private boolean skip = false;

@Parameter(defaultValue = "${project}", readonly = true, required = true)
private MavenProject project;

@Override
protected List<String> getSourceRoots() {
return project.getTestCompileSourceRoots();
}

@Override
protected boolean skip() {
return skip;
}

@Override
protected String getOutputDirectory() {
return project.getBuild().getTestOutputDirectory();
}
}
10 changes: 9 additions & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,15 @@
<artifactId>module-info-compiler-maven-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<goals>
<goal>module-info-compile</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
Expand Down Expand Up @@ -413,6 +420,7 @@
<version>${maven-compiler-plugin.version}</version>
<configuration>
<excludes>**/module-info.java</excludes>
<testExcludes>**/module-info.java</testExcludes>
<useModulePath>false</useModulePath>
<annotationProcessorPaths>
<path>
Expand Down

0 comments on commit 0aa8605

Please sign in to comment.