如何使用Runtime'范围可以有用(使用)?
由于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您知道在运行时环境中会实现实现时,您不想在开发环境中引用任何实现的任何特定
类
,Runtime
范围范围可能会有所帮助。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.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)假设您的项目被构建为-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.