我无法使用 maven-shade-plugin 将 maven-plugin-api 打包到 uber jar 中

I cannot package maven-plugin-api into uber jar using maven-shade-plugin

我的 pom.xml 看起来如下:

  <!-- Use shade plugin for uber jar -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
    <filters>
     <filter>
       <artifact>maven-plugin-api</artifact>
       <includes>
         <include>org/apache/maven/**</include>
       </includes>
     </filter>
     </filters>
      <shadedArtifactAttached>false</shadedArtifactAttached>
      <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <mainClass>com.abc.def.ver.main.Main</mainClass>
        </transformer>
      </transformers>
      <createDependencyReducedPom>false</createDependencyReducedPom>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

但是当我创建 jar 时,我在 运行ning main:

时收到以下错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/maven/plugin/descriptor/PluginDescriptor
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
        at java.lang.Class.privateGetMethodRecursive(Class.java:3035)
        at java.lang.Class.getMethod0(Class.java:3005)
        at java.lang.Class.getMethod(Class.java:1771)
        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.maven.plugin.descriptor.PluginDescriptor
        at java.net.URLClassLoader.run(URLClassLoader.java:372)
        at java.net.URLClassLoader.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 7 more

我也有依赖性

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-plugin-api</artifactId>
  <version>3.2.5</version>
  <scope>provided</scope>
</dependency>

在我的 pom 中。

  1. 它是如何工作的?我的本地存储库中没有 maven-plugin-api。是否将它从我的 Maven(应用程序)lib 文件夹带到 运行,因为通常它 运行 是完美的。如果它是从我的 maven 文件夹中获取的,我有 maven-3.3.1 并且指定的版本是 3.2.5。

  2. 我怎样才能让它发挥作用?

默认情况下,maven-shade-plugin 将忽略所有提供的依赖项。因此,将不会保留具有 provided 范围的 maven-plugin-api 依赖项。

这是预期的:provided dependencies 应该由容器在运行时提供。有两种解决方案:

  • 从依赖声明中删除提供的范围。然后它将包含在 uber jar 中。
  • 保持提供的作用域,但在启动应用程序时将 maven-plugin-api 工件添加到类路径中,使它在运行时可用。

当您说 <scope>provided</scope> 时,您是在说不需要包含相关工件,因为它将 "magically" 在类路径中可用。

有关详细信息,请参阅 Difference between maven scope compile and provided for JAR packaging