使用Maven进行Spring-boot-排除测试范围的依赖

时间:2019-07-16 22:36:01

标签: maven spring-boot maven-3

这是我的全部pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.shadov.test</groupId>
    <artifactId>commons-logging-spring</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.4.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jcl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.0.4.RELEASE</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jcl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.4.RELEASE</version>
            </plugin>
        </plugins>
    </build>
</project>

我运行mvn spring-boot:run时不起作用-java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory。我排除了包含此缺失类的spring-jcl-因此是可以预期的。

现在,我删除了spring-jcl上的spring-boot-starter-test排除对象,它开始起作用。我非常困惑-test范围依赖项中的排除与正在运行的应用程序有什么关系?这是怪异的Maven行为,还是spring-boot Maven插件出于某种原因(即使可能)查看test范围内的依赖项?

1 个答案:

答案 0 :(得分:0)

我想可能是因为RunMojo.java中的requiresDependencyResolution=ResolutionScope.TEST

RunMojo.java#L44-L45