Maven:请求的jar污染了我的lib文件夹

时间:2018-07-12 20:12:54

标签: java maven

我有一个春季项目,我注意到该项目的规模比我预期的要大。我打开了通过lib文件夹中的mvn clean install生成的jar文件。我注意到其中有很多我从未请求过的jar,例如junit.jarmockito-core.jarandroid-json.jar,还有更多的jar文件包含在我从未请求过的lib目录中。

这些不必要的流氓罐子从哪里来?


我的pom.xml如下:

<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.att.onem</groupId>
    <artifactId>SearchSMSHistory</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>SMSSearchService</name>
    <description>SMS Search Service</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- Exclude Spring Boot's Default Logging -->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Add Log4j2 Dependency -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
         </dependency>

        <!-- Needed for Async Logging with Log4j 2 -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.3.6</version>
        </dependency>

        <!-- Swagger dependencies -->



        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>smshistory</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

2 个答案:

答案 0 :(得分:3)

您需要为测试依赖项定义<scope>标签。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

从Maven文档中:

  

有6个可用范围:

     

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

     

提供,这与编译非常相似,但是表明您期望使用JDK   或在运行时提供依赖项的容器。例如,当   为Java Enterprise Edition构建一个Web应用程序,您将   将对Servlet API和相关Java EE API的依赖关系设置为   提供范围,因为Web容器提供了这些类。这个   作用域仅在编译和测试类路径上可用,并且是   不传递。

     

运行时,该范围表明依赖项为   编译时不需要,但执行时需要。它在   运行时和测试类路径,而不是编译类路径。

     

测试   scope表示正常使用不需要依赖项   应用程序,并且仅适用于测试编译和   执行阶段。此范围不是可传递的。

     

系统   与提供的类似,除了您必须提供   明确包含它。该工件始终可用,而并非可用   在存储库中查找。

     

导入(仅适用于Maven 2.0.9或   稍后)仅在类型为pom的依赖项中支持此作用域    部分。它表示要依赖   用指定的有效依赖关系列表替换   POM的部分。由于已被替换,   具有导入范围的依赖项实际上并未参与   限制依赖项的传递性。

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

答案 1 :(得分:2)

您可以使用mvn dependency:tree检查传递依赖。我将您的依赖项简化为入门级spring库,这就是您引入的所有依赖项。

[INFO] com.att.onem:SearchSMSHistory:jar:1.0
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.5.10.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.5.10.RELEASE:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.17:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.5.10.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.27:compile
[INFO] |  |  |  \- org.apache.tomcat:tomcat-annotations-api:jar:8.5.27:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.27:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.27:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.3.6.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.8.10:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.10:compile
[INFO] |  +- org.springframework:spring-web:jar:4.3.14.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.3.14.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.3.14.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.3.14.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.3.14.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:4.3.14.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.4.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-test:jar:1.5.10.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-test-autoconfigure:jar:1.5.10.RELEASE:compile
[INFO] |  +- com.jayway.jsonpath:json-path:jar:2.2.0:compile
[INFO] |  |  +- net.minidev:json-smart:jar:2.2.1:compile
[INFO] |  |  |  \- net.minidev:accessors-smart:jar:1.1:compile
[INFO] |  |  |     \- org.ow2.asm:asm:jar:5.0.3:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] |  +- junit:junit:jar:4.12:compile
[INFO] |  +- org.assertj:assertj-core:jar:2.6.0:compile
[INFO] |  +- org.mockito:mockito-core:jar:1.10.19:compile
[INFO] |  |  \- org.objenesis:objenesis:jar:2.1:runtime
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:compile
[INFO] |  +- org.skyscreamer:jsonassert:jar:1.4.0:compile
[INFO] |  |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.14.RELEASE:compile
[INFO] |  \- org.springframework:spring-test:jar:4.3.14.RELEASE:compile
[INFO] +- org.springframework.cloud:spring-cloud-starter-config:jar:1.4.2.RELEASE:compile
[INFO] |  +- org.springframework.cloud:spring-cloud-starter:jar:1.3.2.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-context:jar:1.3.2.RELEASE:compile
[INFO] |  |  |  \- org.springframework.security:spring-security-crypto:jar:4.2.4.RELEASE:compile
[INFO] |  |  +- org.springframework.cloud:spring-cloud-commons:jar:1.3.2.RELEASE:compile
[INFO] |  |  |  \- org.apache.httpcomponents:httpclient:jar:4.5.5:compile
[INFO] |  |  |     +- org.apache.httpcomponents:httpcore:jar:4.4.9:compile
[INFO] |  |  |     \- commons-codec:commons-codec:jar:1.10:compile
[INFO] |  |  \- org.springframework.security:spring-security-rsa:jar:1.0.3.RELEASE:compile
[INFO] |  |     \- org.bouncycastle:bcpkix-jdk15on:jar:1.55:compile
[INFO] |  |        \- org.bouncycastle:bcprov-jdk15on:jar:1.55:compile
[INFO] |  \- org.springframework.cloud:spring-cloud-config-client:jar:1.4.2.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.4.2.RELEASE:compile
[INFO] |  +- org.apache.tomcat:tomcat-jdbc:jar:8.5.27:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-juli:jar:8.5.27:compile
[INFO] |  \- org.springframework:spring-jdbc:jar:4.3.14.RELEASE:compile
[INFO] |     \- org.springframework:spring-tx:jar:4.3.14.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:1.4.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.5.10.RELEASE:compile
[INFO] |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.10.RELEASE:compile
[INFO] \- org.springframework.boot:spring-boot-starter-log4j2:jar:1.5.10.RELEASE:compile
[INFO]    +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile
[INFO]    +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO]    +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO]    +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO]    \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile

Spring的启动器带来了很多依赖关系,这有助于确保您获得正确的版本,但是代价是您可能拥有实际上不需要的大量依赖关系。

排除依赖项的两个选择是限制范围,或使用依赖项排除手动排除它。

对于测试框架,通常会将其注释为测试范围。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>1.4.2.RELEASE</version>
    <scope>test</scope>
</dependency>

或者,如果要排除其依赖项之一(即,如果它是较旧的版本),或者您的遗留代码需要较旧的版本,则可以使用排除项,并手动提供项目所需的库。这对于仅缩小入门包可能会带来不需要的东西也很有用,例如,我从未听说过skyscreamer jsonassert测试库,如果我不必测试json,我可能会排除它,因为我真的不需要处理那部分。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
</dependency>