Using External Jars(using Maven shading)

Discussion in 'Plugin Development' started by HeyAwesomePeople, Jun 22, 2014.

Thread Status:
Not open for further replies.
  1. Hello! I am very new to Maven and I am setting up a redis database on Ubuntu 14.04 that I want to test the use of in one of my plugins. I am using Jedis(https://github.com/xetorthio/jedis) as an API to connect to the redis database. I wrote a simple "set" method and exported the project.
    When running, I get this: http://hastebin.com/finuqaxuhe.profile

    I understand that this is happening because the plugin doesn't know where the Jedis.jar is. But this is how I have it setup, thinking the plugin should know everything if I did this: http://i.imgur.com/iHP7CFT.png

    Apparently not... So then I heard about this Maven "shading" thing, but have no clue how to use it. This is my current Maven pom file:
    Code:
    <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>me.heyawesomepeople</groupId>
        <artifactId>Maven-KitPVP</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>Maven-KitPVP</name>
        <description>A plugin for a Minecraft KitPVP server.</description>
     
        <repositories>
            <repository>
                <id>bukkit-repo</id>
                <url>http://repo.bukkit.org/content/groups/public/</url>
            </repository>
            <repository>
                <id>dj-tcraft repository</id>
                <url>http://ci.dj-tcraft.nl/plugin/repository/everything/</url>
            </repository>
            <repository>
                <id>confuser-repo</id>
                <url>http://ci.frostcast.net/plugin/repository/everything</url>
            </repository>
        </repositories>
     
        <build>
            <finalName>KitPVP</finalName>
            <sourceDirectory>src/main/java</sourceDirectory>
            <resources>
                <resource>
                    <targetPath>.</targetPath>
                    <filtering>true</filtering>
                    <directory>${basedir}/src/main/resources/</directory>
                    <includes>
                        <include>
                              plugin.yml
                          </include>
                        <include>
                              config.yml
                          </include>
                        <include>
                              info.txt
                          </include>
                    </includes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <includes>
                                        <include></include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
     
            </plugins>
        </build>
     
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
     
        <dependencies>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>LATEST</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>craftbukkit</artifactId>
                <version>LATEST</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.bergerkiller.bukkit</groupId>
                <artifactId>BKCommonLib</artifactId>
                <version>1.58</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>me.confuser</groupId>
                <artifactId>BarAPI</artifactId>
                <version>3.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.5.1</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
        </dependencies>
     
    </project>
    How would I shade the Jedis and CommonPool2 so that they won't show NoClassDefFoundError and ClassNotFoundErrors?
    Thanks,
    HeyAwesomePeople
     
  2. Offline

    aaomidi

    Code:
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.1</version>
                    <configuration>
                        <!-- put your configurations here -->
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    The Maven shade plugin.
     
  3. aaomidi I already have this but I have no clue what to put in between the <include> tags.
    Code:
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <includes>
                                        <include></include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
     
  4. Offline

    fireblast709

    HeyAwesomePeople an artifact is a jar, for example. Thus something like
    Code:
    <include>groupId:artifactId:*:*</include>
    should work. (version independant)

    [edit] small example
    Code:
    <include>org.bukkit:bukkit:*:*</include>
    would tell it to shade Bukkit in your plugin. Then again, never shade Bukkit in your plugins :p
     
  5. fireblast709 So for example I want to shade this into my plugin:
    Code:
    <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.5.1</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
    I would just do this?
    Code:
    <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <includes>
                                        <include>redis.clients:jedis:*:*</include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
     
  6. Offline

    fireblast709

Thread Status:
Not open for further replies.

Share This Page