在模块化项目中添加非模块化依赖。

huangapple go评论105阅读模式
英文:

Add non-modular dependancy in modular project

问题

我对模块化项目系统还比较陌生
在这个情境下我开始了一个JavaFX项目版本是17现在有一个`module-info.java`文件我以前从未听说过
有了更多的信息我开始理解它是如何工作的但我遇到了一个问题

在我的项目中我使用Maven并添加了这个依赖项
```xml
<dependency>
    <groupId>org.jetbrains</groupId>
    <artifactId>annotations</artifactId>
    <version>24.0.1</version>
    <scope>compile</scope>
</dependency>

我想在我的项目中使用@NotNull@Nullable等注解。但当我尝试使用它时,在运行时出现了以下错误:

java: 找不到符号
  符号:   类 NotNull
  位置: 类 be.machigan.dvdindexer.services.DVDFolder

所以我想在module-info.java文件中添加它,像这样:

module DVDIndexer {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.google.gson;
    requires org.jetbrains.annotations; // <- 这里

    opens be.machigan.dvdindexer to javafx.fxml, com.google.gson;
    opens be.machigan.dvdindexer.services to com.google.gson;
    exports be.machigan.dvdindexer;
    exports be.machigan.dvdindexer.controllers;
    opens be.machigan.dvdindexer.controllers to javafx.fxml;
}

但是当我运行它时,我得到了这个错误:

java: 未找到模块: org.jetbrains.annotations

在这个错误的情况下,IDE无法识别这些注解(就像上面的另一种情况)。

我在网上搜索了一下,了解到这个错误可能是因为依赖项没有module-info.class文件引起的。但我在依赖项的Github页面上搜索了一下,发现有一个module-info.java文件。这里

即使如此,在未来我也不知道如何添加一个没有module-info.java的依赖项。

提前感谢您的帮助!


<details>
<summary>英文:</summary>

I am pretty new to the system of the modular project. 
For the context, I began a JavaFX project in version 17 and I have now a `module-info.java` file that I never heard before.
With more informations I begin to understand how it works. But I have an issue.

In my project I use Maven and I add this dependency :
```xml
&lt;dependency&gt;
    &lt;groupId&gt;org.jetbrains&lt;/groupId&gt;
    &lt;artifactId&gt;annotations&lt;/artifactId&gt;
    &lt;version&gt;24.0.1&lt;/version&gt;
    &lt;scope&gt;compile&lt;/scope&gt;
&lt;/dependency&gt;

I want to use the @NotNull, @Nullable etc... annotations in my project. But when I try to use it I have this error when I run :

java: cannot find symbol
  symbol:   class NotNull
  location: class be.machigan.dvdindexer.services.DVDFolder

So I was thinking of add it to the module-info.java file like this :

module DVDIndexer {
    requires javafx.controls;
    requires javafx.fxml;
    requires com.google.gson;
    requires org.jetbrains.annotations; // &lt;- here

    opens be.machigan.dvdindexer to javafx.fxml, com.google.gson;
    opens be.machigan.dvdindexer.services to com.google.gson;
    exports be.machigan.dvdindexer;
    exports be.machigan.dvdindexer.controllers;
    opens be.machigan.dvdindexer.controllers to javafx.fxml;
}

But when I run it, I have this error :

java: module not found: org.jetbrains.annotations

And whith that error the annotations isn't recognized by the IDE (like the other situation above).

I have search on Internett and I have learn that the error can occurs because of the dependency doesn't have a module-info.class file. But I search on the Github of the dependency and there is a module-info.java. Here.

And even that, for the futur I don't know how to add a dependency that doesn't have a module-info.java.

Thanks in advance for the help !

答案1

得分: 1

你的问题与在模块化上下文中使用非模块化依赖项无关。正如你所发现的那样,问题中的依赖项确实是模块化的;它具有一个module-info描述符。而这个模块的名称是org.jetbrains.annotations

问题在于这个依赖项是一个编译时依赖项。你在POM文件中使用了<scope>compile</scope>明确标记了它为这种依赖项1。这意味着Maven会在编译时将依赖项放置在模块路径上,但不会运行时。这会导致错误,因为你写了以下代码:

requires org.jetbrians.annotations;

这告诉Java平台模块系统org.jetbrains.annotations模块在编译时运行时都是DVDIndexer模块所需的。要解决这个问题,你需要使用以下指令:

requires static org.jetbrains.annotations;

在这个上下文中,static关键字意味着所需的模块在运行时是可选的(但在编译时仍然需要)。这个特性的主要目的是允许一个模块在编译时要求另一个模块,而不会强制要求在运行时存在。换句话说,它允许存在只在编译时的依赖项。

1. 这是一件好事。java-annotations库旨在成为一个仅在编译时的依赖项。它的目的是提供更好的文档,并可能允许代码分析器更好地分析潜在错误。但库本身不定义运行时行为,所有的注解都具有CLASS的保留策略(这意味着它们不可通过反射看到)。因此,没有理由要求在运行时引入该依赖项。

英文:

Your problem is not related to using non-modular dependencies in a modular context. As you've discovered, the dependency in question is modular; it has a module-info descriptor. And the name of this module is org.jetbrains.annotations.

The problem is that the dependency is a compile-time only dependency. You've explicitly marked it as such by using &lt;scope&gt;compile&lt;/scope&gt; in your POM file<sup>1</sup>. That means Maven will place the dependency on the module-path at compile-time but not at run-time. This leads to an error because you have:

requires org.jetbrians.annotations;

That tells the Java Platform Module System that the org.jetbrains.annotations module is required by the DVDIndexer module at both compile-time and run-time. To solve this problem, you need to have the following directive instead:

requires static org.jetbrains.annotations;

The static keyword in this context means the required module is optional at run-time (but still required at compile-time). The main purpose of this feature is to allow a module to require another module at compile-time without forcing said other module to be present at run-time. In other words, it allows for compile-time only dependencies.


<sup>1. This is a good thing. The java-annotations library is designed to be a compile-time only dependency. Its purpose is to provide better documentation and possibly allow code analyzers to better analyze code for potential errors. But the library itself defines no run-time behavior, and the annotations all have a retention policy of CLASS (meaning they are not visible via reflection). Thus, there's no reason to require the dependency at run-time.</sup>

huangapple
  • 本文由 发表于 2023年7月20日 09:06:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定