Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用Gradle创建并完成一个JAVA程序 #71

Open
soapgu opened this issue Aug 14, 2021 · 2 comments
Open

使用Gradle创建并完成一个JAVA程序 #71

soapgu opened this issue Aug 14, 2021 · 2 comments
Labels
IDE Good for newcomers JAVA This doesn't seem right

Comments

@soapgu
Copy link
Owner

soapgu commented Aug 14, 2021

  • 前言

一直想要做这件事情了,只是一直没动力去做。
写了这么长时间的Android程序,对JAVA的语法也是非常熟悉了。却连一个“纯”JAVA程序都不会写,确实有点奇怪。正好我对RxJava里面的一个API的用法有些想法想要试验下,但是如果用Android Studio开一个安卓程序验证太麻烦了,我只要测试时RxJava这个类库而已。我还要写一点无关的UI和交互逻辑,还要开模拟器或者真机。实在是太啰嗦了。

  • 环境准备

  • 1. JDK

Gradle的要求是JDK version >8 。
这个Android Studio到是已经帮我安装好了,我需要配置好JAVA_HOME和JAVA运行bin目录的PATH变量。
我前面的博客已经说清楚,这里不再重复了

由于前面已经用过gradle wrapper了,所以gradle其实已经在PC里装好了。我需要把他揪出来,配置到环境变量PATH里
图片
图片
配我电脑里面最新的7.0.2吧,官方gradle最新版本是7.1.1

  • 利用Gradle创建JAVA项目

  1. 新建一个目录作为JAVA项目的根目录
  2. 打开控制台,输入gradle init
    创建过程非常的简单,就是傻瓜向导。没有不会的理由
    相关教程可以参考下面链接,很详尽。
  • Building Java Applications Sample

  • 为JAVA项目选一个合适的IDE

    Gradle解决了JAVA程序的组织问题,但是没有一个趁手的IDE是不行的。我总不能用Notepad++来写吧,没有一个自动提示这手感是百般不是,总不能回到原始社会吧。
    

    不想再新装啥IDE了,还是先试试VS Code吧。
    Code自动提示安装好了插件
    图片

回头改写下build.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/7.0.2/userguide/building_java_projects.html
 */

plugins {
    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13.1'

    // This dependency is used by the application.
    //implementation 'com.google.guava:guava:30.0-jre'
    implementation 'io.reactivex.rxjava3:rxjava:3.0.4'
}

application {
    // Define the main class for the application.
    mainClass = 'javademo.App'
}

把rxjava的库写到dependencies 里面,VSCode会自动同步的
接下来编辑java文件,自动提示写起来没啥不违和的感觉。
就是吐槽下,我需要自己Ctrl + S 保存文件。

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package javademo;

import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;

public class App {
    public String getGreeting() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        System.out.println(new App().getGreeting());
        Single<String> single = Single.just("hellow");
        single.subscribe( s -> System.out.println( s ) ); 

        Maybe<Integer> source = Maybe.empty();
        Maybe<Integer> result = source.flatMapSingle(x -> {
            return Single.just(Math.abs(x));
        });

        result.subscribe(System.out::println, 
                        e -> System.out.println( e.toString()), 
                        () -> System.out.println("---Complete--"));
    }
}

接下来是编译和运行了

BUILD SUCCESSFUL in 9s
7 actionable tasks: 5 executed, 2 up-to-date
PS D:\WorkSpace\PlayPen\javademo> gradle run  

> Task :app:run
Hello World!
hellow
---Complete--

BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date

Debug
图片
基本满足需求
Unit Test
图片
Unit Test的体验也不错

  • 总结

一次还不错的初体验。
一开始是baidu搜处理的博客来参考实践的,结果磕磕巴巴还碰到点傻问题。最后通过Gradle的官网教程扳回正轨了。总结下来参考资料还是越正规越好。不过Gradle的官档洋洋洒洒一大堆,一开始读对新手很不友好才会想要去找其他资料的

@soapgu soapgu added IDE Good for newcomers JAVA This doesn't seem right labels Aug 14, 2021
@nonocast
Copy link

嗯,用gradle。nonocast/me#43

@soapgu
Copy link
Owner Author

soapgu commented Aug 19, 2021

嗯,用gradle。nonocast/me#43

JAVA程序好像主流用Maven的多。gradle功能好强大,现在只是入门水平~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IDE Good for newcomers JAVA This doesn't seem right
Projects
None yet
Development

No branches or pull requests

2 participants