这些Maven依赖范围之间有什么区别:provided / compile / system / import

时间:2021-07-30 13:31:02

I have read the documentation and have some understanding.

我已阅读文档并有一些了解。

Please correct or inform me of the truth; as per my understanding:

请更正或告知我真相;根据我的理解:

  • provided
    The dependencies must will be on the machine you run the code on, and must be included in the path

    提供依赖项必须位于运行代码的计算机上,并且必须包含在路径中

  • compile
    The dependencies will not be on the machine that runs the code, so include them in the build

    编译依赖项不在运行代码的机器上,因此将它们包含在构建中

  • system
    Exactly the same as provided, but you need the dependencies to be present in a jar file strictly

    system与提供的完全相同,但您需要严格依赖于jar文件中的依赖项

  • import
    Seems like it should import the dependencies from some other POM file but I don't know how/why, so a little elaboration would be appreciated

    import似乎应该从其他POM文件导入依赖项,但我不知道如何/为什么,所以一点点阐述将不胜感激

2 个解决方案

#1


47  

You are wrong/ambiguous about provided. It means, "This jar should be compiled against locally, but it will be provided on the classpath by something else during runtime, so don't include it in the classpath for me." For example, all web containers (eg: tomcat) include the jars for servlets. You should use provided for the servlet classes so you can compile your code locally, but you don't want to override the servlet classes that tomcat provides for you when you deploy to it.

你提供的错误/含糊不清。这意味着,“这个jar应该在本地编译,但它会在运行时通过其他东西在类路径中提供,所以不要把它包含在类路径中。”例如,所有Web容器(例如:tomcat)都包含servlet的jar。您应该使用为servlet类提供的代码,以便可以在本地编译代码,但是您不希望覆盖tomcat在部署时为您提供的servlet类。


system means, "These dependencies are on my system and I want to point to them directly". You want to avoid this if you can, because another person on another computer won't necessarily have these dependencies.

系统意味着,“这些依赖关系在我的系统上,我想直接指向它们”。如果可以,您希望避免这种情况,因为另一台计算机上的另一个人不一定具有这些依赖关系。

The difference between provided is easier to show:

提供的差异更容易显示:

<dependency>
  <groupId>javax.sql</groupId>
  <artifactId>jdbc-stdext</artifactId>
  <version>2.0</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

See how it has that <systemPath>? That's the difference. You don't specify the path with provided, provided knows how to get the dependency from a repository. system gets it from your file system only.

看看它是如何有 的?这就是区别。如果知道如何从存储库获取依赖关系,则不要指定提供的路径。系统仅从您的文件系统获取它。


I've never even heard of import. @JigarJoshi linked to http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html which says

我从来没有听说过导入。 @JigarJoshi链接到http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

import (only available in Maven 2.0.9 or later)

导入(仅适用于Maven 2.0.9或更高版本)

This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

此范围仅用于 部分中类型为pom的依赖项。它表示应该用该POM部分中的依赖项替换指定的POM。由于它们被替换,具有导入范围的依赖性实际上不参与限制依赖性的传递性。

I think this is saying, "take all the dependencies this project has and inline them in this <dependencyManagement> section." Someone correct me if I'm wrong.

我想这就是说,“获取这个项目的所有依赖项,并在这个 部分中内联它们。”如果我错了,有人会纠正我。

#2


16  

Please find the exact meaning of scopes in Maven

请在Maven中找到范围的确切含义

I refered to Maven http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

我参考了Maven http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

依赖关系范围用于限制依赖关系的传递性,还用于影响用于各种构建任务的类路径。

There are 6 scopes available:

有6个范围:

  • compile:

    编译:

    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

    这是默认范围,如果未指定,则使用此范围。编译依赖项在项目的所有类路径中都可用。此外,这些依赖项将传播到依赖项目。

  • provided:

    提供:

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

    这很像compile,但表示您希望JDK或容器在运行时提供依赖性。例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖关系设置为提供的范围,因为Web容器提供了这些类。此范围仅在编译和测试类路径中可用,并且不可传递。

  • runtime:

    运行:

    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

    此范围表示编译不需要依赖项,但是用于执行。它位于运行时和测试类路径中,但不是编译类路径。

  • test:

    测试:

    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

    此范围表示正常使用应用程序不需要依赖关系,并且仅适用于测试编译和执行阶段。

  • system:

    系统:

    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

    此范围与提供的类似,只是您必须提供明确包含它的JAR。工件始终可用,不会在存储库中查找。

  • import (only available in Maven 2.0.9 or later):

    导入(仅在Maven 2.0.9或更高版本中可用):

    This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

    此范围仅用于节中pom类型的依赖项。它表示应该用该POM部分中的依赖项替换指定的POM。由于它们被替换,具有导入范围的依赖性实际上不参与限制依赖性的传递性。

#1


47  

You are wrong/ambiguous about provided. It means, "This jar should be compiled against locally, but it will be provided on the classpath by something else during runtime, so don't include it in the classpath for me." For example, all web containers (eg: tomcat) include the jars for servlets. You should use provided for the servlet classes so you can compile your code locally, but you don't want to override the servlet classes that tomcat provides for you when you deploy to it.

你提供的错误/含糊不清。这意味着,“这个jar应该在本地编译,但它会在运行时通过其他东西在类路径中提供,所以不要把它包含在类路径中。”例如,所有Web容器(例如:tomcat)都包含servlet的jar。您应该使用为servlet类提供的代码,以便可以在本地编译代码,但是您不希望覆盖tomcat在部署时为您提供的servlet类。


system means, "These dependencies are on my system and I want to point to them directly". You want to avoid this if you can, because another person on another computer won't necessarily have these dependencies.

系统意味着,“这些依赖关系在我的系统上,我想直接指向它们”。如果可以,您希望避免这种情况,因为另一台计算机上的另一个人不一定具有这些依赖关系。

The difference between provided is easier to show:

提供的差异更容易显示:

<dependency>
  <groupId>javax.sql</groupId>
  <artifactId>jdbc-stdext</artifactId>
  <version>2.0</version>
  <scope>system</scope>
  <systemPath>${java.home}/lib/rt.jar</systemPath>
</dependency>

See how it has that <systemPath>? That's the difference. You don't specify the path with provided, provided knows how to get the dependency from a repository. system gets it from your file system only.

看看它是如何有 的?这就是区别。如果知道如何从存储库获取依赖关系,则不要指定提供的路径。系统仅从您的文件系统获取它。


I've never even heard of import. @JigarJoshi linked to http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html which says

我从来没有听说过导入。 @JigarJoshi链接到http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

import (only available in Maven 2.0.9 or later)

导入(仅适用于Maven 2.0.9或更高版本)

This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

此范围仅用于 部分中类型为pom的依赖项。它表示应该用该POM部分中的依赖项替换指定的POM。由于它们被替换,具有导入范围的依赖性实际上不参与限制依赖性的传递性。

I think this is saying, "take all the dependencies this project has and inline them in this <dependencyManagement> section." Someone correct me if I'm wrong.

我想这就是说,“获取这个项目的所有依赖项,并在这个 部分中内联它们。”如果我错了,有人会纠正我。

#2


16  

Please find the exact meaning of scopes in Maven

请在Maven中找到范围的确切含义

I refered to Maven http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

我参考了Maven http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

依赖关系范围用于限制依赖关系的传递性,还用于影响用于各种构建任务的类路径。

There are 6 scopes available:

有6个范围:

  • compile:

    编译:

    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

    这是默认范围,如果未指定,则使用此范围。编译依赖项在项目的所有类路径中都可用。此外,这些依赖项将传播到依赖项目。

  • provided:

    提供:

    This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

    这很像compile,但表示您希望JDK或容器在运行时提供依赖性。例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖关系设置为提供的范围,因为Web容器提供了这些类。此范围仅在编译和测试类路径中可用,并且不可传递。

  • runtime:

    运行:

    This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

    此范围表示编译不需要依赖项,但是用于执行。它位于运行时和测试类路径中,但不是编译类路径。

  • test:

    测试:

    This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

    此范围表示正常使用应用程序不需要依赖关系,并且仅适用于测试编译和执行阶段。

  • system:

    系统:

    This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

    此范围与提供的类似,只是您必须提供明确包含它的JAR。工件始终可用,不会在存储库中查找。

  • import (only available in Maven 2.0.9 or later):

    导入(仅在Maven 2.0.9或更高版本中可用):

    This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

    此范围仅用于节中pom类型的依赖项。它表示应该用该POM部分中的依赖项替换指定的POM。由于它们被替换,具有导入范围的依赖性实际上不参与限制依赖性的传递性。