使用Maven无法对JAR进行着色(INVOKESPECIAL/STATIC)。

6
完整的错误信息如下:

无法在项目(默认情况下)上执行 org.apache.maven.plugins:maven-shade-plugin:2.3:创建带阴影的 JAR 文件时出错:对于接口,INVOKESPECIAL/STATIC 需要 ASM 5 -> [Help 1]

我试图处理的 jar 文件位于 我的个人远程仓库 中,使用 Sonatype Nexus。以下是我的 pom 配置:
<?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.codelanx</groupId>
    <artifactId>phanaticprison</artifactId>
    <version>1.0.0</version>
    <name>PhanaticPrison</name>
    <packaging>jar</packaging>

    <repositories>
        <repository>
            <id>bukkit-repo</id>
            <url>http://repo.bukkit.org/content/repositories/public/</url>
        </repository>
        <repository>
            <id>codelanx-repo</id>
            <url>http://repo.codelanx.com/content/repositories/public/</url>
        </repository>
    </repositories>
    <licenses>
        <license>
            <name>Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International</name>
            <url>https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode</url>
            <distribution>repo</distribution>
        </license>
    </licenses>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>com.codelanx:codelanxlib</include>
                        </includes>
                    </artifactSet>
                    <minimizeJar>true</minimizeJar>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <targetPath>.</targetPath>
                <directory>${basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <finalName>${project.name}</finalName>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.bukkit</groupId>
            <artifactId>bukkit</artifactId>
            <version>LATEST</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.codelanx</groupId>
            <artifactId>codelanxlib</artifactId>
            <version>LATEST</version>
            <type>jar</type>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

在谷歌上搜索这个错误的结果非常少,我收集到的主要信息是Java 8的静态接口方法可能会导致这个错误,但我没有找到更多的信息。我正在尝试打包的jar文件确实有一个这样的静态方法,但即使如此,我肯定也能使用它们并打包这个jar文件吧?我不确定这个错误需要什么“ASM 5”(汇编?),所以希望得到任何指导。
我的有效pom文件(mvn help:effective-pom):
<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.phanaticmc</groupId>
  <artifactId>phanaticprison</artifactId>
  <version>1.0.0</version>
  <name>PhanaticPrison</name>
  <licenses>
    <license>
      <name>Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International</name>
      <url>https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.bukkit</groupId>
      <artifactId>bukkit</artifactId>
      <version>LATEST</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.codelanx</groupId>
      <artifactId>codelanxlib</artifactId>
      <version>LATEST</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <id>bukkit-repo</id>
      <url>http://repo.bukkit.org/content/repositories/public/</url>
    </repository>
    <repository>
      <id>codelanx-repo</id>
      <url>http://repo.codelanx.com/content/repositories/public/</url>
    </repository>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\src\main\java</sourceDirectory>
    <scriptSourceDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\src\test\java</testSourceDirectory>
    <outputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\classes</outputDirectory>
    <testOutputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <targetPath>.</targetPath>
        <filtering>true</filtering>
        <directory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target</directory>
    <finalName>PhanaticPrison</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
      </plugin>
      <plugin>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <artifactSet>
                <includes>
                  <include>com.codelanx:codelanxlib</include>
                </includes>
              </artifactSet>
              <minimizeJar>true</minimizeJar>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <artifactSet>
            <includes>
              <include>com.codelanx:codelanxlib</include>
            </includes>
          </artifactSet>
          <minimizeJar>true</minimizeJar>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.10</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.3.1</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>C:\Users\Spencer\Documents\NetBeansProjects\PhanaticPrison\target\site</outputDirectory>
  </reporting>
</project>

完整的Maven堆栈跟踪:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (default) on project phanaticprison: Error creating shaded jar: INVOKESPECIAL/STATIC on interfaces require ASM 5
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error creating shaded jar: INVOKESPECIAL/STATIC on interfaces require ASM 5
    at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:566)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 19 more
Caused by: java.lang.IllegalArgumentException: INVOKESPECIAL/STATIC on interfaces require ASM 5
    at org.objectweb.asm.MethodVisitor.visitMethodInsn(Unknown Source)
    at org.objectweb.asm.commons.RemappingMethodAdapter.doVisitMethodInsn(Unknown Source)
    at org.objectweb.asm.commons.RemappingMethodAdapter.visitMethodInsn(Unknown Source)
    at org.objectweb.asm.ClassReader.a(Unknown Source)
    at org.objectweb.asm.ClassReader.b(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.vafer.jdependency.Clazzpath.addClazzpathUnit(Clazzpath.java:194)
    at org.vafer.jdependency.Clazzpath.addClazzpathUnit(Clazzpath.java:130)
    at org.apache.maven.plugins.shade.filter.MinijarFilter.addDependencyToClasspath(MinijarFilter.java:101)
    at org.apache.maven.plugins.shade.filter.MinijarFilter.<init>(MinijarFilter.java:81)
    at org.apache.maven.plugins.shade.mojo.ShadeMojo.getFilters(ShadeMojo.java:805)
    at org.apache.maven.plugins.shade.mojo.ShadeMojo.execute(ShadeMojo.java:458)
    ... 21 more

将依赖插件更新为2.8和汇编插件更新为2.4,然后重试。 - jmj
@JigarJoshi 目前我正在使用 NetBeans 自带的 Maven,下载和安装 Maven 3.x 可能会解决所有这些问题吗? - Rogue
尝试只更新pom.xml中这两个插件,就像您使用版本2.3的shade插件一样。 - jmj
@JigarJoshi 很遗憾,还是同样的错误。 - Rogue
在我看来,这似乎是汇编插件中的错误。我会通过确保最新的Maven版本存在来报告它(您已经使用了最新的Maven插件),只需将Maven更新到最新版本,如果问题仍然存在,请将其报告给汇编插件问题http://jira.codehaus.org/browse/MNG。 - jmj
3个回答

9

由于我最近也遇到了同样的问题,并花了很长时间才解决,所以这是我所做的:

ASM所需的版本是5.0.3。然而,阴影插件中当前可用的版本是5.0.2。查看JIRA票证,它看起来已经在2.4中修复了,但该版本尚未公开发布(我写这篇文章时是2015年5月4日)。

因此,您需要做的就是通过将以下部分添加到您的pom中激活开发人员版本(基于https://maven.apache.org/guides/development/guide-testing-development-plugins.html):

<pluginRepositories>
    <pluginRepository>
        <id>apache.snapshots</id>
        <url>http://repository.apache.org/snapshots/</url>
    </pluginRepository>
</pluginRepositories>

将插件版本设置为2.4-SNAPSHOT

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4-SNAPSHOT</version>
    </plugin>
</plugins>

希望这能帮到你!

1
这很有帮助,因为您可以使用我几个月前提到的固定依赖项。仍在等待包含修复程序的发布。 - Bram

2

我已经查看了源代码,发现消息"在接口上调用INVOKESPECIAL/STATIC需要ASM 5"的意思是MethodVisitor是一个ASM 5方法,但未用正确的参数实例化。

我提交了一个jdependency的补丁,并被接受。为了获得好处,您必须明确使用它:

<dependencies>
     <dependency>
     <groupId>org.vafer</groupId>
     <artifactId>jdependency</artifactId>
     <version>1.0</version>
     </dependency>
</dependencies>

虽然应该包含在maven-shade-plugin版本2.4中。 另请参见http://jira.codehaus.org/browse/MSHADE-174


0
我猜“ASM”是指asm库。确保你使用的是Maven 3而不是Maven 2.x,并且个人建议在命令行上尝试,而不是在IDE中。
当我在包含shade插件版本2.3的项目上使用Maven 3.0.5运行mvn dependency:resolve-plugins时,输出中会显示这些依赖项:
[INFO] Plugin Resolved: maven-shade-plugin-2.3.jar
[INFO]     Plugin Dependency Resolved: maven-plugin-api-3.0.jar
[INFO]     Plugin Dependency Resolved: maven-model-3.0.jar
[INFO]     Plugin Dependency Resolved: maven-core-3.0.jar
[INFO]     Plugin Dependency Resolved: maven-compat-3.0.jar
[INFO]     Plugin Dependency Resolved: maven-artifact-3.0.jar
[INFO]     Plugin Dependency Resolved: plexus-container-default-1.0-alpha-9-stable-1.jar
[INFO]     Plugin Dependency Resolved: plexus-utils-3.0.15.jar
[INFO]     Plugin Dependency Resolved: asm-5.0.2.jar
[INFO]     Plugin Dependency Resolved: asm-commons-5.0.2.jar
[INFO]     Plugin Dependency Resolved: jdom-1.1.jar
[INFO]     Plugin Dependency Resolved: maven-dependency-tree-2.1.jar
[INFO]     Plugin Dependency Resolved: jdependency-0.9.jar
[INFO]     Plugin Dependency Resolved: guava-11.0.2.jar

请注意 ASM 库,它们都是版本 5,这似乎是错误所要求的。如果您在命令行上使用 Maven 3 运行仍然出现错误,则我同意 Jigar 的观点,这似乎是插件中的一个 bug。

很奇怪的是,即使使用了Maven 3和ASM 5插件,它仍然提示需要ASM 5的错误:http://pastie.org/pastes/9324894/text - Rogue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接