maven optional 和 maven scope

一、maven optional

1. What is optional?

optional is used to controlMaven depends on jarAn option at the time, indicating whether the dependency should be delivered. When optional is set to true, it means that it will not be delivered to the dependent. When it is false, it means that it can be delivered to the dependent. The default is false.

<optional>true</optional>

2. Why use optional

  • Reduce unnecessary dependency transfer
  • Reduce jar package conflicts

3. Use business scenarios

Suppose we are writing a toolkit my-common now, this toolkit will import a guava package, then our other projects will also import the guava package when importing the my-common package.

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>30.0-jre</version>
</dependency>

Because maven has a dependency transfer mechanism, other projects will have guava jar packages. In fact, our other projects do not need guava packages, so there will be redundant dependencies in our projects. When this happens more and more, In the end, the jar package of the entire project has a lot of redundant dependencies, resulting in a bloated project. At this time, you can set optional to true and it will not be passed to other projects.

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>30.0-jre</version>
	<optional>true</optional>
</dependency>

1. Maven scope

1. What is scope

The role of the scope element: to control the scope of use of the dependency element. In layman's terms, it is to control the scope in which Jar packages are loaded and used. By default, compile dependent projects need to participate in the compilation, testing, packaging, and running stages of the current project.

<scope>compile</scope>

2, scope optional value

  • compile:Defaults. It means that the dependent project needs to participate in the compilation, testing, packaging and running of the current project , which is a relatively strong dependency.
  • test: Dependent projects are only involved in testing-related work. Including the compilation and execution of the test code , it will not be packaged, for example: junit.
  • runtime: Indicates that the dependent project does not need to participate in the compilation of the project , but only participates in the later testing, packaging and running . Compared to compile, compilation is skipped. Such as JDBC driver, suitable for running and testing phase.
  • provided: In theory, it can participate in compilation, testing and other cycles, but it will not be packaged and will not participate in running , because other dependencies will be provided. Compared with compile, the exclude operation is done in the packaging phase.
  • system: Same as provided. However, the dependent items will not be downloaded from the maven repository, but taken from the local file system. The property of systemPath needs to be added to define the path. Generally used to reference external Jar packages.

Guess you like

Origin blog.csdn.net/weixin_44606481/article/details/132700574