我需要使用Maven构建具有非模块化依赖项的JDK 9项目

编程入门 行业动态 更新时间:2024-10-28 12:29:51
本文介绍了我需要使用Maven构建具有非模块化依赖项的JDK 9项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的Java 9 SE项目,该项目与一个非模块化项目有一个依赖关系(本示例选择Weld SE),我正在尝试使用Maven(clean install)进行构建.为了使Java 9能够正常运行在中,我添加了module-info.java.最初,该文件仅包含模块名称,没有requires公式.

I have a simple Java 9 SE project with one dependency on a non-modularized project (chose Weld SE for this example) and I am trying to build it with Maven (clean install).In order for Java 9 to kick in, I have added module-info.java. Originally, this file only contained module name and had no requires formulas.

请记住,我唯一的依赖项不是模块化项目,因此我认为Maven将放入类路径(而不是模块路径)中,因此最终将以unnamed module的形式出现,如模块化系统的状态.

Please bear in mind that my sole dependency is NOT a modular project, therefore I presumed Maven will put in the classpath (not module-path) and hence it will end up in the unnamed module as described in State of the modular system.

现在,我的Maven版本是3.3.9,我知道我需要在3.6版中使用Maven编译器插件,如在此处描述当然,我已经下载了带有拼图的JDK 9 EA bu​​ild,并设置了Maven来使用它.

Now, my Maven version is 3.3.9 and I am aware that I need to use Maven compiler plugin in version 3.6, as described here Of course I have downloaded JDK 9 EA build with jigsaw and set Maven to use that.

如果我在不使用module-info.java的情况下构建项目,则一切正常,将所有内容添加到类路径中,并且构建成功.我想只要您不考虑该文件,Maven就会坚持使用旧方法.

If I build my project without module-info.java, everything works, stuff is added to classpath and build succeeds. I suppose Maven just sticks to the old ways as long as you leave out that file.

但是用 module-info.java构建它告诉我,依赖项中的类无法在类路径中找到.因此,我在调试模式下(带有-X)运行了Maven,实际上-所有jar都位于模块路径下,而classpath为空.这实际上意味着我所有的依赖都被转移到了自动模块 ,我需要在module-info.java中声明它们.

However building it with module-info.java tells me that the classes from my dependency cannot be found on the classpath. So I ran Maven in debug mode (with -X) and indeed - all jars are under module-path and classpath is empty. This effectively means that all my dependencies are transferred into automatic modules and I need to declare them in module-info.java.

一旦我声明自动模块要求(链接到项目的module-info),我能够在JDK 9上构建它.但这有点杂乱-我唯一的pom.xml依赖项是在weld-se-core上的,但是我的module-info要求我声明更高的要求才能通过编译.

Once I declare the automatic module requirements(link to projects's module-info), I am able to build it on JDK 9. But it is kind of messy - my only pom.xml dependecy is on weld-se-core, yet my module-info requires me to declare a lot more requirements for compilation to pass.

这是一个完整的 GitHub项目,在这里可以观察到所有这些情况.

Here is a whole GitHub project where all this can be observed.

所以我的问题是:

  • 如果我知道它们不是模块化的,我可以告诉Maven将一些工件放到classpath上吗?这样我就可以避免automatic module和声明它们的需要?
  • 如果我坚持使用automatic module,是否可以告诉Maven以可传递的方式允许我的依赖项需要引入的任何内容?例如. Weld的其他部分,CDI API等.
  • 为什么我要声明我的我的项目requires模块不直接使用的真正原因是什么?例如. weld.environmentmon
  • Can I tell Maven to put some artifacts on classpath if I know they are not modularized? So that I can avoid the automatic module and the need to declare them?
  • If I stick with automatic module, can I tell Maven to somehow transitively allow anything my dependency needs to bring in? E.g. other parts of Weld, CDI API etc.
  • What is the actual reason, why I need to state, that my project requires modules, which I do not use directly? E.g. weld.environmentmon
  • 推荐答案

    包括最近的一些更新,我将尝试并回答.

    Including few of the recent updates, I would try and answer this.

    更新

    • Java 9于2017年9月21日公开发布.
    • 最低兼容版本今天的maven-compiler-plugin中的3.7.0.

      • Java 9 was released publically on 21.09.2017.
      • The minimum compatible version of maven-compiler-plugin in today's date is 3.7.0.

        @Tunaki已经共享了有关如何配置它以构建 JDK 1.5至8和JDK 9 .

        As already shared by @Tunaki on how you can configure it to build compatible versions for both JDK 1.5 to 8 and JDK 9.

        根据问题和评论,您已经知道自动模块,并将所有模块依赖项放在模块路径上.

        Assuming from the question and comments, you are already aware of automatic modules and placing all the module dependencies on the module path.

        如果我知道它们不是模块化的,我可以告诉Maven在classpath上放置一些工件吗?这样我就可以避免使用自动模块,而无需声明它们了?

        Can I tell Maven to put some artifacts on classpath if I know they are not modularized? So that I can avoid the automatic module and the need to declare them?

        对于在maven pom <dependencies>中指定的工件以及当前模块的module-info.java中也不包含的工件,最终会以 未命名的模块.

        For artifacts specified in the maven pom <dependencies> and the ones which are as well not included in the module-info.java of the current module are ultimately left behind to be accessed from the classpath in form of an unnamed module.

        如果我坚持使用自动模块,是否可以告诉Maven以某种方式可传递地允许我的依赖项需要引入的任何内容?例如. Weld的其他部分,CDI API等.

        If I stick with automatic module, can I tell Maven to somehow transitively allow anything my dependency needs to bring in? E.g. other parts of Weld, CDI API etc.

        否,由于自动模块不包含明确声明的module-info.java,因此没有定义 requires transitive ,以获取本模块可能需要的任何传递依赖.

        No, since the automatic modules do not consist of an explicitly declared module-info.java, there is no such way of defining requires transitive for any transitive dependency that might be required by the present module.

        摘自我的过去的内容之一经验,该模块在编译时所需的dependency:tree中详述的任何Maven传递依赖都必须在当前项目的module-info中使用requires明确定义.

        From one of my past experiences, any Maven transitive dependency as detailed in the dependency:tree that is needed at compile time by the module would have to be explicitly defined using requires in the module-info of the current project.

        为什么我要声明我的项目需要模块而不直接使用的实际原因是什么?例如. weld.environmentmon **

        What is the actual reason, why I need to state, that my project requires modules, which I do not use directly? E.g. weld.environmentmon**

        • 您不需要为模块在编译或运行时不需要的模块指定requires.

          如果项目没有在运行时需要但在编译时需要的依赖,则需要确保使用 requires static .

          In case there is a dependency that is not required at runtime but required at compile time by your project, you would need to make sure that you define such module using requires static in your declarations.

          I would have preferably practiced a similar approach at present as well, as stated in the answer to should I keep or remove declared dependencies that are also transitive dependencies?

    更多推荐

    我需要使用Maven构建具有非模块化依赖项的JDK 9项目

    本文发布于:2023-11-26 06:13:56,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1632955.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:项目   Maven   JDK

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!