Background:
We have maven
-based java project, which targets JRE 1.7, but the source code uses lambdas, so we use retrolambda
for transforming Java 8
source code to Java 7
. Also we use StreamSupport backport library when we need streams, function.*, Optional, etc.
Usage of retrolambda
involves configuring the project's both source and target language level to 1.8.
Everything works fine if there are no dependencies on java8 classes or methods (like java.util.stream.*
, java.util.Optional
, or methods introduced in java8
like Collection.forEach
). If there are such usages then build passes, but it fails in runtime, when running under JVM of Java 8.
Question:
My goal is to fail the build in case when such dependencies exist. Is there any way of detecting dependencies on new Java 8
classes/methods in build-time?
I thought about 2 possible options, but I'm not sure whether either of them is doable:
lint4j
) rules. Not sure whether it's possible to detect dependency on class/method using lint
You can use the Animal Sniffer Maven Plugin for this. It allows you to check that your code only uses APIs from a specified baseline (called the "signature"). In your case you'd use the org.codehaus.mojo.signature:java17:1.0 signature.
As others pointed out, you also could set up the bootstrap classpath, but that a) requires a JDK 7 to be set up and b) makes the build a bit more complex as you need to point to the JDK 7 install. Animal Sniffer is is much easier to work with in my experience.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With