Skip to content

Commit 0aa8605

Browse files
committed
Add ability to add module-info.java to test code
Add a test goal to the module-info-compiler plugin. Rename goals to be consistent with maven-compiler-plugin
1 parent aae8aa7 commit 0aa8605

File tree

4 files changed

+167
-61
lines changed

4 files changed

+167
-61
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.maven.plugins;
19+
20+
import java.io.File;
21+
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.io.OutputStream;
24+
import java.io.Reader;
25+
import java.nio.charset.StandardCharsets;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.List;
30+
import java.util.Optional;
31+
32+
import org.apache.maven.plugin.AbstractMojo;
33+
import org.apache.maven.plugin.MojoExecutionException;
34+
import org.glavo.mic.ModuleInfoCompiler;
35+
36+
/**
37+
* Compiles the first module-info.java file in the project purely syntactically.
38+
*/
39+
public abstract class BaseModuleInfoCompilerPlugin extends AbstractMojo {
40+
protected abstract List<String> getSourceRoots();
41+
42+
protected abstract boolean skip();
43+
44+
protected abstract String getOutputDirectory();
45+
46+
@Override
47+
public void execute() throws MojoExecutionException {
48+
if (skip()) {
49+
getLog().info("Skipping module-info-compiler-maven-plugin");
50+
return;
51+
}
52+
53+
Optional<File> moduleInfoFile = findFirstModuleInfo(getSourceRoots());
54+
if (moduleInfoFile.isPresent()) {
55+
// The compiled module-info.class file goes into target/classes/module-info/main
56+
Path outputDir = Paths.get(getOutputDirectory());
57+
58+
outputDir.toFile().mkdirs();
59+
Path targetPath = outputDir.resolve("module-info.class");
60+
61+
// Invoke the compiler,
62+
ModuleInfoCompiler compiler = new ModuleInfoCompiler();
63+
try (Reader reader = new InputStreamReader(Files.newInputStream(moduleInfoFile.get().toPath()),
64+
StandardCharsets.UTF_8);
65+
OutputStream output = Files.newOutputStream(targetPath)) {
66+
compiler.compile(reader, output);
67+
getLog().info("Successfully wrote module-info.class file.");
68+
} catch (IOException ex) {
69+
throw new MojoExecutionException("Error compiling module-info.java", ex);
70+
}
71+
} else {
72+
getLog().info("No module-info.java file found. module-info.class file was not generated.");
73+
}
74+
}
75+
76+
/**
77+
* Finds the first module-info.java file in the set of source directories.
78+
*/
79+
private Optional<File> findFirstModuleInfo(List<String> sourceDirectories) {
80+
if (sourceDirectories == null) {
81+
return Optional.empty();
82+
}
83+
84+
return sourceDirectories.stream().map(Paths::get)
85+
.map(sourcePath ->
86+
sourcePath.toFile().listFiles(file ->
87+
file.getName().equals("module-info.java")))
88+
.filter(matchingFiles -> matchingFiles != null && matchingFiles.length != 0)
89+
.map(matchingFiles -> matchingFiles[0])
90+
.findAny();
91+
}
92+
}

java/maven/module-info-compiler-maven-plugin/src/main/java/org/apache/arrow/maven/plugins/ModuleInfoCompilerPlugin.java

+13-60
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,20 @@
1717

1818
package org.apache.arrow.maven.plugins;
1919

20-
import java.io.File;
21-
import java.io.IOException;
22-
import java.io.InputStreamReader;
23-
import java.io.OutputStream;
24-
import java.io.Reader;
25-
import java.nio.charset.StandardCharsets;
26-
import java.nio.file.Files;
27-
import java.nio.file.Path;
28-
import java.nio.file.Paths;
2920
import java.util.ArrayList;
3021
import java.util.List;
31-
import java.util.Optional;
3222

33-
import org.apache.maven.plugin.AbstractMojo;
34-
import org.apache.maven.plugin.MojoExecutionException;
3523
import org.apache.maven.plugins.annotations.LifecyclePhase;
3624
import org.apache.maven.plugins.annotations.Mojo;
3725
import org.apache.maven.plugins.annotations.Parameter;
3826
import org.apache.maven.project.MavenProject;
39-
import org.glavo.mic.ModuleInfoCompiler;
4027

4128
/**
42-
* Compiles the first module-info.java file in the project purely syntactically.
29+
* A maven plugin for compiler module-info files in main code with JDK8.
4330
*/
44-
@Mojo(name = "module-info-compile", defaultPhase = LifecyclePhase.COMPILE)
45-
public class ModuleInfoCompilerPlugin extends AbstractMojo {
46-
/**
47-
* Source directories.
48-
*/
31+
@Mojo(name = "compile", defaultPhase = LifecyclePhase.COMPILE)
32+
public class ModuleInfoCompilerPlugin extends BaseModuleInfoCompilerPlugin {
33+
4934
@Parameter(defaultValue = "${project.compileSourceRoots}", property = "compileSourceRoots",
5035
required = true)
5136
private final List<String> compileSourceRoots = new ArrayList<>();
@@ -57,49 +42,17 @@ public class ModuleInfoCompilerPlugin extends AbstractMojo {
5742
private MavenProject project;
5843

5944
@Override
60-
public void execute() throws MojoExecutionException {
61-
if (skip) {
62-
getLog().info("Skipping module-info-compiler-maven-plugin");
63-
return;
64-
}
65-
66-
Optional<File> moduleInfoFile = findFirstModuleInfo(compileSourceRoots);
67-
if (moduleInfoFile.isPresent()) {
68-
// The compiled module-info.class file goes into target/classes/module-info/main
69-
Path outputDir = Paths.get(project.getBuild().getOutputDirectory());
70-
71-
outputDir.toFile().mkdirs();
72-
Path targetPath = outputDir.resolve("module-info.class");
73-
74-
// Invoke the compiler,
75-
ModuleInfoCompiler compiler = new ModuleInfoCompiler();
76-
try (Reader reader = new InputStreamReader(Files.newInputStream(moduleInfoFile.get().toPath()),
77-
StandardCharsets.UTF_8);
78-
OutputStream output = Files.newOutputStream(targetPath)) {
79-
compiler.compile(reader, output);
80-
getLog().info("Successfully wrote module-info.class file.");
81-
} catch (IOException ex) {
82-
throw new MojoExecutionException("Error compiling module-info.java", ex);
83-
}
84-
} else {
85-
getLog().info("No module-info.java file found. module-info.class file was not generated.");
86-
}
45+
protected List<String> getSourceRoots() {
46+
return compileSourceRoots;
8747
}
8848

89-
/**
90-
* Finds the first module-info.java file in the set of source directories.
91-
*/
92-
private Optional<File> findFirstModuleInfo(List<String> sourceDirectories) {
93-
if (sourceDirectories == null) {
94-
return Optional.empty();
95-
}
49+
@Override
50+
protected boolean skip() {
51+
return skip;
52+
}
9653

97-
return sourceDirectories.stream().map(Paths::get)
98-
.map(sourcePath ->
99-
sourcePath.toFile().listFiles(file ->
100-
file.getName().equals("module-info.java")))
101-
.filter(matchingFiles -> matchingFiles != null && matchingFiles.length != 0)
102-
.map(matchingFiles -> matchingFiles[0])
103-
.findAny();
54+
@Override
55+
protected String getOutputDirectory() {
56+
return project.getBuild().getOutputDirectory();
10457
}
10558
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.arrow.maven.plugins;
19+
20+
import java.util.List;
21+
22+
import org.apache.maven.plugins.annotations.LifecyclePhase;
23+
import org.apache.maven.plugins.annotations.Mojo;
24+
import org.apache.maven.plugins.annotations.Parameter;
25+
import org.apache.maven.project.MavenProject;
26+
27+
/**
28+
* A maven plugin for compiler module-info files in unit tests with JDK8.
29+
*/
30+
@Mojo(name = "testCompile", defaultPhase = LifecyclePhase.TEST_COMPILE)
31+
public class ModuleInfoTestCompilerPlugin extends BaseModuleInfoCompilerPlugin {
32+
33+
@Parameter(defaultValue = "false", property = "skip", required = false)
34+
private boolean skip = false;
35+
36+
@Parameter(defaultValue = "${project}", readonly = true, required = true)
37+
private MavenProject project;
38+
39+
@Override
40+
protected List<String> getSourceRoots() {
41+
return project.getTestCompileSourceRoots();
42+
}
43+
44+
@Override
45+
protected boolean skip() {
46+
return skip;
47+
}
48+
49+
@Override
50+
protected String getOutputDirectory() {
51+
return project.getBuild().getTestOutputDirectory();
52+
}
53+
}

java/pom.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,15 @@
377377
<artifactId>module-info-compiler-maven-plugin</artifactId>
378378
<executions>
379379
<execution>
380+
<id>default-compile</id>
380381
<goals>
381-
<goal>module-info-compile</goal>
382+
<goal>compile</goal>
383+
</goals>
384+
</execution>
385+
<execution>
386+
<id>default-testCompile</id>
387+
<goals>
388+
<goal>testCompile</goal>
382389
</goals>
383390
</execution>
384391
</executions>
@@ -413,6 +420,7 @@
413420
<version>${maven-compiler-plugin.version}</version>
414421
<configuration>
415422
<excludes>**/module-info.java</excludes>
423+
<testExcludes>**/module-info.java</testExcludes>
416424
<useModulePath>false</useModulePath>
417425
<annotationProcessorPaths>
418426
<path>

0 commit comments

Comments
 (0)