不同的maven范围编译和提供的JAR包装。

时间:2022-01-11 03:40:11

Maybe this question seems to be a cliché but it bothers me somehow. What it difference in using maven scope compile and provided when artifact is built as a JAR? If it was WAR, then I understand - artifact would be included or not in WEB-INF/lib. But in case of the JAR it doesn't matter - dependencies aren't included. They have to be on classpath when their scope is compile or provided. I know that provided dependencies aren't transitive - but is it only one difference?

也许这个问题似乎是老生常谈,但却让我很不舒服。当工件构建为JAR时,使用maven范围编译和提供的区别是什么?如果是战争,那么我理解-工件将被包含在WEB-INF/lib中。但是,在JAR的情况下,它并不包括依赖项。当它们的范围被编译或提供时,它们必须在类路径上。我知道,提供的依赖关系不是传递性的——但它只是一个区别吗?

6 个解决方案

#1


207  

From the Maven Doc:

从Maven医生:

  • 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.

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

Recap:

回顾:

  • dependencies are not transitive (as you mentioned)
  • 依赖性不是传递性的(如您所提到的)
  • provided scope is only available on the compilation and test classpath, whereas compile scope is available in all classpaths.
  • 提供的范围仅在编译和测试类路径上可用,而编译范围在所有类路径中都可用。
  • provided dependencies are not packaged
  • 提供的依赖项没有打包。

#2


220  

Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory.

编译意味着您需要JAR来编译和运行应用程序。对于web应用程序,例如,JAR将被放置在web - inf /lib目录中。

Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory.

提供的意思是您需要JAR进行编译,但是在运行时环境中已经有一个JAR提供了,所以您不需要它与您的应用程序打包。

For a web app, if the app server already provides the JAR (or its functionality), then use "provided" otherwise use "compile".

对于一个web应用程序,如果应用服务器已经提供了JAR(或者它的功能),那么使用“提供”,否则使用“编译”。

Here is the reference.

这是参考。

#3


12  

here is the brief on all supported dependencies ( source maven doc )

以下是所有受支持的依赖项(源maven doc)的简介

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.

这很像编译,但表示您希望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. This scope is not transitive.

这个范围表示对应用程序的正常使用不需要依赖项,并且只能用于测试编译和执行阶段。这个范围不是传递的。

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 supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified 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区域中,依赖项被替换为有效的依赖项列表。由于它们被替换,依赖于一个导入范围的依赖项实际上并没有参与限制依赖的传递性。

#4


10  

If you're planning to generate a single JAR file with all of its dependencies (the typical xxxx-all.jar), then provided scope matters, because the classes inside this scope won't be package in the resulting JAR.

如果您计划生成一个包含所有依赖项的JAR文件(典型的xxxx-all.jar),那么提供范围很重要,因为这个范围内的类不会在结果JAR中打包。

See maven-assembly-plugin for more information

有关更多信息,请参见maven-组装-plugin。

#5


6  

  • compile
  • 编译

Make available into class path, don't add this dependency into final jar if it is normal jar; but add this jar into jar if final jar is a single jar (for example, executable jar)

使可用到类路径,如果它是普通jar,不要将此依赖项添加到最终的jar中;但是,如果最终jar是单个jar(例如,可执行jar),将这个jar添加到jar中。

  • provided
  • 提供

Dependency will be available at run time environment so don't add this dependency in any case; even not in single jar (i.e. executable jar etc)

依赖项将在运行时环境中可用,因此在任何情况下都不要添加这种依赖性;甚至不是单罐(即可执行罐等)

#6


2  

For a jar file, the difference is in the classpath listed in the MANIFEST.MF file included in the jar if addClassPath is set to true in the maven-jar-plugin configuration. 'compile' dependencies will appear in the manifest, 'provided' dependencies won't.

对于jar文件,区别在于清单中列出的类路径。如果addClassPath在maven-jar-plugin配置中设置为true,则在jar中包含MF文件。“编译”依赖项将出现在清单中,“提供的”依赖项不会出现。

One of my pet peeves is that these two words should have the same tense. Either compiled and provided, or compile and provide.

我最讨厌的事情之一就是这两个词应该有相同的时态。编译和提供,或编译和提供。

#1


207  

From the Maven Doc:

从Maven医生:

  • 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.

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

Recap:

回顾:

  • dependencies are not transitive (as you mentioned)
  • 依赖性不是传递性的(如您所提到的)
  • provided scope is only available on the compilation and test classpath, whereas compile scope is available in all classpaths.
  • 提供的范围仅在编译和测试类路径上可用,而编译范围在所有类路径中都可用。
  • provided dependencies are not packaged
  • 提供的依赖项没有打包。

#2


220  

Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory.

编译意味着您需要JAR来编译和运行应用程序。对于web应用程序,例如,JAR将被放置在web - inf /lib目录中。

Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory.

提供的意思是您需要JAR进行编译,但是在运行时环境中已经有一个JAR提供了,所以您不需要它与您的应用程序打包。

For a web app, if the app server already provides the JAR (or its functionality), then use "provided" otherwise use "compile".

对于一个web应用程序,如果应用服务器已经提供了JAR(或者它的功能),那么使用“提供”,否则使用“编译”。

Here is the reference.

这是参考。

#3


12  

here is the brief on all supported dependencies ( source maven doc )

以下是所有受支持的依赖项(源maven doc)的简介

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.

这很像编译,但表示您希望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. This scope is not transitive.

这个范围表示对应用程序的正常使用不需要依赖项,并且只能用于测试编译和执行阶段。这个范围不是传递的。

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 supported on a dependency of type pom in the section. It indicates the dependency to be replaced with the effective list of dependencies in the specified 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区域中,依赖项被替换为有效的依赖项列表。由于它们被替换,依赖于一个导入范围的依赖项实际上并没有参与限制依赖的传递性。

#4


10  

If you're planning to generate a single JAR file with all of its dependencies (the typical xxxx-all.jar), then provided scope matters, because the classes inside this scope won't be package in the resulting JAR.

如果您计划生成一个包含所有依赖项的JAR文件(典型的xxxx-all.jar),那么提供范围很重要,因为这个范围内的类不会在结果JAR中打包。

See maven-assembly-plugin for more information

有关更多信息,请参见maven-组装-plugin。

#5


6  

  • compile
  • 编译

Make available into class path, don't add this dependency into final jar if it is normal jar; but add this jar into jar if final jar is a single jar (for example, executable jar)

使可用到类路径,如果它是普通jar,不要将此依赖项添加到最终的jar中;但是,如果最终jar是单个jar(例如,可执行jar),将这个jar添加到jar中。

  • provided
  • 提供

Dependency will be available at run time environment so don't add this dependency in any case; even not in single jar (i.e. executable jar etc)

依赖项将在运行时环境中可用,因此在任何情况下都不要添加这种依赖性;甚至不是单罐(即可执行罐等)

#6


2  

For a jar file, the difference is in the classpath listed in the MANIFEST.MF file included in the jar if addClassPath is set to true in the maven-jar-plugin configuration. 'compile' dependencies will appear in the manifest, 'provided' dependencies won't.

对于jar文件,区别在于清单中列出的类路径。如果addClassPath在maven-jar-plugin配置中设置为true,则在jar中包含MF文件。“编译”依赖项将出现在清单中,“提供的”依赖项不会出现。

One of my pet peeves is that these two words should have the same tense. Either compiled and provided, or compile and provide.

我最讨厌的事情之一就是这两个词应该有相同的时态。编译和提供,或编译和提供。