本文介绍了如何真正&从罐子中完全排除包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 pom.xml 中具有此依赖项部分:

Having this dependencies section in my pom.xml:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
  </dependency>


  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>2.7.1</version>
    <type>jar</type>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.7.1</version>
    <type>jar</type>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>3.0.7.RELEASE</version>
  </dependency>


  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.26</version>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.6.Final</version>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-api</artifactId>
    <version>2.7.1</version>
    <exclusions>
      <exclusion>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-javamail_1.4_spec</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>2.7.1</version>
    <type>jar</type>
  </dependency>

  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
  </dependency>

  <dependency>
    <groupId>javax.mail</groupId>
    <artifactId>mail</artifactId>
    <version>1.4.1</version>
  </dependency>
  <dependency>
    <groupId>javax.activation</groupId>
    <artifactId>activation</artifactId>
    <version>1.1.1</version>
  </dependency>

</dependencies>

我希望从构建中排除 geronimo-javamail_1.4_spec 软件包,实际上,当我运行mvn dependency:tree时,该软件包不再在其中列出.

I expected the geronimo-javamail_1.4_spec package to be excluded from the build and, indeed, when I run mvn dependency:tree, it is no longer listed there.

但是,在运行生成的jar时,JarClassLoader仍然抱怨geronimo的类隐藏了javax/mail类...

However, when running the resulting jar, the JarClassLoader still complains about javax/mail classes being hidden by the geronimo ones...

果然,当我通过7-zip检查jar时,我发现geronimo-javamail_1.4_spec-1.7.1.jar仍然存在(作为org.apache.cxf:cxf-api:jar的一部分出现) :2.7.1:compile包).

Sure enough, when I examine the jar via 7-zip, I see that geronimo-javamail_1.4_spec-1.7.1.jar is still there (comes as part of the org.apache.cxf:cxf-api:jar:2.7.1:compile package).

如何完全排除软件包?即不仅将其从依赖关系树中删除,而且根本不将其包括在生成的jar中?

How do I fully exclude a package? i.e. not only remove it from the dependency tree, but also not include it at all in the resulting jar?

根据以下注释添加与onejar相关的xml:

Adding the onejar relevant xml per the comment below:

<plugin>
  <groupId>com.jolira</groupId>
  <artifactId>onejar-maven-plugin</artifactId>
  <version>1.4.4</version>
  <executions>
    <execution>
      <configuration>
        <mainClass>com.corp.dept.proj.myclient</mainClass>
        <onejarVersion>0.97</onejarVersion>
        <attachToBuild>true</attachToBuild>
        <!-- Optional, default is "onejar" -->
        <classifier>onejar</classifier>
      </configuration>
      <goals>
        <goal>one-jar</goal>
      </goals>
    </execution>
  </executions>
</plugin>


在范围&之后添加mvn依赖项的输出.根据以下答案指定了调试选项:


Adding the output of the mvn dependency after the scope & debug options were specified per the answer below:

C:\Users\withheld\workspace\myclient>mvn dependency:tree -Dscope=runtime -Ddebug=true
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] org.apache.maven.plugins: checking for updates from onejar-maven-plugin.googlecode.com
[INFO] org.codehaus.mojo: checking for updates from onejar-maven-plugin.googlecode.com
[INFO] ------------------------------------------------------------------------
[INFO] Building myclient
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/com/oracle/ojdbc14/14/ojdbc14-14.pom
[INFO] Unable to find resource 'com.oracle:ojdbc14:pom:14' in repository central (http://repo1.maven.org/maven2)
[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.corp.dept.proj:myclient:jar:0.0.6
[INFO] +- junit:junit:jar:3.8.1:test
[INFO] +- com.oracle:ojdbc14:jar:14:compile
[INFO] +- org.apache.cxf:cxf-rt-frontend-jaxws:jar:2.7.1:runtime
[INFO] |  +- xml-resolver:xml-resolver:jar:1.2:runtime
[INFO] |  +- asm:asm:jar:3.3.1:runtime
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-soap:jar:2.7.1:runtime
[INFO] |  |  \- org.apache.cxf:cxf-rt-databinding-jaxb:jar:2.7.1:runtime
[INFO] |  +- org.apache.cxf:cxf-rt-bindings-xml:jar:2.7.1:runtime
[INFO] |  +- org.apache.cxf:cxf-rt-frontend-simple:jar:2.7.1:runtime
[INFO] |  \- org.apache.cxf:cxf-rt-ws-addr:jar:2.7.1:runtime
[INFO] |     \- org.apache.cxf:cxf-rt-ws-policy:jar:2.7.1:runtime
[INFO] |        \- org.apache.neethi:neethi:jar:3.0.2:runtime
[INFO] +- org.apache.cxf:cxf-rt-transports-http:jar:2.7.1:compile
[INFO] |  \- org.apache.cxf:cxf-rt-core:jar:2.7.1:compile
[INFO] |     \- com.sun.xml.bind:jaxb-impl:jar:2.1.13:compile
[INFO] +- org.springframework:spring-web:jar:3.0.7.RELEASE:compile
[INFO] |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:3.0.7.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:3.0.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:3.0.7.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-expression:jar:3.0.7.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-asm:jar:3.0.7.RELEASE:compile
[INFO] |  \- org.springframework:spring-core:jar:3.0.7.RELEASE:compile
[INFO] |     \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.26:compile
[INFO] +- org.hibernate:hibernate-core:jar:4.2.6.Final:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.0.GA:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec:jar:1.0.1.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.1.Final:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.2.Final:compile
[INFO] |  \- org.javassist:javassist:jar:3.15.0-GA:compile
[INFO] +- org.apache.cxf:cxf-api:jar:2.7.1:compile
[INFO] |  +- org.codehaus.woodstox:woodstox-core-asl:jar:4.1.4:runtime
[INFO] |  |  \- org.codehaus.woodstox:stax2-api:jar:3.1.1:runtime
[INFO] |  +- org.apache.ws.xmlschema:xmlschema-core:jar:2.0.3:compile
[INFO] |  \- wsdl4j:wsdl4j:jar:1.6.2:compile
[INFO] +- javax.servlet:servlet-api:jar:2.5:compile
[INFO] +- javax.mail:mail:jar:1.4.1:compile
[INFO] \- javax.activation:activation:jar:1.1.1:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Apr 24 11:22:49 EDT 2014
[INFO] Final Memory: 20M/49M
[INFO] ------------------------------------------------------------------------

推荐答案

onejar插件按需添加了来自maven运行时范围的所有工件.

The onejar plugin adds all artifacts from maven's runtime scope, as it should be.

虽然命令mvn dependency:tree似乎没有正确列出所有运行时依赖项.可能是个错误,我不知道为什么.

The command mvn dependency:tree does not seem to list all runtime dependencies properly though. Could be a bug, I don't know why.

幸运的是,通过添加一些参数,您可以列出所有运行时范围依赖项.

Luckily, by adding a few parameters you can have all runtime scope dependencies listed.

mvn dependency:tree -Dscope=runtime -Ddebug=true

这将显示cxf-rt-core也具有geronimo-javamail作为依赖项.因此,您的解决方案是显式添加核心jar,并将其排除在依赖之外.

This will show that cxf-rt-core also has geronimo-javamail as dependency. So, your solution would be to explicitly add the core jar with an exclusion to your dependencies.

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-core</artifactId>
        <version>2.7.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.geronimo.specs</groupId>
                <artifactId>geronimo-javamail_1.4_spec</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

这篇关于如何真正&amp;从罐子中完全排除包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:27