包含许多第三方jar到一个简单的春季启动项目时出现错误

时间:2016-01-24 04:48:28

标签: java spring maven spring-boot

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

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

如果从pom中排除A,则弹簧启动效果很好。但我确实需要几个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

更新

ADD以下两个排除项目,现在没有错误。

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

但是因为包含了春季网络..它将从后面启动一个tomcat,这非常烦人..

更新

由于任何弹簧网都会使弹簧靴现在认为它是一个网络项目,最后所有弹簧网都被移除了。而且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>

现在一切正常......

1 个答案:

答案 0 :(得分:0)

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

在下一个示例中,不会包含所有扩展名为.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