Skip to content

Commit

Permalink
Adds a workflow to check built class release
Browse files Browse the repository at this point in the history
  • Loading branch information
fathzer committed Sep 3, 2024
1 parent 85b3215 commit 53131e9
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/java8Check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Check java class version
on:
push:
branches:
- "*"
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
name: Build and check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- name: Cache Maven packages
uses: actions/cache@v4
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build and check
run: |
mvn -P sonar package
cd java8Test
mvn package
echo "checking ../javaluator-demo/target/javaluator-demo.jar"
java -jar target/javaReleaseTest.jar ../javaluator-demo/target/javaluator-demo.jar 52
# Let check the library itself in case demo pom points to an older release of the library
OUT="$(ls ../javaluator/target/javaluator*.jar | grep -v javadoc.jar | grep -v sources.jar)"
echo "checking $OUT"
java -jar target/javaReleaseTest.jar $OUT 52
OUT="$(ls ../javaluator-examples/target/javaluator-examples*.jar | grep -v javadoc.jar | grep -v sources.jar)"
echo "checking $OUT"
java -jar target/javaReleaseTest.jar $OUT 52
1 change: 1 addition & 0 deletions java8Test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This folder contains a program used by 'java8Check.yml' workflow to check if generated classes are compatible with a java 8 (the exact release is defined in the workflow) virtual machine.
50 changes: 50 additions & 0 deletions java8Test/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>
<parent>
<groupId>com.fathzer</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.8</version>
<relativePath></relativePath>
</parent>
<artifactId>java-release-test</artifactId>
<version>0.0.1</version>
<name>java-release-test</name>
<description>A program that tests a jar contains classes compatible with a
specific java release.</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.source.skip>true</maven.source.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.install.skip>true</maven.install.skip>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>

<build>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<finalName>javaReleaseTest</finalName>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.fathzer.java.release.Check</mainClass>
</transformer>
</transformers>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
47 changes: 47 additions & 0 deletions java8Test/src/main/java/com/fathzer/java/release/Check.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.fathzer.java.release;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.function.Predicate;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Check {
private static final String CLASS_SUFFIX = ".class";

public static void main(String[] args) {
try {
if (args.length!=2) {
throw new IllegalArgumentException("This program requires two arguments: a jar file path and a maximum class level");
}
final File jar = new File(args[0]);
final int maxRelease = Integer.parseInt(args[1]);
new Check().check(jar, maxRelease, s->s.endsWith("-info.class"));
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

private void check(File jar, int maxRelease, Predicate<String> ignored) throws IOException {
try (ZipInputStream zip = new ZipInputStream(new FileInputStream(jar))) {
final DataInputStream data = new DataInputStream(zip);
for (ZipEntry entry = zip.getNextEntry(); entry!=null; entry = zip.getNextEntry()) {
if (!entry.isDirectory() && entry.getName().endsWith(CLASS_SUFFIX) && !ignored.test(entry.getName())) {
if (zip.skip(6)!=6) {
throw new IOException("End of file reached before major class version");
}
final int release = data.readUnsignedShort();
if (release>maxRelease) {
throw new IllegalArgumentException("The major class version of "+entry.getName()+" is "+release);
}
}
}
}
}


}

0 comments on commit 53131e9

Please sign in to comment.