zoukankan      html  css  js  c++  java
  • Selecting Contents for Uber JAR

    Selecting Contents for Uber JAR

    The POM snippet below shows how to control which project dependencies should be included/excluded in the uber JAR:

    1. <project>
    2. ...
    3. <build>
    4. <plugins>
    5. <plugin>
    6. <groupId>org.apache.maven.plugins</groupId>
    7. <artifactId>maven-shade-plugin</artifactId>
    8. <version>3.2.4</version>
    9. <executions>
    10. <execution>
    11. <phase>package</phase>
    12. <goals>
    13. <goal>shade</goal>
    14. </goals>
    15. <configuration>
    16. <artifactSet>
    17. <excludes>
    18. <exclude>classworlds:classworlds</exclude>
    19. <exclude>junit:junit</exclude>
    20. <exclude>jmock:*</exclude>
    21. <exclude>*:xml-apis</exclude>
    22. <exclude>org.apache.maven:lib:tests</exclude>
    23. <exclude>log4j:log4j:jar:</exclude>
    24. </excludes>
    25. </artifactSet>
    26. </configuration>
    27. </execution>
    28. </executions>
    29. </plugin>
    30. </plugins>
    31. </build>
    32. ...
    33. </project>

    Of course, <includes> can be used as well to specify a white list of artifacts. Artifacts are denoted by a composite identifier of the form groupId:artifactId[[:type]:classifier]. Since plugin version 1.3, the wildcard characters '*' and '?' can be used to do glob-like pattern matching.

    For fine-grained control of which classes from the selected dependencies are included, artifact filters can be used:

    1. <project>
    2. ...
    3. <build>
    4. <plugins>
    5. <plugin>
    6. <groupId>org.apache.maven.plugins</groupId>
    7. <artifactId>maven-shade-plugin</artifactId>
    8. <version>3.2.4</version>
    9. <executions>
    10. <execution>
    11. <phase>package</phase>
    12. <goals>
    13. <goal>shade</goal>
    14. </goals>
    15. <configuration>
    16. <filters>
    17. <filter>
    18. <artifact>junit:junit</artifact>
    19. <includes>
    20. <include>junit/framework/**</include>
    21. <include>org/junit/**</include>
    22. </includes>
    23. <excludes>
    24. <exclude>org/junit/experimental/**</exclude>
    25. <exclude>org/junit/runners/**</exclude>
    26. </excludes>
    27. </filter>
    28. <filter>
    29. <artifact>*:*</artifact>
    30. <excludes>
    31. <exclude>META-INF/*.SF</exclude>
    32. <exclude>META-INF/*.DSA</exclude>
    33. <exclude>META-INF/*.RSA</exclude>
    34. </excludes>
    35. </filter>
    36. </filters>
    37. </configuration>
    38. </execution>
    39. </executions>
    40. </plugin>
    41. </plugins>
    42. </build>
    43. ...
    44. </project>

    Here, Ant-like patterns are used to specify that from the dependency junit:junit only certain classes/resources should be included in the uber JAR. The second filter demonstrates the use of wildcards for the artifact identity which was introduced in plugin version 1.3. It excludes all signature related files from every artifact, regardless of its group or artifact id.

    Besides user-specified filters, the plugin can also be configured to automatically remove all classes of dependencies that are not used by the project, thereby minimizing the resulting uber JAR:

    1. <project>
    2. ...
    3. <build>
    4. <plugins>
    5. <plugin>
    6. <groupId>org.apache.maven.plugins</groupId>
    7. <artifactId>maven-shade-plugin</artifactId>
    8. <version>3.2.4</version>
    9. <executions>
    10. <execution>
    11. <phase>package</phase>
    12. <goals>
    13. <goal>shade</goal>
    14. </goals>
    15. <configuration>
    16. <minimizeJar>true</minimizeJar>
    17. </configuration>
    18. </execution>
    19. </executions>
    20. </plugin>
    21. </plugins>
    22. </build>
    23. ...
    24. </project>

    As of version 1.6, minimizeJar will respect classes that were specifically marked for inclusion in a filter. Note that specifying an include filter for classes in an artifact implicitly excludes all non-specified classes in that artifact. <excludeDefaults>false<excludeDefaults> will override this behavior so that all non-specified classes still will be included though.

    1. <project>
    2. ...
    3. <build>
    4. <plugins>
    5. <plugin>
    6. <groupId>org.apache.maven.plugins</groupId>
    7. <artifactId>maven-shade-plugin</artifactId>
    8. <version>3.2.4</version>
    9. <executions>
    10. <execution>
    11. <phase>package</phase>
    12. <goals>
    13. <goal>shade</goal>
    14. </goals>
    15. <configuration>
    16. <minimizeJar>true</minimizeJar>
    17. <filters>
    18. <filter>
    19. <artifact>log4j:log4j</artifact>
    20. <includes>
    21. <include>**</include>
    22. </includes>
    23. </filter>
    24. <filter>
    25. <artifact>commons-logging:commons-logging</artifact>
    26. <includes>
    27. <include>**</include>
    28. </includes>
    29. </filter>
    30. <filter>
    31. <artifact>foo:bar</artifact>
    32. <excludeDefaults>false</excludeDefaults>
    33. <includes>
    34. <include>foo/Bar.class</include>
    35. </includes>
    36. </filter>
    37. </filters>
    38. </configuration>
    39. </execution>
    40. </executions>
    41. </plugin>
    42. </plugins>
    43. </build>
    44. ...
    45. </project>
    如有错误,恳求读者指出,发送到wu13213786609@outlook.com。
  • 相关阅读:
    mysql-8.0.16-winx64/Linux修改root用户密码
    MYSQL学习笔记/2019
    博客论坛系统数据库之表的设计
    MySql-8.0.16版本部分安装问题修正
    将博客搬至CSDN
    解决远程连不到CentOS7虚拟机或ifconfig中没有ens33
    Windows本地运行调试Spark或Hadoop程序失败:ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path
    CentOS7安装Git-2.22.1
    CentOS7安装SVN1.9.12
    Storm本地启动拓扑报错:Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/15181175.html
Copyright © 2011-2022 走看看