Maven 遮罩插件不会遮蔽工件中的依赖项

Posted

技术标签:

【中文标题】Maven 遮罩插件不会遮蔽工件中的依赖项【英文标题】:Maven shader plugin doesn't shade dependencies in artifact 【发布时间】:2019-07-21 12:25:08 【问题描述】:

好的,我已经尝试了更多的依赖项,似乎我拥有的每个依赖项都没有被隐藏到我的 jar 文件中。为什么会这样?

我创建了具有不同组和工件 ID 的不同项目,但我创建的每个项目都遇到相同的问题。

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.paper</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>WeirdPlugin</name>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <defaultGoal>clean package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>$java.version</source>
                    <target>$java.version</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                $project.build.directory/libs
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>
                                com.paper.test.Main
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>
                                        com.paper.test.Main
                                    </mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <transformers>
                                <transformer implementation=
                                                     "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.paper.test.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <repositories>
        <repository>
            <id>destroystokyo-repo</id>
            <url>https://repo.destroystokyo.com/repository/maven-public/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.destroystokyo.paper</groupId>
            <artifactId>paper-api</artifactId>
            <version>1.14.1-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen</artifactId>
            <version>3.11.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
            <version>3.11.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>3.11.11</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>me.lucko.luckperms</groupId>
            <artifactId>luckperms-api</artifactId>
            <version>4.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

TestPlugin.iml:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="minecraft" name="Minecraft">
      <configuration>
        <autoDetectTypes>
          <platformType>PAPER</platformType>
        </autoDetectTypes>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: com.destroystokyo.paper:paper-api:1.14.1-R0.1-SNAPSHOT" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: junit:junit:4.10" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
    <orderEntry type="library" name="Maven: com.google.guava:guava:21.0" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
    <orderEntry type="library" name="Maven: org.ow2.asm:asm:7.1" level="project" />
    <orderEntry type="library" name="Maven: org.ow2.asm:asm-commons:7.1" level="project" />
    <orderEntry type="library" name="Maven: org.ow2.asm:asm-tree:7.1" level="project" />
    <orderEntry type="library" name="Maven: org.ow2.asm:asm-analysis:7.1" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven.plugins:maven-shade-plugin:3.2.1" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-plugin-api:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.sisu:sisu-inject-plexus:1.4.2" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.sisu:sisu-inject-bean:1.4.2" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.sisu:sisu-guice:noaop:2.1.7" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-model:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-core:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-settings:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-settings-builder:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-repository-metadata:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-model-builder:3.0" level="project" />
    <orderEntry type="library" scope="RUNTIME" name="Maven: org.apache.maven:maven-aether-provider:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.aether:aether-impl:1.7" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.aether:aether-spi:1.7" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.aether:aether-api:1.7" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.aether:aether-util:1.7" level="project" />
    <orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-interpolation:1.14" level="project" />
    <orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-classworlds:2.2.3" level="project" />
    <orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-component-annotations:1.5.5" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.plexus:plexus-sec-dispatcher:1.3" level="project" />
    <orderEntry type="library" name="Maven: org.sonatype.plexus:plexus-cipher:1.4" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven:maven-artifact:3.0" level="project" />
    <orderEntry type="library" name="Maven: org.codehaus.plexus:plexus-utils:3.1.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven.shared:maven-artifact-transfer:0.10.0" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven.shared:maven-common-artifact-filters:3.0.1" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven.shared:maven-shared-utils:3.1.0" level="project" />
    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.11" level="project" />
    <orderEntry type="library" name="Maven: org.jdom:jdom2:2.0.6" level="project" />
    <orderEntry type="library" name="Maven: org.apache.maven.shared:maven-dependency-tree:3.0.1" level="project" />
    <orderEntry type="library" name="Maven: org.eclipse.aether:aether-util:0.9.0.M2" level="project" />
    <orderEntry type="library" name="Maven: commons-io:commons-io:2.5" level="project" />
    <orderEntry type="library" name="Maven: org.vafer:jdependency:2.1.1" level="project" />
    <orderEntry type="library" name="Maven: org.ow2.asm:asm-util:7.0-beta" level="project" />
    <orderEntry type="library" name="Maven: org.jooq:jooq-codegen:3.11.11" level="project" />
    <orderEntry type="library" name="Maven: org.jooq:jooq-meta:3.11.11" level="project" />
    <orderEntry type="library" name="Maven: org.jooq:jooq:3.11.11" level="project" />
    <orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.2.12" level="project" />
    <orderEntry type="library" name="Maven: javax.annotation:javax.annotation-api:1.2" level="project" />
    <orderEntry type="library" scope="PROVIDED" name="Maven: me.lucko.luckperms:luckperms-api:4.4" level="project" />
  </component>
</module>

我得到NoClassDefFoundError,因为我想做的一切都取决于依赖项之一。 我已经尝试了很多东西编辑 pom.xml 和 TestPlugin.iml 在很多方面仍然没有,只有依赖 bukkit/paper 依赖的东西工作得很好,我现在一无所知。

使用了 mvn clean install 和 mvn dependency:tree,它运行良好,但是当我把它放在服务器的插件中时,它就坏了。

关注此tutorial 并更改了我必须更改的所有内容以更改相同的结果。

【问题讨论】:

感谢您的加入。看来这与maven-shade-plugin如何配合创建插件有关。我不确定这是如何工作的,但我会在那里寻找问题。代码生成可能在 IntelliJ 中起作用的原因是您复制了配置。您还使用了jooq-codegen-maven 插件,这对于您的程序代码生成器配置来说似乎是多余的。 我确实通过maven使用了代码生成器,直到我找到了程序化代码生成器。 还有问题是我的设置方式还是我可能做错了什么? 我不知道。我告诉了你我的怀疑,但你必须最终验证。 更改了配置,而不是为其创建方法,而是将其放入 onEnable 并删除了 maven code-gen 插件仍然是同样的问题。 【参考方案1】:

最终用这段代码修复了它:

<?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.DGD</groupId>
    <artifactId>PapaPlugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>PapaPlugin</name>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <defaultGoal>clean package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>false</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <repositories>
        <repository>
            <id>destroystokyo-repo</id>
            <url>https://repo.destroystokyo.com/repository/maven-public/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>http://jcenter.bintray.com/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.destroystokyo.paper</groupId>
            <artifactId>paper-api</artifactId>
            <version>1.12.2-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>me.lucko.luckperms</groupId>
            <artifactId>luckperms-api</artifactId>
            <version>4.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>3.8.3_464</version>
            <exclusions>
                <exclusion>
                    <groupId>club.minnced</groupId>
                    <artifactId>opus-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>3.11.11</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
            <version>3.11.11</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen</artifactId>
            <version>3.11.11</version>
        </dependency>
    </dependencies>
</project>

【讨论】:

以上是关于Maven 遮罩插件不会遮蔽工件中的依赖项的主要内容,如果未能解决你的问题,请参考以下文章

使用提供的工件作为 Maven 插件依赖

为啥有时会跳过 maven 依赖项中的版本号?

如何找出是不是在 Maven 应用程序中使用了依赖项中的任何类?

Maven 阴影插件重定位不更新资源文件中的条目

可以使Maven构建包含依赖项中的.class文件[重复]

为啥即使我的 maven 依赖项中有第三方依赖项,也只需要 OSGi 容器中的第三方依赖项?