1

Is there a proper or easy way of building one project for different JDK versions, the project would use different versions of its dependency depending on what JDK version it's built with.

I have a Java project that is built using JDK8, the project uses javax and its dependencies of course also uses JDK8. Now another project that uses this as a dependency is being upgraded to JDK 17, so that means I need to upgrade the dependency project to JDK 17 as well (I was getting compile errors due to javax), so I upgraded to jakarta and updated the dependencies to more recent versions (some now requiring JDK9 at minimum). This is all fine and I got everything working with JDK 17 and removed all traces of javax.

My dilemma though is this project is still actively updated and used as a dependency by other projects running on JDK8 (no chance of them being upgraded anytime soon) but since I upgraded it to JDK 17 they now get compile errors because of the jdk version difference. My only option now is to just publish 2 different versions, one jdk8 and one JDK 17.

The JDK8 would have dependencies with lower version because it's the final version compatible for JDK8, for example

dependecies {
    implementation: 'org.example:dependency-a:1.5.0'
}

But the JDK 17 version would be upgraded (assume I can't use the lower version due to compatibility issues)

dependecies {
    implementation: 'org.example:dependency-a:2.0.0'
}

Assuming all code stays the same for both version and it's just the dependencies that are different, is there a way for me to just have this on a single project and every time I build I would get a JDK8 and JDK 17 version of my project? My only other option is to just maintain 2 separate branches and update, build, and publish both when there are changes.

7
  • 1
    You can have a common base for compatible things, hence isolating javax/jakarta pains, but two branches for the incompatibilities is easiest IMHO. Commented Apr 30 at 14:58
  • 3
    "Assuming all code stays the same for both version and it's just the dependencies that are different" is not a reasonable. The package names appear at least in import statements in at least some of your source files, and they appear in the corresponding class files as well. It may be that you can isolate the classes that must differ so as to maintain different branches only for these, but it's unclear whether that would provide enough advantage to be worth it. Commented Apr 30 at 14:59
  • 3
    I believe that a generally accepted way to handle such a situation is JEP 14: The Tip & Tail Model of Library Development. In your source control system make branches for Java 8 and Java 17. They say it’s manageable. Commented Apr 30 at 18:55
  • It seems like your question is about Java EE vs Jakarta EE. You might want to take a look at Eclipse Transformer for that (though there are less and less reasons to continue supporting JDK 8 and it gets more expensive the longer you do so). For having different code for different JDKs, you can also use multi-release JARs. Commented May 1 at 6:57
  • 1
    baeldung.com/java-multi-release-jar Commented May 1 at 19:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.