【问题标题】:How to really & fully exclude a package from a jar?如何真正完全地从 jar 中排除一个包?
【发布时间】:2014-05-25 23:59:59
【问题描述】:

在我的 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 时,它不再列在那里。

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

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

如何完全排除一个包?即不仅将其从依赖树中删除,而且根本不将其包含在生成的 jar 中?


根据以下评论添加 onejar 相关的 xml:

<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 依赖项的输出:

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

【问题讨论】:

  • 看罐子comes as part of org.apache.cxf:cxf-api...怎么说?
  • @Raghuram 不是通过查看 jar,而是在我将 &lt;exclusion&gt; 添加到 pom.xml 之前运行 mvn dependency:tree。这棵树明确地告诉我geronimo 来自哪里。谷歌搜索进一步证实了 Apache CXF 包括这个(除了其他几个)。
  • 如何运行 jar?您是否使用 maven 插件来构建 fat-jar?你用mvn exec 吗?
  • @rompetroll 我使用简单的java -jar myonejar.jar 运行 jar。是的,我正在使用一个名为 onejar 的插件来创建一个胖 jar。
  • 您能否也为 onejar 插件添加您的 xml。它可能与问题有关。

标签: java maven pom.xml onejar


【解决方案1】:

onejar 插件添加了 maven 运行时范围内的所有工件。

mvn dependency:tree 命令似乎没有正确列出所有运行时依赖项。可能是bug,不知道为什么。

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

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

这将表明cxf-rt-core 也有geronimo-javamail 作为依赖项。因此,您的解决方案是显式添加带有排除项的核心 jar 到您的依赖项中。

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

【讨论】:

  • 感谢 +1 对 mvn dependency:tree-Dscope=runtime -Ddebug=true 提示。出于某种原因,我没有看到包含 geronimo 家伙的 cxf-rt-core,而只看到了 com.sun.xml.bind:jaxb-impl。不过,我会继续尝试您的建议并报告。
  • 我还将mvn树的输出添加到了OP中。
  • 难以置信。你的把戏就像魔术一样奏效(尽管没有出现在我的mvn dependency:tree -Dscope=runtime -Ddebug=true 输出中)。谢谢一百万!
  • 输出看起来不像调试输出我猜它是一个 Windows 的东西。值得尝试将所有系统属性都设为大写。 mvn dependency:tree -DDEBUG=true -DSCOPE=runtime
猜你喜欢
  • 1970-01-01
  • 2012-04-02
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 2016-01-22
  • 2021-12-14
相关资源
最近更新 更多