gpt4 book ai didi

java - 将许多第 3 方 jar 包含到一个简单的 spring boot 项目时会发生错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:48:42 27 4
gpt4 key购买 nike

是否可以使用我自己的一个 JAR 中的 API,而不包括其依赖项?

我创建了一个独立的 Maven 项目 (A),它是一个 API 模块,它使用许多第 3 方 jar,应该在多个项目中使用。但是在一个spring boot项目(B)中,由于A引入了依赖,导致spring boot项目无法正常运行。

如果A被排除在pom之外,那么spring boot就可以正常工作了。但我确实需要它的几个 API。我该如何解决这个问题?

起初,一切正常。它是一个纯控制台应用程序。Pom 如下所示。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
-->
<!--
<dependency>
<groupId>InsuranceWrapper</groupId>
<artifactId>InsuranceWrapper</artifactId>
<version>1.0</version>
</dependency>
-->
</dependencies>

然后我将 InsuranceWrapper 添加到其中,它会出现一个错误。

java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration$TransactionManagementConfiguration due to org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurer not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:55) ~[spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE]
at
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/servlet/view/freemarker/FreeMarkerConfigurer
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_45]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_45]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_45]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609) ~[spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at
Caused by: java.lang.ClassNotFoundException: org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_45]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_45]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_45]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_45]
... 38 common frames omitted

然后我想到spring boot可能会认为我是一个web项目。所以我添加了 spring boot web。现在出现另一个错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at
Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor.getValidationMode(PersistenceUnitInfoDescriptor.java:99) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]

搜索这些错误,一点线索都没有。我的A项目是一个复杂的spring+hibernate项目。这就是为什么我问这个问题,只想在使用 A 的多个 API 时为 B 提供一个干净的环境。

更新

添加 spring-boot-starter-freemarker 修复第一个错误

更新

更新mvn树

    [INFO] net.ebaolife:EBTasks:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:1.3.1.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:4.2.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:4.2.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:1.3.1.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.1.2:compile
[INFO] | | | \- ch.qos.logback:logback-core:jar:1.1.2:compile
[INFO] | | +- org.slf4j:jcl-over-slf4j:jar:1.7.7:compile
[INFO] | | +- org.slf4j:jul-to-slf4j:jar:1.7.7:compile
[INFO] | | \- org.slf4j:log4j-over-slf4j:jar:1.7.7:compile
[INFO] | +- org.springframework:spring-core:jar:4.2.4.RELEASE:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.16:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-freemarker:jar:1.3.1.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:1.3.1.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.3.1.RELEASE:compile
[INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.30:compile
[INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.30:compile
[INFO] | | | +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.30:compile
[INFO] | | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.30:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-validation:jar:1.3.1.RELEASE:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.4:compile
[INFO] | | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.4:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-core:jar:2.6.4:compile
[INFO] | | +- org.springframework:spring-web:jar:4.2.4.RELEASE:compile
[INFO] | | \- org.springframework:spring-webmvc:jar:4.2.4.RELEASE:compile
[INFO] | +- org.freemarker:freemarker:jar:2.3.23:compile
[INFO] | \- org.springframework:spring-context-support:jar:4.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-beans:jar:4.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.3.1.RELEASE:compile
[INFO] | +- org.apache.tomcat:tomcat-jdbc:jar:8.0.30:compile
[INFO] | | \- org.apache.tomcat:tomcat-juli:jar:8.0.30:compile
[INFO] | \- org.springframework:spring-jdbc:jar:4.2.4.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:4.2.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.3.1.RELEASE:test
[INFO] | +- junit:junit:jar:4.12:compile
[INFO] | +- org.mockito:mockito-core:jar:1.10.19:test
[INFO] | | \- org.objenesis:objenesis:jar:2.1:test
[INFO] | +- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] | +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] | \- org.springframework:spring-test:jar:4.2.4.RELEASE:test
[INFO] +- com.dangdang:elastic-job-core:jar:1.0.2:compile
[INFO] | +- org.projectlombok:lombok:jar:1.16.4:compile
[INFO] | +- com.google.guava:guava:jar:18.0:compile
[INFO] | +- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO] | | \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] | +- org.quartz-scheduler:quartz:jar:2.2.1:compile
[INFO] | | +- c3p0:c3p0:jar:0.9.1.1:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] | +- org.apache.curator:curator-framework:jar:2.8.0:compile
[INFO] | | \- org.apache.zookeeper:zookeeper:jar:3.4.6:compile
[INFO] | | +- log4j:log4j:jar:1.2.17:compile
[INFO] | | +- jline:jline:jar:0.9.94:compile
[INFO] | | \- io.netty:netty:jar:3.7.0.Final:compile
[INFO] | +- org.apache.curator:curator-client:jar:2.8.0:compile
[INFO] | \- org.apache.curator:curator-recipes:jar:2.8.0:compile
[INFO] +- com.dangdang:elastic-job-spring:jar:1.0.2:compile
[INFO] +- org.mybatis:mybatis:jar:3.2.8:compile
[INFO] +- org.mybatis:mybatis-spring:jar:1.2.2:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.38:compile
[INFO] +- org.mongodb.morphia:morphia:jar:1.1.0:compile
[INFO] | +- org.mongodb:mongo-java-driver:jar:2.13.3:compile
[INFO] | +- com.thoughtworks.proxytoys:proxytoys:jar:1.0:compile
[INFO] | \- cglib:cglib-nodep:jar:2.2.2:compile
[INFO] +- eu.bitwalker:UserAgentUtils:jar:1.18:compile
[INFO] \- InsuranceWrapper:InsuranceWrapper:jar:1.0:compile
[INFO] +- ebcore:ebcore:jar:1.0:compile
[INFO] | +- com.google.code.gson:gson:jar:2.3.1:compile
[INFO] | +- org.apache.commons:commons-dbcp2:jar:2.1.1:compile
[INFO] | | \- org.apache.commons:commons-pool2:jar:2.4.2:compile
[INFO] | +- org.jdom:jdom:jar:1.1:compile
[INFO] | +- org.jdom:jdom2:jar:2.0.6:compile
[INFO] | +- commons-pool:commons-pool:jar:1.6:compile
[INFO] | +- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-aop:jar:4.2.4.RELEASE:compile
[INFO] | +- org.springframework:spring-orm:jar:4.2.4.RELEASE:compile
[INFO] | +- org.acegisecurity:acegi-security:jar:1.0.7:compile
[INFO] | | +- org.springframework:spring-remoting:jar:1.2.9:compile
[INFO] | | +- org.springframework:spring-support:jar:1.2.9:runtime
[INFO] | | \- commons-lang:commons-lang:jar:2.1:compile
[INFO] | +- com.aliyun.openservices:aliyun-openservices:jar:1.2.2:compile
[INFO] | | \- com.google.protobuf:protobuf-java:jar:2.4.1:compile
[INFO] | +- antlr:antlr:jar:2.7.7:compile
[INFO] | +- org.apache.ant:ant:jar:1.9.4:compile
[INFO] | | \- org.apache.ant:ant-launcher:jar:1.9.4:compile
[INFO] | +- asm:asm-attrs:jar:1.5.3:compile
[INFO] | +- asm:asm:jar:3.1:compile
[INFO] | +- org.aspectj:aspectjrt:jar:1.8.7:compile
[INFO] | +- aspectj:aspectjweaver:jar:1.5.3:compile
[INFO] | +- org.glassfish.hk2.external:cglib:jar:2.1.3:compile
[INFO] | +- org.cometd.java:cometd-api:jar:1.0.beta8:compile
[INFO] | +- org.mortbay.jetty:cometd-server:jar:6.1.17:compile
[INFO] | | \- org.mortbay.jetty:jetty-util5:jar:6.1.17:compile
[INFO] | +- commons-discovery:commons-discovery:jar:0.2:compile
[INFO] | +- commons-fileupload:commons-fileupload:jar:1.3:compile
[INFO] | | \- commons-io:commons-io:jar:2.2:compile
[INFO] | +- com.hynnet:commons-lang3:jar:3.3.2:compile
[INFO] | +- commons-net:commons-net:jar:3.3:compile
[INFO] | +- org.xhtmlrenderer:core-renderer:jar:R8:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | | \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] | +- org.directwebremoting:dwr:jar:2.0.3:compile
[INFO] | +- net.sf.ehcache:ehcache:jar:2.10.1:compile
[INFO] | +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] | +- net.sf.ezmorph:ezmorph:jar:1.0.6:compile
[INFO] | +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] | | +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] | | \- org.hibernate:hibernate-core:jar:4.3.11.Final:compile
[INFO] | | \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] | +- org.hibernate:hibernate-entitymanager:jar:4.3.11.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging:jar:3.3.0.Final:compile
[INFO] | | +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] | | +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] | | +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] | | +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] | | \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] | +- org.hibernate:hibernate-search:jar:3.1.0.GA:compile
[INFO] | | \- org.apache.lucene:lucene-core:jar:2.4.0:compile
[INFO] | +- org.hibernate:hibernate-tools:jar:3.2.4.GA:compile
[INFO] | | +- org.beanshell:bsh:jar:2.0b4:compile
[INFO] | | \- org.hibernate:jtidy:jar:r8-20060801:compile
[INFO] | +- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] | | \- com.fasterxml:classmate:jar:1.1.0:compile
[INFO] | +- oro:oro:jar:2.0.8:compile
[INFO] | +- javassist:javassist:jar:3.11.0.GA:compile
[INFO] | +- jaxen:jaxen:jar:1.1.6:compile
[INFO] | +- axis:axis-jaxrpc:jar:1.3:compile
[INFO] | +- jboss:jboss-archive-browsing:jar:2.0.2.alpha:compile
[INFO] | +- com.jcraft:jsch:jar:0.1.51:compile
[INFO] | +- org.json:json:jar:20140107:compile
[INFO] | +- javax.servlet:jstl:jar:1.2:compile
[INFO] | +- javax.transaction:jta:jar:1.1:compile
[INFO] | +- net.sourceforge.jexcelapi:jxl:jar:2.6.10:compile
[INFO] | +- com.sun.mail:javax.mail:jar:1.5.4:compile
[INFO] | +- ognl:ognl:jar:3.0.6:compile
[INFO] | +- jsptags:pager-taglib:jar:2.0:compile
[INFO] | +- com.belerweb:pinyin4j:jar:2.5.0:compile
[INFO] | +- org.apache.poi:poi:jar:3.12:compile
[INFO] | +- org.apache.poi:poi-scratchpad:jar:3.12:compile
[INFO] | +- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
[INFO] | +- com.google.code.simple-spring-memcached:spymemcached:jar:2.8.4:compile
[INFO] | +- taglibs:standard:jar:1.1.2:compile
[INFO] | +- org.apache.struts:struts2-core:jar:2.3.24.1:compile
[INFO] | | \- org.apache.struts.xwork:xwork-core:jar:2.3.24.1:compile
[INFO] | | \- asm:asm-commons:jar:3.3:compile
[INFO] | | \- asm:asm-tree:jar:3.3:compile
[INFO] | +- org.apache.struts:struts2-json-plugin:jar:2.3.24.1:compile
[INFO] | | \- org.apache.commons:commons-lang3:jar:3.2:compile
[INFO] | +- org.apache.struts:struts2-spring-plugin:jar:2.3.24.1:compile
[INFO] | +- redis.clients:jedis:jar:2.7.3:compile
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.5.1:compile
[INFO] | | \- org.apache.httpcomponents:httpcore:jar:4.4.4:compile
[INFO] | +- ebao:tk-filters:jar:1.0:compile
[INFO] | +- ebao:flexjson:jar:1.0:compile
[INFO] | +- ebao:QRCode:jar:1.0:compile
[INFO] | +- ebao:java_memcached:jar:release_2.6.6:compile
[INFO] | +- ebao:jbarcode:jar:0.2.8:compile
[INFO] | +- ebao:jdbc2.0:jar:stdext:compile
[INFO] | \- ebao:lylab:jar:1.0:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] +- ebao:cxf:jar:2.3.3:compile
[INFO] +- com.google.code.findbugs:jsr305:jar:3.0.0:compile
[INFO] +- org.apache.ws.commons.schema:XmlSchema:jar:1.4.7:compile
[INFO] +- org.apache.axis:axis:jar:1.4:compile
[INFO] +- org.codehaus.xfire:xfire-core:jar:1.2.6:compile
[INFO] | +- javax.activation:activation:jar:1.1:compile
[INFO] | +- wsdl4j:wsdl4j:jar:1.6.3:compile
[INFO] | +- stax:stax-api:jar:1.0.1:compile
[INFO] | +- commons-codec:commons-codec:jar:1.3:compile
[INFO] | +- org.codehaus.woodstox:wstx-asl:jar:3.2.0:compile
[INFO] | +- commons-logging:commons-logging:jar:1.0.4:compile
[INFO] | \- commons-httpclient:commons-httpclient:jar:3.0:compile
[INFO] +- org.codehaus.xfire:xfire-aegis:jar:1.2.6:compile
[INFO] | \- net.java.dev.stax-utils:stax-utils:jar:20040917:compile
[INFO] +- org.codehaus.xfire:xfire-java5:jar:1.2.6:compile
[INFO] | +- org.codehaus.xfire:xfire-annotations:jar:1.2.6:compile
[INFO] | | \- commons-attributes:commons-attributes-api:jar:2.1:compile
[INFO] | | \- qdox:qdox:jar:1.5:compile
[INFO] | \- xfire:xfire-jsr181-api:jar:1.0-M1:compile
[INFO] \- org.codehaus.xfire:xfire-xmlbeans:jar:1.2.6:compile
[INFO] \- xmlbeans:xbean:jar:2.2.0:compile

更新

在下面添加两个排除项,现在没有错误了。

<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>

但是因为包含spring web..它会从后面启动一个tomcat,这很烦人..

更新

由于任何 spring-web 都会让 spring boot 认为它现在是一个 web 项目,最后所有的 spring web 都被删除了。而且 spring-boot-starter-freemarker 也被删除了,因为它实际上没有被使用。

<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.hibernate</groupId>
</exclusion>
<exclusion>
<artifactId>*</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>freemarker</artifactId>
<groupId>org.freemarker</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>struts2-spring-plugin</artifactId>
<groupId>org.apache.struts</groupId>
</exclusion>
</exclusions>

现在一切正常..

最佳答案

要从 jar/target 目录中排除任何文件,您可以使用 <excludes> pom.xml 文件中的标记。

在下一个示例中,所有带有 .properties 的文件扩展名将不包括在内:

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>

也看看这个

<dependencies>
<dependency>
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>sample.ProjectE</groupId> <!-- Exclude Project-E from Project-B -->
<artifactId>Project-E</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

关于java - 将许多第 3 方 jar 包含到一个简单的 spring boot 项目时会发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34972345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com