简体   繁体   English

使用maven在导出的jar文件中包含jar文件和依赖项

[英]Containing a jar file and dependency in your exported jar file with maven

I am trying to program a plugin for BungeeCord (Minecraft) with geoip2. 我正在尝试使用geoip2为BungeeCord(Minecraft)编写插件。 I am trying to contain the dependency "com.maxmind.geoip2" and the BungeeCord API jar file in my jar file because I'm getting a java.lang.NoClassDefFoundError: com/maxmind/geoip2/exception/GeoIp2Exception 我试图在我的jar文件中包含依赖项“ com.maxmind.geoip2”和BungeeCord API jar文件,因为我收到了java.lang.NoClassDefFoundError: com/maxmind/geoip2/exception/GeoIp2Exception

java.lang.NoClassDefFoundError: com/maxmind/geoip2/exception/GeoIp2Exception
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at net.md_5.bungee.api.plugin.PluginManager.enablePlugin(PluginManager.java:305)
at net.md_5.bungee.api.plugin.PluginManager.loadPlugins(PluginManager.java:212)
at net.md_5.bungee.BungeeCord.start(BungeeCord.java:259)
at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:56)
at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15)
Caused by: java.lang.ClassNotFoundException: com.maxmind.geoip2.exception.GeoIp2Exception
at net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53)
at net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27)
at java.lang.Cl

I've tried this , but it didn't work for me. 我已经尝试过 ,但是对我没有用。

While researching more I came across maven shader and I currently have this in my pom.xml: 在研究更多内容时,我遇到了Maven着色器,目前在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>general</groupId>
<artifactId>CubenexBungee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>CubenexBungee</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>com.maxmind.geoip2</groupId>
        <artifactId>geoip2</artifactId>
        <version>2.11.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <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.1.0</version>
     <executions>
        <execution>
           <phase>package</phase>
           <goals>
              <goal>shade</goal>
           </goals>
           <configuration>
              <artifactSet>
                 <excludes>
                    <exclude>org:*</exclude>
                 </excludes>
                 <includes>
                    <include>com.maxmind.geoip2:geoip2</include>
                    <include>Referenced Libraries:BungeeCord.jar</include>
                 </includes>
              </artifactSet>
           </configuration>
        </execution>
     </executions>
  </plugin>
    </plugins>
</build>

And my App.java is: 我的App.java是:

public class App extends Plugin implements Listener{

public static DatabaseReader database;

@Override
public void onEnable(){
    getProxy().getPluginManager().registerListener(this, this);
    try {
        database = new DatabaseReader.Builder(new File(this.getDataFolder().getPath()+"//countrydatabase.csv")).build();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@EventHandler
public void login(LoginEvent e){
    CountryResponse response;
    try {
        response = database.country(e.getConnection().getAddress().getAddress());
        System.out.println(e.getConnection().getName()+" comes from "+response.getCountry().getName());
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (GeoIp2Exception e1) {
        e1.printStackTrace();
    }
}

}

I must say that I've only just found out about maven and don't really understand it very goodly and haven't found any tutorials that I understand. 我必须说,我只是发现了有关Maven的知识,并不太了解它,也没有找到我了解的任何教程。

What you are trying to make is what a lot of people call a 'fat jar' or 'uber jar' 您要制作的东西被很多人称为“胖罐子”或“超大罐子”
You can create this by using the maven-shade-plugin. 您可以使用maven-shade-plugin创建它。
Here is more information about it: http://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/ 这是有关它的更多信息: http : //www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM