Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Java

Table of Contents

Spring

Lombok

Create private constructor

@NoArgsConstructor(access = AccessLevel.PRIVATE)

Maven

Basic commands

# Display available updates
mvn versions:display-dependency-updates

# Execute only the tests in SomeTestClass
mvn test -Dtest=SomeTestClass

# Stop immediately after the first failing test
mvn test -Dsurefire.skipAfterFailureCount=1

Gradle

Basic commands

# Tasks runnable from root project
gradle tasks

# Build displaying more logging information
gradle -i build

# Executes only the tests in SomeTestClass
gradle test --tests SomeTestClass

# Executes only one specific test method
gradle test --tests SomeTestClass.specificMethod

# Stop immediately after the first failing test
gradle test --fail-fast

Plugins

Since Gradle version 3 plugins can be specified through the plugins DSL:

plugins {
    id 'java'
}

The plugins {} block must be a top level statement in the buildscript and cannot be nested inside another construct.

Set console=rich by default

Both in Gradle and Gradle Wrapper, add the following line to your ./gradle.properties:

org.gradle.console=rich

Print the full stacktrace when logging tests exceptions

Add the following block to your ./build.gradle:

test {
    testLogging.exceptionFormat = 'full'
}

Upgrade all dependencies to their latest version

Use Gradle Use Latest Versions Plugin .

SDKMAN

SDKMAN allows to install multiple versions of Java and conveniently switch between them.

Set up a specific Java version for a project

  1. cd into the base directory of your project

  2. use SDKMAN to switch to the desired Java version

  3. run sdk env init (this will generate a .sdkmanrc file in the current directory, pre-populated with the Java version in use)

From now on when you enter your project base directory you can switch to the chosen Java version by simply running:

$ sdk env

Switch Java version automatically when you cd into a directory

Set the folling in your ~/.sdkman/etc/config:

sdkman_auto_env=true 

Flyway

Flyway is an open-source database migration tool: it is based around just 7 basic commands: migrate, clean, info, validate, undo, baseline and repair.

Migrations files names must comply with Flyway naming pattern.

To check that flyway is configured correctly and to see the list of pending migrations run:

flyway info

To run the pending migrations run:

flyway migrate

Rolling back/undo migrations is availble only for Flyway Pro or Enterprise Edition. To roll back with the Community Edition you need to:

  • locally:

    1. run flyway clean

    2. move the migrations you want to roll back out of flyway.locations

    3. run flyway migrate

    4. move the migrations you rolled back in flyway.locations

  • staging/production:

    1. write migrations which revert the changes made in the migrations you want to roll back

    2. commit, push and deploy the migrations

For furthern info check the Flyway documentation.

NeoVim for Java

The following steps outline a basic setup for working with Java in NeoVim, enabling these key features: syntax highlighting, code navigation, code refactoring, code completion, formatting, debugging, diagnostics (errors, warnings, etc.), hover information, auto imports.

  1. Install SDKMAN with curl -s "https://get.sdkman.io" | bash

  2. Install the latest stable version of Java with sdk install java

  3. Install NeoVim

  4. Install LazyVim

  5. Create ~/.config/nvim/lua/plugins/treesitter.lua and paste in the following:

    return {
      "nvim-treesitter/nvim-treesitter",
      opts = {
        ensure_installed = {
          "java",
        },
      },
    }
  6. Run :Mason, select jdtls and press i to install it, go up to the top and wait until you see it is installed.

  7. Create ~/.config/nvim/lua/plugins/jdtls.lua and paste in the following:

    return {
      {
        "mfussenegger/nvim-jdtls",
        ft = "java",
      }
    }

Groovy

Support to Groovy might be needed for example if you are using the Spock test framework.

  1. Append "groovy", to the ensure_installed object in the treesitter.lua file mentioned above.

  2. To be continued...

Google Java Format

google-java-format is not supported by eclipse.jdt.ls (see redhat-developer/vscode-java#663 & redhat-developer/vscode-java#419) but given that it's an executable you should be able to use it with formatprg, see :help formatprg.

Lombok

To be continued...

IntelliJ IDEA

Install on Linux

Install as a snap package to have your installation always up to date.

Alternatively, install manually with the following steps:

  1. Download IntelliJ IDEA

  2. Unpack the tar.gz file to an empty directory using the following command:

    tar -xzf idea-2020.2.1.tar.gz
  3. Run bin/idea.sh

Find in files regexes

# Find files which contain two strings on any line
(string1)[\s\S]*(string2)|\2[\s\S]*\1

# Find files which contain two strings on the same line
\Qstring1\E.*\Qstring2\E

# Find in files excluding packages and imports statements
^(?!import|package).*wordToSearch

Object to JSON in debugger

Favorite plugins

Settings

Navigate back and forward using mouse buttons clicks

Keymap > Navigate > Back | Forward > right click > Add mouse shortcut

Enable changing the font size with the mouse wheel

Editor > General > Change font size with Ctrl+Mouse Wheel

Prevent IDEA from automatically close opened files

Editor > General > Editor Tabs > Tab limit = 100

Shortcuts

Key Command Notes
Ctrl+Shift+A Find action Enables you to search for commands and settings across all menus and tools (similarly to VsCode "Show all commands" - Ctrl+Shift+P)
Shift Shift Search for a target by name (Search everywhere) Find any item in the project or outside of it by its name: files, actions, classes, symbols, settings, and UI elements. (similar - but more powerful - than VsCode "Go to File..., Quick Open" - Ctrl+P)
Ctrl+Shift+F Find in path Search for a text string within a project
Ctrl+E Open Recent files
Ctrl+Shift+E Open Recent Edited files
Ctrl+F12 Locate a code element Open the structure view popup allowing to jump to the desired element
Ctrl+N Navigate to class
Ctrl+Shift+N Navigate to file by file name
Ctrl+Alt+Shift+N Navigate to symbol in project (class, method, etc)

Useful links