开发速记-emacs篇: java+gradle开发模式

安装nix并配置channel:

sudo install -d -m755 -o $(id -u) -g $(id -g) /nix
curl https://nixos.org/nix/install | sh

nix-channel --add https://nixos.org/channels/nixos-20.03 nixos-20.03
nix-channel --update nixos-20.03

安装gradle及lombok:

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA gradle
nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA lombok

安装emacs:

nix-env -f ~/.nix-defexpr/channels/nixos-20.03 -iA emacs

配置emacs:

创建并拷贝github文件内容至~/.emacs.d/init.el
https://github.com/clojurians-org/my-emacs/blob/master/init.el

代码片段解释:

  1. 安装lsp-java, gradle-mode
(dolist (package '(paredit restclient json-mode typescript-mode                                                                     
                   dap-mode lsp-java gradle-mode                                                                                    
                           dante cider                                                                                              
                   ))                                                                                                               
  (unless (package-installed-p package)                                                                                             
    (package-install package)))                                                                                                     
  1. 忽略文件变更
(setq lsp-file-watch-ignored
  '(".idea" ".ensime_cache" ".eunit" "node_modules"
            ".git" ".hg" ".fslckout" "_FOSSIL_"
            ".bzr" "_darcs" ".tox" ".svn" ".stack-work"
            "build"))
  1. 开启java编程及调试工具
(require 'dap-java)
(require 'lsp-java)
  1. 开启lombok支持
(setq path-to-lombok (car (file-expand-wildcards "/nix/store/*-lombok-*/share/java/lombok.jar") ) )
(setq lsp-java-vmargs
      `("-noverify"
        "-Xmx1G"
        "-XX:+UseG1GC"
        "-XX:+UseStringDeduplication"
        ,(concat "-javaagent:" path-to-lombok)
        ,(concat "-Xbootclasspath/a:" path-to-lombok)))
  1. 绑定java开发模式至lsp-mode + gradle-mode
(add-hook 'java-mode-hook #'lsp)
(add-hook 'java-mode-hook (lambda ()
                            (setq c-default-style "java")
                            (setq c-basic-offset 4)
                            (display-line-numbers-mode t)
                            (gradle-mode 1)
                            ))

创建项目:

larrys-MacBook-Pro:my-test larluo$ mkdir my-test
larrys-MacBook-Pro:my-test larluo$ cd my-test
larrys-MacBook-Pro:my-test larluo$ gradle init                                                                                      
                                                                                                                                    
Select type of project to generate:                                                                                                 
  1: basic                                                                                                                          
  2: application                                                                                                                    
  3: library                                                                                                                        
  4: Gradle plugin                                                                                                                  
Enter selection (default: basic) [1..4] 3                                                                                           
                                                                                                                                    
Select implementation language:                                                                                                     
  1: C++
  2: Groovy
  3: Java
  4: Kotlin
  5: Scala
  6: Swift
Enter selection (default: Java) [1..6]

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2]

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit 4) [1..4]

Project name (default: my-test):
Source package (default: my.test):

> Task :init
Get more help with your project: https://docs.gradle.org/5.6.1/userguide/java_library_plugin.html

BUILD SUCCESSFUL in 5m 14s
2 actionable tasks: 2 executed

添加application运行支持

修改build.gradle文件内容

  1. 添加application插件并设置MainClass
plugins {                                                                                                                           
    // Apply the java-library plugin to add support for Java Library                                                                
    id 'java-library'                                                                                                               
    id "application"                                                                                                                
}
application {
    mainClassName = "my.test.Library"
}                                                                                                                                   
  1. 设置国内源
repositories {                                                                                                                      
    maven {                                                                                                                         
        url "https://mirrors.tuna.tsinghua.edu.cn/clojars/"                                                                         
    }                                                                                                                               
    maven {                                                                                                                         
        url "http://maven.aliyun.com/nexus/content/groups/public"
    }
    mavenLocal()
}

  1. 添加依赖
dependencies {
    api 'commons-codec:commons-codec:1.14'
    testImplementation 'junit:junit:4.12'
}

使用emacs编码

  1. 打开文件
emacs -nw src/main/java/my/test/Library.java
  1. 导入项目
i==>Import project root /Users/larluo/my-tmp/my-test/src/main/java/my/test/.
I==>Import project by selecting root directory interactively.
d==>Do not ask again for the current project by adding /Users/larluo/my-tmp/my-test/src/main/java/my/test/ to lsp-session-folders-b\
lacklist.
D==>Do not ask again for the current project by selecting ignore path interactively.
n==>Do nothing: ask again when opening other files from the current project.

选择I, 输入项目目录,后续可变改

  1. 打开错误提示面板,并编写代码
    [C-l, !, l] 打开错误面板


    image.png
  2. 简单编写main方法


    image.png
  3. 使用M-., M-, 进行代码跳转


    image.png

使用gradle-mode编译运行

https://github.com/jacobono/emacs-gradle-mode

输入C-c C-g r, 接着输入run运行Application。


image.png

项目实战

  1. 打开linked datahub效果:
    https://github.com/linkedin/datahub
emacs -nw metadata-jobs/mae-consumer-job/src/main/java/com/linkedin/metadata/kafka/MaeStreamTask.java
image.png

你可能感兴趣的:(开发速记-emacs篇: java+gradle开发模式)