如何使用Runtime'范围可以有用(使用)?

发布于 2025-02-09 20:43:03 字数 817 浏览 1 评论 0原文

由于Maven依赖性的运行时范围使得无法从父级项目中“看到”此依赖关系,因此该如何有用?

示例父Maven Project POM:

...
<dependency>
  <groupId>org.example</groupId>
  <artifactId>example-dependency</artifactId>
  <version>1.0</version>
  <scope>runtime</scope?
</dependency>
...

据我了解,不是MAIN输入功能,或者父级项目中的任何类都可以在示例依赖性依赖性中引用任何内容。

根据 documentation> documentation ::

此范围表明不需要依赖项 汇编,但用于执行。 Maven包括 在运行时和测试类路径中的此范围,但不是编译 classPath。

但这并不能准确解释,这种范围如何有用。可以以某种方式在这种捆绑的依赖中以某种方式执行某些东西,而不会被父级应用程序引用吗?

将这种依赖捆绑到罐子里有什么意义?

Since runtime scope for maven dependency makes it impossible to "see" this dependency from parent project, how can it be useful?

Example parent maven project pom:

...
<dependency>
  <groupId>org.example</groupId>
  <artifactId>example-dependency</artifactId>
  <version>1.0</version>
  <scope>runtime</scope?
</dependency>
...

As i understand, not main entry function, nor any classes in parent project can reference anything in example-dependency.

According to the documentation:

This scope indicates that the dependency is not required for
compilation, but is for execution. Maven includes a dependency with
this scope in the runtime and test classpaths, but not the compile
classpath.

But this doesn't explain exactly, how such scope can be useful. Can something somehow be executed inside such bundled dependency on it's own, without being referenced by the parent app?

What is the point of bundling such dependency into the jar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2025-02-16 20:43:04

当您知道在运行时环境中会实现实现时,您不想在开发环境中引用任何实现的任何特定Runtime范围范围可能会有所帮助。

我露不在,API可以拥有一些通用类,实现可以扩展它们,然后是什么? ROOT应用不会看到那些扩展类

Java SPI(服务提供商界面)可以在您的环境(类Path)和Easilly中扫描实现。数据库驱动程序是常见的示例。 (java.sql.driver < / code> - &gt; mysql / postgres /任何数据库驱动程序)

When you know there will be an implementation at runtime environment, you don't want to refer any specific Class of any implementation at development environment, runtime scope could help.

As i understant, the api can have some generic classes, implementation can extend them and then what? The root app doesn't see those extending classes anyway

Java SPI (Service Provider Interface) could scan implementations in your environment (classpath) dynamically and easilly. Database drivers are common examples. (java.sql.Driver -> MySQL / Postgres / any database driver)

云裳 2025-02-16 20:43:04

假设您的项目被构建为-A.Jar
而且该a.jar取决于另一个库-B.Jar

两个罐子都取决于log4j.jar的记录目的。

在这种情况下,您将将log4j作为运行时依赖关系添加到B.Jar的POM中,以便无需任何问题就可以编译。
当通过其POM建立a.jar时,将包括log4j和b.jar。

这也将有助于避免运行时方法冲突。

Assume that you project is builded as - A.jar
and this A.jar is dependent on another library - B.jar

Both the jars are dependent on log4j.jar for logging purpose.

In this case you will be adding log4j as runtime dependency to B.jar's pom so that it will compile without any issues.
And when A.jar is builded from its pom the log4j and B.jar will be included.

This will also help to avoid runtime method conflict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文