Maven pom file dependency scope usage

Dependent field has in Maven: compile, provided, runtime, system, test, import

A, compile (default)

When the scope is dependent compile time, then the current dependence of the package, will be joined at compile time, and will be joined in the pack (mvn package) time.
Compile effective range, when compiling the package will join in.

二、provided

When the scope is dependent on the time provided at compile time and test effective, when executing (mvn package) packaged will not join. For example, we developed a web application, at compile time, we need to rely on servlet-api.jar, but we do not need to run the jar package, because the jar package provided by the web server, if the added time and packaged in, then it may conflict. At this point we can use the provided range be modified.

Three, system

And provided the same, but will not be available from the dependencies maven repository, but take from the local file system, you need to use with systemPath property. such as:

<dependency>
    <groupId>org.open</groupId>
    <artifactId>open-core</artifactId>
    <version>1.5</version>
    <scope>system</scope>
    <systemPath>${basedir}/WebContent/WEB-INF/lib/open-core.jar</systemPath>
</dependency>

四、runtime

When the scope is dependent runtime when at run time will depend, will not depend at compile time. For example, at compile time we do not need the JDBC API jar package, but when we need to run the JDBC driver package. You can use the runtime modification.

Five, test

When the time is dependent on the scope of the test, it refers to a valid test range, in compiling and packaging when they are not using this dependence.

Six, import

maven multi-module program structure, the parent can use the program defines the parent, dependent inheritance from the parent project. But maven only single inheritance, namely a project can only use one parent tag that defines the parent project.
After maven2.9 version introduces a new feature, multiple inheritance can be achieved depends on. This feature can be split depend configure complex pom pom file into a plurality of separate files. Such processing can be made more compact maven pom configuration, while these may be multiplexed pom dependent.

For example, we are developing projects spring boot time, pom there will be as follows:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>

However, if the item is a sub-module maven, it will be a problem. Since the java maven Similar single inheritance, you can not have two parent, now <parent> </ parent> tag is used to reference the parent module has, now used to refer to springboot, a collision occurs.

That solution is: Use dependencyManagement introduction of dependency, and the scope attribute to import. as follows:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Note: scope of import property can only be used in <dependencyManagement> in dependency represents import configuration file from another pom.

 

Guess you like

Origin www.cnblogs.com/alan6/p/11519958.html