tgn
tgn

Reputation: 13

Adding external jar to project using maven-shading

My goal is to add an external jar into my Maven-project. Running the project invokes an NoClassDefFoundError. So I did a little research to found out that possibly the external jar is not included in my created jar file.

Therefor I found the solution to use maven-shade-plugin, but I'm stuck a little because the project will not compile.

I've got the following pom.xml:

<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>this.isa.test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1</version>
  <build>
   <plugins>
       <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-compiler-plugin</artifactId>
           <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.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
   </plugins>
</build>
<repositories>
    <repository>
      <id>spigot-repo</id>
      <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
    </repository>
</repositories>
<dependencies>
       <dependency>
           <groupId>org.bukkit</groupId>
           <artifactId>bukkit</artifactId>
           <version>1.12.2-R0.1-SNAPSHOT</version><!--change this value depending on the version or use LATEST-->
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.spigotmc</groupId>
           <artifactId>spigot-api</artifactId>
           <version>1.12.2-R0.1-SNAPSHOT</version><!--change this value depending on the version-->
           <type>jar</type>
           <scope>provided</scope>
       </dependency>
       <dependency>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>3.2.0</version>
         <type>maven-plugin</type>
        </dependency>
   </dependencies>
</project>

Note that maven-shade-plugin is referenced as a dependency and included as an plugin for the build. I'm compiling the project through the Eclipse IDE with the preinstalled maven integration like the following mvn-install.

The following Error messages will be given in the Console:

[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for org.apache.maven.plugins:maven-shade-plugin:jar:3.2 is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.195 s
[INFO] Finished at: 2018-09-30T09:48:43+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin org.apache.maven.plugins:maven-shade-plugin:3.2 or one of its dependencies could not be resolved: Failure to find org.apache.maven.plugins:maven-shade-plugin:jar:3.2 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

To be honest, I'm a little overchallenged because it's my first attempt to use Maven. There are plenty of questions and answers here and all over the internet, but nothing seems to fit my situation.

Upvotes: 1

Views: 722

Answers (1)

Petr Kozelka
Petr Kozelka

Reputation: 7990

I am not using Eclipse, but instead invoking maven from commandline - and for making the thing work, I would recommend the same to you. Once you have maven build working, you can try the next challenge with Eclipse, which should not be hard anyway.

Your pom needs following fixes:

  • remove the shade plugin from dependencies; dependencies should contain only artifacts that are needed for javac to compile the project, in your case also artifacts needed in runtime, but never (with very few exceptions) maven tooling artifacts

  • fix the shade plugin version to 3.2.0 (you have it correctly in your dependency, but not in plugin declaration)

Then try mvn clean install on commandline (make sure you are in the same directory where your pom file exists) and it should work - at least it did for me.

Upvotes: 1

Related Questions