简体   繁体   English

使用 gradle(实时数据库)导出 FireBase

[英]Export FireBase With gradle (RealTime DataBase)

I want export my project of firebase to jar, and when I try use the jar, it doesn't work.我想将我的 firebase 项目导出到 jar,当我尝试使用 jar 时,它不起作用。

My build.gradle:我的 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/**'
        }
    }
}

I use this code to check what is the problem:我使用此代码来检查问题所在:

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);
}

When I run this inside my project, it works and the output is:当我在我的项目中运行它时,它可以工作并且 output 是:

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

When I export my project to jar and use it to run the same main, it doesn't work.当我将我的项目导出到 jar 并使用它来运行相同的主程序时,它不起作用。 The output is: output 是:

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

And the program stuck.程序卡住了。

This is a very bad idea in any program:这在任何程序中都是一个非常糟糕的主意:

while (true);

This is called a tight loop, and it will completely tie up the CPU on the thread where you run this.这称为紧密循环,它将完全占用运行它的线程上的 CPU。 And since the completion handler for setValue also needs to run on that thread, it is never able to execute.而且由于setValue的完成处理程序也需要在该线程上运行,因此它永远无法执行。

To properly wait for the data to be written, you'll want to use semaphores to signal when the writing is done.为了正确地等待数据被写入,您需要使用信号量在写入完成时发出信号。 For example, with a CountdownLatch :例如,使用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();
}

I fixed my problam.我解决了我的问题。 (I didn't said that i use intellij) The problem was that the project that use the jar, had anoter jar. (我没有说我使用 intellij)问题是使用 jar 的项目有另一个 jar。 i dragged FireBase.jar (the jar i made) to over craftbukkit (like in the picture) and it fixed it.我将 FireBase.jar(我制造的 jar)拖到craftbukkit(如图所示)并修复它。

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

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