英文:
Maven not running with Spigot
问题
我正在尝试在 **Spigot 1.16.1** 插件中使用 **MongoDB**,但我的 **maven** 导入出现了问题。
在我的 IDE(EclipseIDE)中没有错误。如果我导出插件并重新加载服务器,会出现以下错误:
[![服务器重新加载时出现的错误][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'm trying to use **MongoDB** in a **Spigot 1.16.1** Plugin and I'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*
<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 Connect
MongoDBHandler.connect();
// Start Plugin Message
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(" ");
// Listener Register
getServer().getPluginManager().registerEvents(new JoinListener(), this);
}
}
I already tried to import the libary with a .jar file but that didn'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:
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<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> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
[...]
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论