Skip to content

Commit

Permalink
Post-process classes from 1.6 to 1.4 via antrun
Browse files Browse the repository at this point in the history
I don't want to make a Maven Mojo out of it, because it will also need
to work with Gradle. So a standalone Java tool is the right way to go.
  • Loading branch information
httpdigest committed Dec 31, 2016
1 parent 082e09a commit 72a2b0d
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
53 changes: 53 additions & 0 deletions buildhelper/Java6to4.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;

public class Java6to4 {

private static byte[] transform(InputStream classfile, ByteArrayOutputStream buffer) throws IOException {
buffer.reset();
int nRead;
byte[] data = new byte[1024];
while ((nRead = classfile.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
byte[] arr = buffer.toByteArray();
// patch byte 8
arr[7] = 48; // <- Java 1.4
return arr;
}

private static void walk(File root, ByteArrayOutputStream buffer) throws IOException {
File[] list = root.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return f.isDirectory() || name.endsWith(".class");
}
});
if (list == null)
return;
for (int i = 0; i < list.length; i++) {
File f = list[i];
if (f.isDirectory()) {
walk(f, buffer);
} else {
FileInputStream fis = new FileInputStream(f);
byte[] transformed = transform(fis, buffer);
fis.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(transformed);
fos.close();
}
}
}

public static void main(String[] args) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
walk(new File(args[0]), buffer);
}

}
65 changes: 65 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,71 @@
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<plugins>
<!--
Make sure the jdk9 profile is only used with a
1.9 JDK, and not forcefully enabled via -Pjdk9, because
antrun will not work on earlier JDKs, because of missing
tools.jar.
-->
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>1.9</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<!--
Use the antrun plugin to build and run a post-processing
Java application to rewrite the Java class file
version from 1.6 back to 1.4 in all compiled class files.
-->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<target>
<echo message="Post-process compiled class files..."></echo>
<mkdir dir="${project.build.directory}/buildhelper" />
<javac
srcdir="buildhelper"
destdir="${project.build.directory}/buildhelper"
includeantruntime="false">
<classpath refid="maven.plugin.classpath" />
</javac>
<java classname="Java6to4" classpathref="maven.plugin.classpath">
<classpath>
<pathelement location="${project.build.directory}/buildhelper" />
</classpath>
<arg value="${project.build.outputDirectory}" />
</java>
<echo message="Post-processing done."></echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!--
Expand Down

0 comments on commit 72a2b0d

Please sign in to comment.