Maven在Spigot上未运行。

huangapple go评论58阅读模式
英文:

Maven not running with Spigot

问题

我正在尝试在 **Spigot 1.16.1** 插件中使用 **MongoDB**但我的 **maven** 导入出现了问题

在我的 IDEEclipseIDE中没有错误如果我导出插件并重新加载服务器会出现以下错误

[![服务器重新加载时出现的错误][1]][1]

  [1]: https://i.stack.imgur.com/CJGEQ.png

*pom.xml*

```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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>LeeCraft-Core_hj.2_v.2.0</groupId>
    <artifactId>LeeCraft-Core_hj.2_v.2.0</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>
</project>

Main Class

package de.philipp.main;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import de.philipp.api.OnlineTimeAPI;
import de.philipp.data.Data;
import de.philipp.listener.JoinListener;
import de.philipp.mongodb.MongoDBHandler;
import de.philipp.mysql.MySQL;

public class Main extends JavaPlugin {

    @Override
    public void onDisable() {
    }

    @Override
    public void onEnable() {

        // MongoDB 连接
        
        MongoDBHandler.connect();


        // 启动插件消息

        Bukkit.getConsoleSender().sendMessage("§b§l  _            §r§3 ___           __ _        _        ___               _    _      ___       _ _          ");
        Bukkit.getConsoleSender().sendMessage("§b§l | |   ___ ___ §r§3/ __|_ _ __ _ / _| |_   __| |___   / __|___ _ _ ___  (_)__| |_   / _ \\ _ _ | (_)_ _  ___ ");
        Bukkit.getConsoleSender().sendMessage("§b§l | |__/ -_) -_§r§3 (__| '_/ _` |  _|  _|_/ _` / -_) | (__/ _ \\ '_/ -_) | (_-<  _| | (_) | ' \\| | | ' \\/ -_)");
        Bukkit.getConsoleSender().sendMessage("§b§l |____\\___\\__§r§3_|\\___|_| \\__,_|_|  \\__(_)__,_\\___|  \\___\\___/_| \\___| |_/__\\__|  \\___/|_||_|_|_|_||_\\___|");
        Bukkit.getConsoleSender().sendMessage(" ");
        
        // 监听器注册

        getServer().getPluginManager().registerEvents(new JoinListener(), this);
    }
}

我已经尝试使用 .jar 文件导入库,但也没有成功。

任何帮助都将不胜感激。


<details>
<summary>英文:</summary>
I&#39;m trying to use **MongoDB** in a **Spigot 1.16.1** Plugin and I&#39;m having Problems with my **maven** imports.
In my IDE (EclipseIDE) there are no Errors. If I export the Plugin and reload the Server the following error appears:
[![Error on Server reload][1]][1]
[1]: https://i.stack.imgur.com/CJGEQ.png
*pom.xml*
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;LeeCraft-Core_hj.2_v.2.0&lt;/groupId&gt;
&lt;artifactId&gt;LeeCraft-Core_hj.2_v.2.0&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;build&gt;
&lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.8.1&lt;/version&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mongodb&lt;/groupId&gt;
&lt;artifactId&gt;mongodb-driver-sync&lt;/artifactId&gt;
&lt;version&gt;4.1.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/project&gt;
*Main Class*
package de.philipp.main;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import de.philipp.api.OnlineTimeAPI;
import de.philipp.data.Data;
import de.philipp.listener.JoinListener;
import de.philipp.mongodb.MongoDBHandler;
import de.philipp.mysql.MySQL;
public class Main extends JavaPlugin {
@Override
public void onDisable() {
}
@Override
public void onEnable() {
// MongoDB Connect
MongoDBHandler.connect();
// Start Plugin Message
Bukkit.getConsoleSender().sendMessage(&quot;&#167;b&#167;l  _            &#167;r&#167;3 ___           __ _        _        ___               _    _      ___       _ _          &quot;);
Bukkit.getConsoleSender().sendMessage(&quot;&#167;b&#167;l | |   ___ ___ &#167;r&#167;3/ __|_ _ __ _ / _| |_   __| |___   / __|___ _ _ ___  (_)__| |_   / _ \\ _ _ | (_)_ _  ___ &quot;);
Bukkit.getConsoleSender().sendMessage(&quot;&#167;b&#167;l | |__/ -_) -_)&#167;r&#167;3 (__| &#39;_/ _` |  _|  _|_/ _` / -_) | (__/ _ \\ &#39;_/ -_) | (_-&lt;  _| | (_) | &#39; \\| | | &#39; \\/ -_)&quot;);
Bukkit.getConsoleSender().sendMessage(&quot;&#167;b&#167;l |____\\___\\__&#167;r&#167;3_|\\___|_| \\__,_|_|  \\__(_)__,_\\___|  \\___\\___/_| \\___| |_/__/\\__|  \\___/|_||_|_|_|_||_\\___|&quot;);
Bukkit.getConsoleSender().sendMessage(&quot; &quot;);
// Listener Register
getServer().getPluginManager().registerEvents(new JoinListener(), this);
}
}
I already tried to import the libary with a .jar file but that didn&#39;t worked too. 
Any help is appreciated 
</details>
# 答案1
**得分**: 2
使用[Maven Shade Plugin][1]将依赖项包含在您的jar中。
[1]: http://maven.apache.org/plugins/maven-shade-plugin/usage.html
<details>
<summary>英文:</summary>
Use the [Maven Shade Plugin][1] to include the dependency in your jar.
[1]: http://maven.apache.org/plugins/maven-shade-plugin/usage.html
</details>
# 答案2
**得分**: 0
你应该使用maven-assembly-plugin来创建一个"fat" jar文件,示例如下:
```xml
<plugins>
<plugin>
<!-- 注意:我们不需要指定groupId,因为默认情况下group是
org.apache.maven.plugins……这是默认的。
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- 这用于继承合并 -->
<phase>package</phase> <!-- 绑定到打包阶段 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
[...]

这确保每个具有范围为compile的依赖项都会被添加到一个jar文件中,它应该被命名为(项目名称和版本)-jar-with-dependencies.jar
请注意,您应该将spigot的范围设置为provided,因为它已经在运行时加载。使用mvn package进行编译。

附注:如果您使用maven,不应该通过Idea或其他IDE将jar文件导入类路径,应该使用Maven导入所有依赖项。

英文:

You should use the maven-assembly-plugin for creating a "fat" jarfile, example:

&lt;plugins&gt;
      &lt;plugin&gt;
        &lt;!-- NOTE: We don&#39;t need a groupId specification because the group is
             org.apache.maven.plugins ...which is assumed by default.
         --&gt;
        &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
        &lt;version&gt;3.3.0&lt;/version&gt;
        &lt;configuration&gt;
          &lt;descriptorRefs&gt;
            &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
          &lt;/descriptorRefs&gt;
        &lt;/configuration&gt;
        &lt;executions&gt;
          &lt;execution&gt;
            &lt;id&gt;make-assembly&lt;/id&gt; &lt;!-- this is used for inheritance merges --&gt;
            &lt;phase&gt;package&lt;/phase&gt; &lt;!-- bind to the packaging phase --&gt;
            &lt;goals&gt;
              &lt;goal&gt;single&lt;/goal&gt;
            &lt;/goals&gt;
          &lt;/execution&gt;
        &lt;/executions&gt;
        [...]

This makes sure that every dependency with scope compile gets shaded into a jar file, it should be called (project name and version)-jar-with-dependencies.jar
Note that you should set the scope of spigot to provided, because it is already loaded on runtime. Compile using mvn package.

PS: You should never import jars to the classpath using Idea/some other ide if you are using maven, import all your dependencies using Maven.

huangapple
  • 本文由 发表于 2020年9月20日 04:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63972961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定