使用Gradle导出Firebase(实时数据库)。

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

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

然后程序就会卡住。

英文:

I want export my project of firebase to jar, and when I try use the jar, it doesn't work.

My 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:

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. The output is:

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.

答案1

得分: 1

这在任何程序中都是一个非常糟糕的想法:

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();
}
英文:

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. And since the completion handler for setValue also needs to run on that thread, it is never able to execute.

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:

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

答案2

得分: 0

我解决了我的问题。(我没有说过我使用IntelliJ)
问题是使用该jar的项目有另一个jar。
我将FireBase.jar(我制作的jar)拖动到craftbukkit上方(如图片中所示),问题得以解决。

使用Gradle导出Firebase(实时数据库)。

英文:

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.
i dragged FireBase.jar (the jar i made) to over craftbukkit (like in the picture) and it fixed it.
使用Gradle导出Firebase(实时数据库)。

huangapple
  • 本文由 发表于 2020年4月9日 19:32:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61120189.html
匿名

发表评论

匿名网友

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

确定