In a Maven Build, I am dynamically generating some Java types using a byte code generation library (Byte Buddy). Naturally, these class files don't have corresponding source files. Only a few classes would be generated this way. The majority of the code for this project is going to be Java source. Ideally, the Java source would reference the generated types in a static way, rather than using reflection or runtime code generation, which means the classes need to be on the the compile class path for javac. Can I get the generated classes on the compile class path for the same Maven project i.e. without having a separate Maven project and artifact to hold the generated byte code referenced by the Maven project containing the source code?
UPDATE: I have already tried putting the generated classes directly into target/classes
i.e. project.build.outputDirectory
, early in the Maven Build Lifecycle, but it seems that is not on the class path. The generated types couldn't be resolved by the Maven Compiler Plugin or the IDE. I also tried using the Build Helper Maven Plugin to add an extra resources directory under target
containing the generated classes, which were then automatically copied into target/classes
. This configuration exhibited the same problem.
UPDATE: I have created a complete public repo on GitHub: https://github.com/mches/so-42376851
Here is the Maven POM for the project I want to have static classes that referenced byte code enhanced classes:
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>demo-enhanced</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>demo</groupId>
<artifactId>demo-original</artifactId>
<version>${project.version}</version>
<overWrite>true</overWrite>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<includes>**/*.class</includes>
<excludes>META-INF/</excludes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>transform</goal>
</goals>
<configuration>
<initialization>
<entryPoint>net.bytebuddy.test.SimpleEntryPoint</entryPoint>
<groupId>demo</groupId>
<artifactId>demo-transformer</artifactId>
</initialization>
<transformations>
<transformation>
<plugin>net.bytebuddy.test.SimplePlugin</plugin>
<groupId>demo</groupId>
<artifactId>demo-transformer</artifactId>
</transformation>
</transformations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and here is the Parent POM:
<?xml version="1.0" encoding="UTF-8"?>
<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>demo</groupId>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>demo-original</module>
<module>demo-transformer</module>
<module>demo-enhanced</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<byte-buddy.version>1.6.9</byte-buddy.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${byte-buddy.version}</version>
</dependency>
<dependency>
<groupId>demo</groupId>
<artifactId>demo-original</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>demo</groupId>
<artifactId>demo-transformer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>demo</groupId>
<artifactId>demo-enhanced</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-maven-plugin</artifactId>
<version>${byte-buddy.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
foo/Bar.java (original source):
package foo;
public class Bar {
}
net/bytebuddy/test/SimplePlugin.java (byte code enhancer):
package net.bytebuddy.test;
import net.bytebuddy.build.Plugin;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FieldAccessor;
public class SimplePlugin implements Plugin {
@Override
public boolean matches(TypeDescription target) {
return target.getName().equals("foo.Bar");
}
@Override
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription) {
if (typeDescription.getTypeName().equals("foo.Bar")) {
builder = builder.defineField("qux", String.class, Visibility.PRIVATE)
.defineMethod("getQux", String.class, Visibility.PUBLIC)
.intercept(FieldAccessor.ofField("qux"))
.defineMethod("setQux", void.class, Visibility.PUBLIC)
.withParameter(String.class)
.intercept(FieldAccessor.ofField("qux"));
}
return builder;
}
}
foo/Baz.java (static source file referencing dynamic type):
package foo;
public class Baz {
private Bar bar = new Bar();
public String getQuux() {
return bar.getQux();
}
public void setQuux(String quux) {
bar.setQux(quux);
}
}
UPDATE: Maven seems to understand a structure involving a consolidated module with the enhanced byte code and static class source code, as well as separate modules for each, but the IDEs, IntelliJ and Eclipse, fail to understand the class path for either structure the way Maven does.