• 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。
  • 相关阅读:
    题解-CF1375E Inversion SwapSort
    寒门再难出贵子
    js获取链接中的内容方法
    MySQL添加用户、删除用户、授权及撤销权限
    Ubuntu保存退出vim编辑器
    最全!Linux服务器下安装SVN,并添加SVN项目,自动更新项目文件到web目录
    php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别以及REQUEST_URI的讲解
    RESTful API 最佳实践----转载阮一峰
    PHP图像处理(GD库)
    nginx.conf配置
  • 原文地址:https://www.cnblogs.com/WLCYSYS/p/15181175.html
Copyright © 2020-2023  润新知