Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use burningwave lib to export javac packages from module #139

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ For example, you may prefer that the `check` goal is performed in an earlier pha

`style` sets the formatter style to be _google_ or _aosp_. By default this is 'google'. Projects using Android conventions may prefer `aosp`.

`exportCompilerPackages` exports the `javac` packages needed from the `jdk.compiler` for the [google-java-format](https://github.com/google/google-java-format#jdk-16) to work with JDK 16+ with using additional JVM arguments to Maven.


example:
```xml
<build>
Expand All @@ -119,6 +122,7 @@ example:
<skip>false</skip>
<skipSortingImports>false</skipSortingImports>
<style>google</style>
<exportCompilerPackages>true</exportCompilerPackages>
</configuration>
<executions>
<execution>
Expand Down
15 changes: 5 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<artifactId>google-java-format</artifactId>
<version>1.14.0</version>
</dependency>
<dependency>
<groupId>org.burningwave</groupId>
<artifactId>core</artifactId>
<version>12.46.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down Expand Up @@ -236,15 +241,5 @@
</plugins>
</build>
</profile>
<profile>
<id>jdk17+</id>
<activation>
<jdk>[17,)</jdk>
</activation>
<properties>
<!-- TODO: Remove this once google-java-fmt supports 17 or the plugin supports forking and run with these args built-in -->
<argLine> --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</argLine>
</properties>
</profile>
</profiles>
</project>
19 changes: 19 additions & 0 deletions src/main/java/com/spotify/fmt/AbstractFMT.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.spotify.fmt;

import static org.burningwave.core.assembler.StaticComponentContainer.Modules;

import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.googlejavaformat.java.*;
Expand Down Expand Up @@ -57,6 +59,9 @@ public abstract class AbstractFMT extends AbstractMojo {
@Parameter(defaultValue = "false", property = "skipSortingImports")
private boolean skipSortingImports = false;

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

@Parameter(defaultValue = "google", property = "style")
private String style;

Expand All @@ -81,6 +86,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (skipSortingImports) {
getLog().info("Skipping sorting imports");
}
if (exportCompilerPackages) {
getLog().info("Exports javac packages from jdk.compiler module");
exportCompilerPackages();
}
List<File> directoriesToFormat = new ArrayList<>();
if (sourceDirectory.exists()) {
directoriesToFormat.add(sourceDirectory);
Expand Down Expand Up @@ -112,6 +121,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
postExecute(this.filesProcessed, this.nonComplyingFiles);
}

private void exportCompilerPackages() {
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.file");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.main");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.parser");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.tree");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.util");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.code");
Modules.exportPackageToAllUnnamed("jdk.compiler", "com.sun.tools.javac.api");
}

/**
* Post Execute action. It is called at the end of the execute method. Subclasses can add extra
* checks.
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/burningwave.static.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
managed-logger.repository.enabled=false
banner.hide=true
resource-releaser.enabled=false
14 changes: 14 additions & 0 deletions src/test/java/com/spotify/fmt/FMTTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class FMTTest {
private static String FORMAT = "format";
private static String CHECK = "check";

@Rule public MojoRule mojoRule = new MojoRule();

@Test
public void aaaaFirstTestThatAddsCompilerExports() throws Exception {
// Note: After this test has added compiler exports, it will work for the rest of the tests as
// well.
// However, adding a test before this that doesn't add exports, then all tests will fail?!
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("exportcompilerpackages"), FORMAT);
fmt.execute();

assertThat(fmt.getFilesProcessed()).hasSize(3);
}

@Test
public void noSource() throws Exception {
FMT fmt = (FMT) mojoRule.lookupConfiguredMojo(loadPom("nosource"), FORMAT);
Expand Down
50 changes: 50 additions & 0 deletions src/test/resources/exportcompilerpackages/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Test MyMojo</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.12</version>
<configuration>
<exportCompilerPackages>true</exportCompilerPackages>
</configuration>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>









Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorld1 {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package notestsource.src.main.java;

public class HelloWorldTest {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}