Is there a way to add maven dependencies while using the maven-jlink-plugin?

谁说我不能喝 提交于 2019-11-27 02:06:32

This has not much to do with the plugin I believe. Module joda.time in your case seems to be an automatic module.

The jlink tool does not support linking of automatic modules because they can rely on the arbitrary content of the classpath, which goes against the idea of a self-contained Java runtime.

So there are two ways to fix this probably :-

  • (you don't own the jar) Temporarily go ahead create a module-info.java[you could use jdeps tool for it] and update the jar[using jar tool] with the corresponding compiled class as in projects under Java 9.

  • (you own the dependency) Permanently migrate the jar to Java 9 itself, where it would consist of the module-info.class by itself after being compiled and packaged.

You could use the ModiTect Maven plug-in to add a module descriptor to the JAR and create a modular runtime image with that module.

Disclaimer: I'm the author of ModiTect.

Let me try to explain the technical background on this a little bit:

joda-time in the version 2.9.9 as given in the question is a non-modular jar it does not contain a module-info.class and it does not declare itself as an automatic module by using a Automatic-Module-Name: in its META-INF/MANIFEST.MF

Classes in a real module can not call classes on the classpath thus you can not use the "requires jode.time" in your module-info with that version.

Automatic Modules can use classes on the ClassPath. In order to use Classes from an non-modular jar in a real module you can use a wrapper which itself is an automatic module.

By the time now there exists a version 2.10.1 of joda-time which is an automatic module and declares "Automatic-Module-Name: org.joda.time" thus you can use an "requires org.joda.time" in your real module with that new version.

Now the new jlink tool can not operate directly with non-modular jars or automatic jars and the maven-jlink plugin currently is basically just a maven wrapper around the jlink tool translating its configuration parameters into arguments for the jlink tool.

In order to create a custom Java Runtime for projects using mixed maven dependencies with real modules, automatic modules and non-modular jars you have to know all system module dependencies of your real-modules, automatic-modules and non-modular jars and than use a --add-modules parameter to jlink with these.

To collect the system module dependencies you can use the new jdeps tool with either the --print-module-deps or --list-deps parameter depending on the actual JDK Version being used.

If you are using maven for your project you can automate that task by creating or using a maven plugin that does this for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!