0

My pom.xml looks as follows:

  <!-- 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>

But when my jar is created I get the following error while running 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$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.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

I also have dependency as

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

in my pom.

  1. How does it work? I don't have maven-plugin-api in my local repository. Is it taking it from my Maven (application) lib folder to run it because normally it runs perfectly. And if it is taking from my maven folder I have maven-3.3.1 and the specified version is 3.2.5.

  2. How can I make it work?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
user1615664
  • 591
  • 2
  • 11
  • 24
  • Why would you like to do this? Apart from that i assume that you have a `maven-plugin-api` somewhere in your repository.... – khmarbaise Nov 19 '15 at 14:01
  • I am basically converting an maven plugin to a java project so it can be executed on a remote PC without maven being installed. I need this for that. If it would help it works perfectly when I run my main from eclipse. – user1615664 Nov 19 '15 at 14:03
  • $ java -classpath "C:\Users\works\maven-plugin-api\3.2.5\maven-plugin-api-3.2.5.jar" -jar "C:\tools\main\target\main-2.0.0-SNAPSHOT.jar" If it helps even this does not work – user1615664 Nov 19 '15 at 14:08

2 Answers2

1

By default, the maven-shade-plugin will ignore all provided dependencies. As such, the maven-plugin-api dependency, which has a provided scope, will not be kept.

This is expected: provided dependencies are supposed to be made available at runtime by the container. There are two solutions:

  • Remove the provided scope from the dependency declaration. It will be then included inside the uber jar.
  • Keep the provided scope but make the maven-plugin-api artifact available at runtime by adding it to the classpath when your application is launched.
Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Sorry Got too Excited doesn't work. keepDependenciesWithProvidedScope: When true, dependencies are kept in the pom but with scope 'provided'; when false, the dependency is removed. This is a bit weird statement. Have you done this before? I tried true as well as this false Jar is still the same and provided are not included. But that is the correct diagnosis. How do I make it work. I don't want to change scope to compile. – user1615664 Nov 19 '15 at 14:50
  • @user1615664 Yes, this parameter only affects the dependency reduces POM. I've edited my answer. – Tunaki Nov 19 '15 at 14:59
  • I shifted to maven-assembly-plugin. It did the job for me. Thanks for your help. – user1615664 Nov 19 '15 at 16:02
1

When you say <scope>provided</scope> you are saying that the related artifact does not need to be included, because it will "magically" be available in the classpath.

See Difference between maven scope compile and provided for JAR packaging for more information.

Community
  • 1
  • 1
Rob
  • 6,247
  • 2
  • 25
  • 33