执行jar包上次遇到过一个问题,这次又出现一个,同样是idea执行没问题,达成jar包运行jar包出现问题
    log4j2 是采用的插件式编程,当log4j2包编译时,或者含有log4j2插件的包编译时,会将需要加载的插件信息放在META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat这个文件中(包括官方logj42的原生插件),然后项目启动的时候,log4j2会在各个jar包的META-INF目录下扫描这个插件信息文件,然后去加载插件。

    但是当项目被打成一个jar包时,如果两个不同的jar包中都有Log4j2Plugins.dat 这个文件,就会出现问题,其中一个文件会被另一个覆盖,导致项目启动的时候有一个文件中的插件不能被正常加载,导致报错。

    解决这个问题就是当所有jar包被打成一个jar包时,需要对各个jar包中的Log4j2Plugins.dat进行合并,这就是maven-shade-plugin.log4j2-cachefile-transformer这个包所做的事情了。

    解决
    使用mavenShadePlugin.log4j2CacheTransformer打jar包

    1. <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer"/>

    在plugin中加上依赖,要不然上面的类会找不到

    1. <dependencies>
    2. <dependency>
    3. <groupId>com.github.edwgiz</groupId>
    4. <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
    5. <version>2.6.1</version>
    6. </dependency>
    7. </dependencies>

    完整的

    1. <build>
    2. <finalName>my-python</finalName>
    3. <plugins>
    4. <!-- Maven Shade Plugin -->
    5. <plugin>
    6. <groupId>org.apache.maven.plugins</groupId>
    7. <artifactId>maven-shade-plugin</artifactId>
    8. <version>2.4.3</version>
    9. <configuration>
    10. <createDependencyReducedPom>true</createDependencyReducedPom>
    11. </configuration>
    12. <executions>
    13. <execution>
    14. <phase>package</phase>
    15. <goals>
    16. <goal>shade</goal>
    17. </goals>
    18. <configuration>
    19. <transformers>
    20. <transformer
    21. implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer"/>
    22. </transformers>
    23. <filters>
    24. <filter>
    25. <artifact>*:*</artifact>
    26. <excludes>
    27. <exclude>META-INF/*.SF</exclude>
    28. <exclude>META-INF/*.DSA</exclude>
    29. <exclude>META-INF/*.RSA</exclude>
    30. </excludes>
    31. </filter>
    32. </filters>
    33. </configuration>
    34. </execution>
    35. </executions>
    36. <dependencies>
    37. <dependency>
    38. <groupId>com.github.edwgiz</groupId>
    39. <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
    40. <version>2.6.1</version>
    41. </dependency>
    42. </dependencies>
    43. </plugin>
    44. </plugins>
    45. </build>

    dependencies在build里有时会导致找不到包,在pom依赖添加依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.apache.logging.log4j</groupId>
    4. <artifactId>log4j-core</artifactId>
    5. <version>2.12.1</version>
    6. <scope>compile</scope>
    7. </dependency>
    8. <dependency>
    9. <groupId>org.apache.logging.log4j</groupId>
    10. <artifactId>log4j-api</artifactId>
    11. <version>2.12.1</version>
    12. </dependency>
    13. <dependency>
    14. <groupId>org.apache.logging.log4j</groupId>
    15. <artifactId>log4j-jcl</artifactId>
    16. <version>2.12.1</version>
    17. </dependency>
    18. <dependency>
    19. <groupId>org.apache.logging.log4j</groupId>
    20. <artifactId>log4j-slf4j-impl</artifactId>
    21. <version>2.12.1</version>
    22. </dependency>
    23. <dependency>
    24. <groupId>com.github.edwgiz</groupId>
    25. <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
    26. <version>2.6.1</version>
    27. </dependency>