Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一个简单的spring引导项目中包含许多第三方JAR时会发生错误_Java_Spring_Maven_Spring Boot - Fatal编程技术网

Java 在一个简单的spring引导项目中包含许多第三方JAR时会发生错误

Java 在一个简单的spring引导项目中包含许多第三方JAR时会发生错误,java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,是否可以从我自己的一个JAR中使用API,而不包括它的依赖项 我创建了一个独立的maven项目(A),它是一个API模块,使用了许多第三方JAR,应该在几个项目中使用。但是在一个springboot项目(B)中,由于A引入了它的依赖项,springboot项目不能很好地工作 如果从pom中排除了A,则spring boot工作良好。但我确实需要它的几个API。我应该如何解决这个问题 起初,一切都很顺利。它是一个纯控制台应用程序。 Pom类似于下面的内容 <dependencies>

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

我创建了一个独立的maven项目(A),它是一个API模块,使用了许多第三方JAR,应该在几个项目中使用。但是在一个springboot项目(B)中,由于A引入了它的依赖项,springboot项目不能很好地工作

如果从pom中排除了A,则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>
然后我想SpringBoot可能会认为我是一个web项目。所以我添加了SpringBootWeb。现在发生了另一个错误

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项目。这就是为什么我问这个问题,只是想为B提供一个干净的环境,同时使用a中的几个API

更新

添加spring启动程序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>

*
org.hibernate
*
org.slf4j
但是由于包含了SpringWeb。。它会从后面启动一只雄猫,这很烦人

更新

因为任何SpringWeb都会让SpringBoot认为它现在是一个web项目,所以最后所有SpringWeb都被删除了。此外,弹簧启动机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>

*
org.hibernate
*
org.slf4j
自由标记
org.freemarker
弹簧网
org.springframework
struts2弹簧插件
org.apache.struts

现在一切正常。

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

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


src/main/resources
*.物业
假的
也看看这个

<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>

示例项目B

您能提供Maven构建消息吗?如果没有更多细节,很难提供好的答案。当添加到API项目中时,了解导致问题的依赖项是非常有用的。了解Spring Boot如何工作得不好也很有用。它导致了什么问题?@AndyWilkinson当我试图从or中排除JAR时,它显示了几个不同的错误,将JAR添加到B。我将修改问题以添加更多详细信息。也许您可以添加项目A依赖项并排除其所有依赖项,如中所述。您可以尝试将依赖项添加到org.springframework.boot:spring boot starter freemarkerstrange回答:问题是关于依赖项的,但示例是关于资源的…这只是一个示例,他可以排除其他未使用的文件。在成功编译B时,如何排除A的所有(或部分)依赖项?你能帮我解释一下你的答案吗?tks:)
<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>