使用 gradle 导出 FireBase(实时数据库)
Export FireBase With gradle (RealTime DataBase)
我想将我的 firebase 项目导出到 jar,当我尝试使用 jar 时,它不起作用。
我的build.gradle:
apply plugin: "java"
buildscript {
ext.bukkit_version = "1.8.3-R0.1-SNAPSHOT"
}
repositories {
mavenCentral()
maven {
url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
}
}
version = "0.1"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
configurations {
shade
compile.extendsFrom shade
}
dependencies {
compile "org.bukkit:bukkit:$bukkit_version"
shade 'com.google.firebase:firebase-admin:6.12.2'
}
jar {
configurations.shade.each { dep ->
from(project.zipTree(dep)) {
exclude 'META-INF', 'META-INF/**'
}
}
}
我用这段代码来检查问题是什么:
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println(1);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("../yourpixel/YourPixel/key.json"))))
.setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
.build();
System.out.println(2);
FirebaseApp.initializeApp(options);
System.out.println(3);
CountDownLatch latch = new CountDownLatch(1);
FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
System.out.println("Error:" + (databaseError == null ? "null" : databaseError.getMessage()));
System.out.println("Reference:" + (databaseReference == null ? "null" : databaseReference.getPath()));
latch.countDown();
});
System.out.println(4);
latch.await();
System.out.println(5);
}
当我在我的项目中 运行 时,它工作并且输出是:
1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
3
4
Error:null
Reference:/TEST
当我将我的项目导出到 jar 并将其用于 运行 相同的 main 时,它不起作用。输出为:
1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
3
4
然后程序卡住了。
这在任何程序中都是非常的坏主意:
while (true);
这称为紧密循环,它将完全占用您 运行 所在线程上的 CPU。由于 setValue
的完成处理程序还需要在该线程上 运行,因此它永远无法执行。
要正确等待数据写入,您需要使用信号量在写入完成时发出信号。例如,使用 CountdownLatch
:
public static void main(String[] args) throws IOException {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("key.json"))))
.setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
CountDownLatch latch = new CountDownLatch(1);
FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
latch.countDown();
});
latch.await();
}
我解决了我的问题。 (我没说我用的是intellij)
问题是使用 jar 的项目有另一个 jar。
我将 FireBase.jar(我制作的罐子)拖到 craftbukkit 上方(如图所示)并修复了它。
我想将我的 firebase 项目导出到 jar,当我尝试使用 jar 时,它不起作用。
我的build.gradle:
apply plugin: "java"
buildscript {
ext.bukkit_version = "1.8.3-R0.1-SNAPSHOT"
}
repositories {
mavenCentral()
maven {
url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
}
}
version = "0.1"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
configurations {
shade
compile.extendsFrom shade
}
dependencies {
compile "org.bukkit:bukkit:$bukkit_version"
shade 'com.google.firebase:firebase-admin:6.12.2'
}
jar {
configurations.shade.each { dep ->
from(project.zipTree(dep)) {
exclude 'META-INF', 'META-INF/**'
}
}
}
我用这段代码来检查问题是什么:
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println(1);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("../yourpixel/YourPixel/key.json"))))
.setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
.build();
System.out.println(2);
FirebaseApp.initializeApp(options);
System.out.println(3);
CountDownLatch latch = new CountDownLatch(1);
FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
System.out.println("Error:" + (databaseError == null ? "null" : databaseError.getMessage()));
System.out.println("Reference:" + (databaseReference == null ? "null" : databaseReference.getPath()));
latch.countDown();
});
System.out.println(4);
latch.await();
System.out.println(5);
}
当我在我的项目中 运行 时,它工作并且输出是:
1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
3
4
Error:null
Reference:/TEST
当我将我的项目导出到 jar 并将其用于 运行 相同的 main 时,它不起作用。输出为:
1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2
3
4
然后程序卡住了。
这在任何程序中都是非常的坏主意:
while (true);
这称为紧密循环,它将完全占用您 运行 所在线程上的 CPU。由于 setValue
的完成处理程序还需要在该线程上 运行,因此它永远无法执行。
要正确等待数据写入,您需要使用信号量在写入完成时发出信号。例如,使用 CountdownLatch
:
public static void main(String[] args) throws IOException {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("key.json"))))
.setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
CountDownLatch latch = new CountDownLatch(1);
FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
latch.countDown();
});
latch.await();
}
我解决了我的问题。 (我没说我用的是intellij)
问题是使用 jar 的项目有另一个 jar。
我将 FireBase.jar(我制作的罐子)拖到 craftbukkit 上方(如图所示)并修复了它。