简体   繁体   English

Maven shade插件重定位并捆绑不起作用

[英]Maven shade plugin relocate and bundle not working

In my POM.xml, I have used maven-shade plugin and relocated the httpClient dependency. 在我的POM.xml中,我使用了maven-shade插件并重新定位了httpClient依赖项。 But when I print the dependency tree using mvn dependency:tree, I still see the httpClient library in the tree as shown below. 但是当我使用mvn dependency:tree打印依赖树时,我仍然在树中看到httpClient库,如下所示。 Why is this happening. 为什么会这样呢? Any help will be appreciated. 任何帮助将不胜感激。

[INFO]    +- org.apache.httpcomponents:httpclient:jar:4.3.5:provided

My POM.xml is as below. 我的POM.xml如下。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <version>1.0-SNAPSHOT</version>
 <artifactId>parser</artifactId>
  <packaging>jar</packaging>

 <!-- change these to the appropriate values -->
  <name>Parser Apps</name>
  <description>Applications to showcase different parsers</description>

  <properties>
<!-- change this if you desire to use a different version of Apex Core -->
<apex.version>3.4.0</apex.version>
    <apex.apppackage.classpath>lib/*.jar</apex.apppackage.classpath>
   <malhar.version>3.4.0</malhar.version>
 </properties>

  <build>
<plugins>
   <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-eclipse-plugin</artifactId>
     <version>2.9</version>
     <configuration>
       <downloadSources>true</downloadSources>
     </configuration>
   </plugin>
   <plugin>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.3</version>
     <configuration>
       <encoding>UTF-8</encoding>
       <source>1.7</source>
       <target>1.7</target>
       <debug>true</debug>
       <optimize>false</optimize>
       <showDeprecation>true</showDeprecation>
       <showWarnings>true</showWarnings>
     </configuration>
   </plugin>
   <plugin>
     <artifactId>maven-dependency-plugin</artifactId>
     <version>2.8</version>
     <executions>
       <execution>
         <id>copy-dependencies</id>
         <phase>prepare-package</phase>
         <goals>
           <goal>copy-dependencies</goal>
         </goals>
         <configuration>
           <outputDirectory>target/deps</outputDirectory>
           <includeScope>runtime</includeScope>
         </configuration>
       </execution>
     </executions>
   </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <artifactSet>
            <includes>
              <include>org.apache.httpcomponents.*</include>
            </includes>
          </artifactSet>
          <relocations>
            <relocation>
              <pattern>org.apache.httpcomponents</pattern>
              <shadedPattern>org.shaded.httpcomponents</shadedPattern>
            </relocation>
          </relocations>
        </configuration>
      </execution>
    </executions>
  </plugin>
   <plugin>
     <artifactId>maven-assembly-plugin</artifactId>
     <executions>
       <execution>
         <id>app-package-assembly</id>
         <phase>package</phase>
         <goals>
           <goal>single</goal>
         </goals>
         <configuration>
           <finalName>${project.artifactId}-${project.version}-apexapp</finalName>
           <appendAssemblyId>false</appendAssemblyId>
           <descriptors>
             <descriptor>src/assemble/appPackage.xml</descriptor>
           </descriptors>
           <archiverConfig>
             <defaultDirectoryMode>0755</defaultDirectoryMode>
           </archiverConfig>
           <archive>
             <manifestEntries>
               <Class-Path>${apex.apppackage.classpath}</Class-Path>
               <DT-Engine-Version>${apex.version}</DT-Engine-Version>
               <DT-App-Package-Group-Id>${project.groupId}</DT-App-Package-Group-Id>
               <DT-App-Package-Name>${project.artifactId}</DT-App-Package-Name>
               <DT-App-Package-Version>${project.version}</DT-App-Package-Version>
               <DT-App-Package-Display-Name>${project.name}</DT-App-Package-Display-Name>
               <DT-App-Package-Description>${project.description}</DT-App-Package-Description>
             </manifestEntries>
           </archive>
         </configuration>
       </execution>
     </executions>
   </plugin>

   <plugin>
     <artifactId>maven-antrun-plugin</artifactId>
     <version>1.7</version>
     <executions>
       <execution>
         <phase>package</phase>
         <configuration>
           <target>
             <move file="${project.build.directory}/${project.artifactId}-${project.version}-apexapp.jar"
                   tofile="${project.build.directory}/${project.artifactId}-${project.version}.apa" />
           </target>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
       </execution>
       <execution>
         <!-- create resource directory for xml javadoc-->
         <id>createJavadocDirectory</id>
         <phase>generate-resources</phase>
         <configuration>
           <tasks>
             <delete dir="${project.build.directory}/generated-resources/xml-javadoc"/>
             <mkdir dir="${project.build.directory}/generated-resources/xml-javadoc"/>
           </tasks>
         </configuration>
         <goals>
           <goal>run</goal>
         </goals>
       </execution>
     </executions>
   </plugin>

   <plugin>
     <groupId>org.codehaus.mojo</groupId>
     <artifactId>build-helper-maven-plugin</artifactId>
     <version>1.9.1</version>
     <executions>
       <execution>
         <id>attach-artifacts</id>
         <phase>package</phase>
         <goals>
           <goal>attach-artifact</goal>
         </goals>
         <configuration>
           <artifacts>
             <artifact>
               <file>target/${project.artifactId}-${project.version}.apa</file>
               <type>apa</type>
             </artifact>
           </artifacts>
           <skipAttach>false</skipAttach>
         </configuration>
       </execution>
     </executions>
   </plugin>

  <!-- generate javdoc -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <executions>
      <!-- generate xml javadoc -->
      <execution>
        <id>xml-doclet</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>javadoc</goal>
        </goals>
        <configuration>
          <doclet>com.github.markusbernhardt.xmldoclet.XmlDoclet</doclet>
          <additionalparam>-d ${project.build.directory}/generated-resources/xml-javadoc -filename ${project.artifactId}-${project.version}-javadoc.xml</additionalparam>
          <useStandardDocletOptions>false</useStandardDocletOptions>
          <docletArtifact>
            <groupId>com.github.markusbernhardt</groupId>
            <artifactId>xml-doclet</artifactId>
            <version>1.0.4</version>
          </docletArtifact>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <!-- Transform xml javadoc to stripped down version containing only class/interface comments and tags-->
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
      <execution>
        <id>transform-xmljavadoc</id>
        <phase>generate-resources</phase>
        <goals>
          <goal>transform</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <transformationSets>
        <transformationSet>
          <dir>${project.build.directory}/generated-resources/xml-javadoc</dir>
          <includes>
            <include>${pmalhar.versioroject.artifactId}-${project.version}-javadoc.xml</include>
          </includes>
          <stylesheet>XmlJavadocCommentsExtractor.xsl</stylesheet>
          <outputDir>${project.build.directory}/generated-resources/xml-javadoc</outputDir>
        </transformationSet>
      </transformationSets>
    </configuration>
  </plugin>
  <!-- copy xml javadoc to class jar -->
  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>process-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/classes</outputDirectory>
          <resources>
            <resource>
              <directory>${project.build.directory}/generated-resources/xml-javadoc</directory>
              <includes>
                <include>${project.artifactId}-${project.version}-javadoc.xml</include>
              </includes>
              <filtering>true</filtering>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

</plugins>

 </build>

  <dependencies>
<!-- add your dependencies here -->
<dependency>
  <groupId>org.apache.apex</groupId>
  <artifactId>malhar-library</artifactId>
  <version>${malhar.version}</version>
  <!--
       If you know that your application does not need transitive dependencies pulled in by malhar-library,
       uncomment the following to reduce the size of your app package.
  -->
  <!--
  <exclusions>
    <exclusion>
      <groupId>*</groupId>
      <artifactId>*</artifactId>
    </exclusion>
  </exclusions>
  -->
</dependency>
<dependency>
  <groupId>org.apache.apex</groupId>
  <artifactId>malhar-contrib</artifactId>
  <version>${malhar.version}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.4</version>
</dependency>
<dependency>
  <groupId>com.github.fge</groupId>
  <artifactId>json-schema-validator</artifactId>
  <version>2.0.1</version>
  <optional>true</optional>
</dependency>
<dependency>
  <groupId>net.sf.supercsv</groupId>
  <artifactId>super-csv</artifactId>
  <version>2.4.0</version>
  <optional>true</optional>
</dependency>
<dependency>
  <groupId>org.apache.apex</groupId>
  <artifactId>apex-common</artifactId>
  <version>${apex.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.apache.apex</groupId>
  <artifactId>apex-engine</artifactId>
  <version>${apex.version}</version>
  <scope>provided</scope>
</dependency>

Two things - 两件事情 -

  1. You should be aware what relocation does is not "...relocated the httpClient dependency" but as the source suggests - 您应该知道重定位不是“...重定位httpClient依赖”,但正如消息来源所暗示的那样 -

    If the uber JAR is reused as a dependency of some other project, directly including classes from the artifact's dependencies in the uber JAR can cause class loading conflicts due to duplicate classes on the class path. 如果将uber JAR重用为某个其他项目的依赖项,则直接在uber JAR中包含工件的依赖项中的类可能会因类路径上的重复类而导致类加载冲突。 To address this issue, one can relocate the classes which get included in the shaded artifact in order to create a private copy of their bytecode 要解决此问题,可以重新定位包含在着色工件中的类,以便创建其字节码的私有副本

  2. When you execute the mvn dependency:tree it would display all the dependencies(direct and transitive) included in your project. 当您执行mvn dependency:tree ,它将显示项目中包含的所有依赖项(直接和传递)。 Instead, the maven shade plugin 相反,maven shade插件

shade:shade is bound to the package phase and is used to create a shaded jar. 阴影:阴影与包装相结合,用于制作阴影罐。

renaming or optimizing the dependencies further. 进一步重命名或优化依赖项。 Here goes the details for Maven Shade Plugin - 以下是Maven Shade插件的详细信息 -

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - ie rename - the packages of some of the dependencies. 这个插件提供了将工件打包在超级jar中的功能,包括它的依赖关系和阴影 - 即重命名 - 一些依赖项的包。

The dependency is not removed from original POM, so the behavior of dependency:tree is correct. 不会从原始POM中删除依赖项,因此dependency:tree的行为是正确的。

Shade plugin will also generate a new POM ( dependency-reduced-pom.xml ) wherein shaded dependencies are removed from the <dependencies> section. Shade插件还将生成一个新的POM( dependency-reduced-pom.xml ),其中从<dependencies>部分删除着色的依赖项。 This looks like the one you are after. 这看起来就像你追求的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM