Skip to content

Commit

Permalink
Build output must be consistent between reactor projects and dependen…
Browse files Browse the repository at this point in the history
…cies (#105)

The "bytecode" and "apt" tasks have been officially merged now - while
these separate output types were an attempt to keep generated sources
separate from bytecode, in order to have reactor projects produce
equivalent contents to the contents one might expect to find in
distributed jars.

This patch also modifies the maven goals from permitting
generated-sources/annotations source directories from being added as
source directories. Some basic logic is present to try to permit this
directory instead of allowing the BytecodeTask run annotation processors
(see SkipAptTask), but this is incomplete and doesn't work in reactor
tasks yet.

Output directories are also created at the beginning of goals now to
hopefully make it clear where output will be written to users while
waiting for compilation to complete.

Error from javac are improved, to include the filename and line number
with each error, if present.

Added an integration test project that includes an annotation processor
in the reactor so we can confirm it can build, and a project with
resources to ensure they can be found from other projects in the same
reactor.

Fixes #101
Fixes #14
  • Loading branch information
niloc132 authored Feb 11, 2022
1 parent 09c8517 commit ecdeb90
Show file tree
Hide file tree
Showing 31 changed files with 654 additions and 157 deletions.
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ that is incompatible for GWT will annotate appropriately with `@GwtIncompatible`
all annotation processors after stripping bytecode, which would require running on all artifacts from remote
repositories as well.

Reactor projects all have their generated content and bytecode built into a single directory, alongside original
sources, resources, and bytecode. This results in a directory that should reflect accurately unpacking a jar
which happens to include its own sources.


### Strip `@GwtIncompatible`
The J2CL-provided `JavaPreprocessor` class is used to process all sources before they are compiled, stripping out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,41 @@
*/
public interface OutputTypes {
/**
* A special output type to indicate to use project's own sources.
* A special output type to indicate to use project's own sources, or
* the contents of an external dependency's jar/zip file.
*/
String INPUT_SOURCES = "input_sources";

/**
* Annotation processor output.
* Represents the contents of a project if it were built into a jar
* file as an external dependency. Ostensibly should contain the
* un-stripped bytecode for a project and all of its resources (so
* that downstream projects can look for those resources on the
* classpath), but also presently ends up holding generated resources
* (so that the {@link #GENERATED_SOURCES} task can copy them out),
* and at that point it might as well contain the original Java
* sources too, unstripped. Including those sources however means
* that this becomes the source of truth for stripping sources,
* rather than the union of {@link #INPUT_SOURCES} and {@link #GENERATED_SOURCES}.
* This conflict arises since there could be .js files in the original
* sources, and we must copy them here since downstream projects
* could require them on the classpath - and after APT runs, we can't
* tell which sources were copied in and which came from sources,
* so we can't let downstream closure point to this (or generated
* sources) and input sources, it will find duplicate files.
*/
String GENERATED_SOURCES = "generated_sources";
String BYTECODE = "bytecode";

/**
* Bytecode from the original and generated sources. Not suitable
* for j2cl, only meant to be used for downstream generated sources
* tasks that need a compile classpath and will generate sources
* that also need to be stripped.
* Formerly annotation processor output.
*
* DISABLED FOR NOW - reintroducing this would require an intermediate
* output that would feed .java files to this, and everything else to
* {@link #BYTECODE} including source .js files. Taking this step may
* be necessary for better incremental builds.
*/
String BYTECODE = "bytecode";
@Deprecated
String GENERATED_SOURCES = "generated_sources";

/**
* Sources where the Java code has had GwtIncompatible members stripped.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<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>annotation-processor-in-reactor</groupId>
<artifactId>app</artifactId>
<version>1.0</version>
<packaging>war</packaging>

<dependencies>
<!-- Annotation processor and resources to use to generate output -->
<dependency>
<groupId>annotation-processor-in-reactor</groupId>
<artifactId>processor</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>annotation-processor-in-reactor</groupId>
<artifactId>resources</artifactId>
<version>${project.version}</version>
</dependency>

<!-- J2cl Test dependencies -->
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-annotations</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>junit-emul</artifactId>
<version>@j2cl.version@</version>
<classifier>sources</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vertispan.j2cl</groupId>
<artifactId>gwttestcase-emul</artifactId>
<version>@j2cl.version@</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>build-js</id>
<phase>prepare-package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>test-js</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<compilationLevel>ADVANCED</compilationLevel>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>google-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/google-snapshots/</url>
</repository>
</repositories>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface MyAnnotation {
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example;

public class MyApp {
public static void start() {
MyResources.INSTANCE.resourceInRoot();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example;

@MyAnnotation
public interface MyResources {
public static final MyResources INSTANCE = new MyResources_Impl();

@MyAnnotation("res-in-root-dir.txt")
String resourceInRoot();
@MyAnnotation("com/example/res-in-package.txt")
String resourceInPackage();

@MyAnnotation("res-in-java-default-package.txt")
String resourceInJavaSourceRoot();

@MyAnnotation("com/example/res-in-java-nested-package.txt")
String resourceInJavaPackage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<web-app></web-app>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example;

import com.google.j2cl.junit.apt.J2clTestInput;

import org.junit.Assert;
import org.junit.Test;

@J2clTestInput(ResourceTest.class)
public class ResourceTest {
@Test
public void resourceContents() {
// This test verifies that the resource contents were correctly read at build time
MyResources res = MyResources.INSTANCE;

Assert.assertEquals("res-in-root-dir.txt", res.resourceInRoot());
Assert.assertEquals("res-in-package.txt", res.resourceInPackage());
Assert.assertEquals("res-in-java-default-package.txt", res.resourceInJavaSourceRoot());
Assert.assertEquals("res-in-java-nested-package.txt", res.resourceInJavaPackage());
}
}
44 changes: 44 additions & 0 deletions j2cl-maven-plugin/src/it/annotation-processor-in-reactor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<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>annotation-processor-in-reactor</groupId>
<artifactId>annotation-processor-in-reactor</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<description>
This project tests not only if an annotation processor can be in the reactor (as a dependency of
the actual j2cl app), but also if the processor can correctly read files out of java and resources
directories of projects and dependencies.
</description>

<modules>
<module>processor</module>
<module>resources</module>
<module>app</module>
</modules>

<repositories>
<repository>
<id>google-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/google-snapshots/</url>
</repository>
</repositories>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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>

<parent>
<groupId>annotation-processor-in-reactor</groupId>
<artifactId>annotation-processor-in-reactor</artifactId>
<version>1.0</version>
</parent>
<artifactId>processor</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>javapoet</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>com.google.auto</groupId>
<artifactId>auto-common</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>none</proc>
</configuration>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit ecdeb90

Please sign in to comment.