maven和springboot

背景

前面已经写过一篇关于jar包冲突的解决文章,本来以为在这条路上将会畅通无阻,无非就是那三四个原则,不过在使用maven和springboot的组合时,又遇到了这个问题,特此记录

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jingyou_spring</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.4.0</version>  
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <finalName>jingyou</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>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

分析

我使用的springboot版本是2.2.2.RELEASE,我在一个maven项目中需要使用到自动化项目的依赖是selenium-java,使用的是3.4.0,此时我把这个maven项目的使用的selenium-java的依赖复制到springboot项目中,运行的时候直接报错如下:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.util.concurrent.SimpleTimeLimiter.create(Ljava/util/concurrent/ExecutorService;)Lcom/google/common/util/concurrent/SimpleTimeLimiter;
	at org.openqa.selenium.net.UrlChecker.<init>(UrlChecker.java:62)
	at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:197)
	at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:188)
	at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:79)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
	at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
	at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
	at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
	at com.example.demo.JingyouSpringApplication.tencentbalance(JingyouSpringApplication.java:45)
	at com.example.demo.JingyouSpringApplication.main(JingyouSpringApplication.java:33)

Process finished with exit code 1

下意识的就意识到是jar包冲突导致的,找不到这个类的这个方法,此时看看maven依赖版本图,执行mvn dependency:tree,得到依赖如下

java0904@weigongdeMacBook-Pro jingyou_spring % mvn dependency:tree 
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building jingyou_spring 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.seleniumhq.selenium:selenium-java:jar:3.4.0:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-api:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.141.59:compile
[INFO] |  |  +- net.bytebuddy:byte-buddy:jar:1.10.4:compile
[INFO] |  |  +- com.squareup.okhttp3:okhttp:jar:3.14.4:compile
[INFO] |  |  \- com.squareup.okio:okio:jar:1.14.0:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-support:jar:3.141.59:compile
[INFO] |  +- cglib:cglib-nodep:jar:3.2.4:compile
[INFO] |  +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.9:compile
[INFO] |  +- commons-codec:commons-codec:jar:1.13:compile
[INFO] |  +- commons-io:commons-io:jar:2.5:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.2:compile
[INFO] |  +- org.w3c.css:sac:jar:1.3:compile
[INFO] |  +- net.sourceforge.cssparser:cssparser:jar:0.9.22:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.8.6:compile
[INFO] |  +- com.google.guava:guava:jar:21.0:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:2.1:compile
[INFO] |  +- net.sourceforge.htmlunit:htmlunit:jar:2.36.0:compile
[INFO] |  |  +- org.apache.commons:commons-text:jar:1.7:compile
[INFO] |  |  +- net.sourceforge.htmlunit:htmlunit-cssparser:jar:1.5.0:compile
[INFO] |  |  +- commons-net:commons-net:jar:3.6:compile
[INFO] |  |  \- org.brotli:dec:jar:0.1.2:compile
[INFO] |  +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.26:compile
[INFO] |  +- net.sourceforge.htmlunit:neko-htmlunit:jar:2.25:compile
[INFO] |  +- org.apache.httpcomponents:httpclient:jar:4.5.10:compile
[INFO] |  +- org.apache.httpcomponents:httpcore:jar:4.4.12:compile
[INFO] |  +- org.apache.httpcomponents:httpmime:jar:4.5.10:compile
[INFO] |  +- org.eclipse.jetty:jetty-io:jar:9.4.24.v20191120:compile
[INFO] |  +- org.eclipse.jetty:jetty-util:jar:9.4.24.v20191120:compile
[INFO] |  +- net.java.dev.jna:jna:jar:4.5.2:compile
[INFO] |  +- net.java.dev.jna:jna-platform:jar:4.5.2:compile
[INFO] |  +- junit:junit:jar:4.12:compile
[INFO] |  +- com.codeborne:phantomjsdriver:jar:1.4.0:compile
[INFO] |  +- org.seleniumhq.selenium:htmlunit-driver:jar:2.36.0:compile
[INFO] |  +- javax.servlet:javax.servlet-api:jar:4.0.1:compile
[INFO] |  +- org.eclipse.jetty.websocket:websocket-api:jar:9.4.24.v20191120:compile
[INFO] |  +- org.eclipse.jetty.websocket:websocket-client:jar:9.4.24.v20191120:compile
[INFO] |  |  +- org.eclipse.jetty:jetty-client:jar:9.4.24.v20191120:compile
[INFO] |  |  |  \- org.eclipse.jetty:jetty-http:jar:9.4.24.v20191120:compile
[INFO] |  |  \- org.eclipse.jetty:jetty-xml:jar:9.4.24.v20191120:compile
[INFO] |  +- org.eclipse.jetty.websocket:websocket-common:jar:9.4.24.v20191120:compile
[INFO] |  +- xalan:serializer:jar:2.7.2:compile
[INFO] |  +- xalan:xalan:jar:2.7.2:compile
[INFO] |  +- xerces:xercesImpl:jar:2.11.0:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:2.2.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:5.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.2.2.RELEASE:compile
[INFO] |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.12.1:compile
[INFO] |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.12.1:compile
[INFO] |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.29:compile
[INFO] |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  +- org.springframework:spring-core:jar:5.2.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.2.2.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.25:runtime
[INFO] +- org.projectlombok:lombok:jar:1.18.10:compile (optional) 
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.2.2.RELEASE:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.10.1:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.10.1:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.10.1:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.10.1:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.10.1:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.10.1:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.2.2.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.29:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.29:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.29:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-validation:jar:2.2.2.RELEASE:compile
[INFO] |  |  +- jakarta.validation:jakarta.validation-api:jar:2.0.1:compile
[INFO] |  |  \- org.hibernate.validator:hibernate-validator:jar:6.0.18.Final:compile
[INFO] |  |     +- org.jboss.logging:jboss-logging:jar:3.4.1.Final:compile
[INFO] |  |     \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] |  +- org.springframework:spring-web:jar:5.2.2.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.2.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.2.2.RELEASE:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.2.2.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.2.2.RELEASE:compile
[INFO] +- net.sf.json-lib:json-lib:jar:jdk15:2.4:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.8.0:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.5:compile
[INFO] |  \- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.2.2.RELEASE:test
[INFO]    +- org.springframework.boot:spring-boot-test:jar:2.2.2.RELEASE:test
[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.2.2.RELEASE:test
[INFO]    +- com.jayway.jsonpath:json-path:jar:2.4.0:test
[INFO]    |  +- net.minidev:json-smart:jar:2.3:test
[INFO]    |  |  \- net.minidev:accessors-smart:jar:1.2:test
[INFO]    |  |     \- org.ow2.asm:asm:jar:5.0.4:test
[INFO]    |  \- org.slf4j:slf4j-api:jar:1.7.29:compile
[INFO]    +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.2:test
[INFO]    |  \- jakarta.activation:jakarta.activation-api:jar:1.2.1:test
[INFO]    +- org.junit.jupiter:junit-jupiter:jar:5.5.2:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-api:jar:5.5.2:test
[INFO]    |  |  +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO]    |  |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO]    |  |  \- org.junit.platform:junit-platform-commons:jar:1.5.2:test
[INFO]    |  +- org.junit.jupiter:junit-jupiter-params:jar:5.5.2:test
[INFO]    |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.5.2:test
[INFO]    |     \- org.junit.platform:junit-platform-engine:jar:1.5.2:test
[INFO]    +- org.mockito:mockito-junit-jupiter:jar:3.1.0:test
[INFO]    +- org.assertj:assertj-core:jar:3.13.2:test
[INFO]    +- org.hamcrest:hamcrest:jar:2.1:compile
[INFO]    +- org.mockito:mockito-core:jar:3.1.0:test
[INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.10.4:test
[INFO]    |  \- org.objenesis:objenesis:jar:2.6:test
[INFO]    +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO]    +- org.springframework:spring-test:jar:5.2.2.RELEASE:test
[INFO]    \- org.xmlunit:xmlunit-core:jar:2.6.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.531 s
[INFO] Finished at: 2021-01-08T10:56:28+08:00
[INFO] ------------------------------------------------------------------------

可以看出com.google.guava:guava:jar:21.0:compile,版本是21。于是我又按照之前分析的那个方法,在其他jar包中进行查找是否依赖了这个包,可以看到在这个 tree上我是查不到有哪个其他jar包依赖了这个包。陷入了绝路。只能想其他办法。

解决

此时,想到springboot都是自动控制版本号的,也就是引入的其他的jar都无需写出版本号(此处不是很确定),于是去掉selenium这个依赖的版本号。于是执行tree命令,得到的是

com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] +- org.seleniumhq.selenium:selenium-java:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-api:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-chrome-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-edge-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-firefox-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-ie-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-opera-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-remote-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-safari-driver:jar:3.141.59:compile
[INFO] |  +- org.seleniumhq.selenium:selenium-support:jar:3.141.59:compile
[INFO] |  +- net.bytebuddy:byte-buddy:jar:1.10.4:compile
[INFO] |  +- org.apache.commons:commons-exec:jar:1.3:compile
[INFO] |  +- com.google.guava:guava:jar:25.0-jre:compile
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] |  |  +- org.checkerframework:checker-compat-qual:jar:2.0.0:compile
[INFO] |  |  +- com.google.errorprone:error_prone_annotations:jar:2.1.3:compile
[INFO] |  |  +- com.google.j2objc:j2objc-annotations:jar:1.1:compile
[INFO] |  |  \- org.codehaus.mojo:animal-sniffer-annotations:jar:1.14:compile
[INFO] |  +- com.squareup.okhttp3:okhttp:jar:3.14.4:compile
[INFO] |  \- com.squareup.okio:okio:jar:1.14.0:compile

可以看到此时selenium的版本号是[INFO] ± org.seleniumhq.selenium:selenium-java:jar:3.141.59:compile,这个版本明显高于我自己引入的版本号,同时,还有这个重要的信息。
问题是可以解决,项目可以正常运行。但是我的问题没有解决,为什么?此时我还是按照三个原则去分析。难道是springboot中自己就引入了这个包,难道你还控制了所有的 jar包的版本号吗,难道你引入了世界上所有的jar包,连selenium这么不常用的jar也引入了吗。我打开springboot的parent的依赖,向里面查看,打开parent,然后继续打开spring-boot-dependencies,此时搜索selenium,不得了,竟然搜到了,而且很多

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>${selenium-htmlunit.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-edge-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-ie-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-opera-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>${selenium.version}</version>
        <exclusions>
          <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
          </exclusion>
        </exclusions>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-safari-driver</artifactId>
        <version>${selenium.version}</version>
      </dependency>
      <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>${selenium.version}</version>
        <exclusions>
          <exclusion>
            <artifactId>commons-logging</artifactId>
            <groupId>commons-logging</groupId>
          </exclusion>
        </exclusions>
      </dependency>

看看版本号,properties里面是

    <selenium.version>3.141.59</selenium.version>
    <selenium-htmlunit.version>2.36.0</selenium-htmlunit.version>

到此处问题就明了了。springboot果然是管理了如此众多的包。

拓展

springboot引入了如此众多的包,那么为什么在我的项目中没有存在所有的这些包呢,是因为继承父工程的maven项目,如果dependencyManagement声明的包,如果在子项目中显示的声明(即引入),才会真正使用到,此时在idea的External libraries目录中就会显示到。

鸣谢

谢谢maven,谢谢springboot!非常感谢!!!!

上一篇:爬虫-房网


下一篇:每日日报