其他分享
首页 > 其他分享> > 如何从jar中真正完全排除包?

如何从jar中真正完全排除包?

作者:互联网

在我的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仍然抱怨jronax / 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] ------------------------------------------------------------------------

解决方法:

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>

标签:java,maven,pom-xml,onejar
来源: https://codeday.me/bug/20190830/1771411.html