gradle_new_java_library

This function will create a new Gradle Java library project with sensible, modern defaults and the given name.

The optional second parameter is the directory ($PWD if not given) the project is created in.

A Git repository will also be initialized for the project if git is installed.

A Git pre-commit hook will be added also.

This script uses Git commit signing; you need to:

Alternatively, you can remove --gpg-sign:

zfunc/gradle_new_java_library
  git commit \
    --quiet \
-   --gpg-sign \
    --signoff \

The generated default package will be org.example.

You can change the default by adding org.gradle.buildinit.source.package in Gradle properties:

printf 'org.gradle.buildinit.source.package=my.org' >> "${GRADLE_USER_HOME:=${HOME}}/gradle.properties"

If you want no comments to be generated add org.gradle.buildinit.comments=false in Gradle properties:

printf 'org.gradle.buildinit.comments=false' >> "${GRADLE_USER_HOME:=${HOME}}/gradle.properties"

You might want to customize the defaults for the created gradle.properties, .gitignore, .gitattributes, or .editorconfig, e.g.:

zfunc/gradle_new_java_library
      cat << 'EOF' >gradle.properties
  ...
  EOF

      cat << 'EOF' >.gitignore
  ...
  EOF

      cat << 'EOF' >.gitattributes
  ...
  ...
  EOF

      cat << 'EOF' >.editorconfig
  ...
  EOF

Usage

$ gradle_new_java_library example-java-library
$ gradle_new_java_library other-java-library /tmp
$ tree --noreport -a -I .git .
.
├── .editorconfig
├── .git-blame-ignore-revs
├── .gitattributes
├── .githooks
│   └── pre-commit
├── .gitignore
├── gradle
│   ├── libs.versions.toml
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradle.properties
├── gradlew
├── gradlew.bat
├── lib
│   ├── build.gradle.kts
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── org
│       │   │       └── example
│       │   │           └── Library.java
│       │   └── resources
│       └── test
│           ├── java
│           │   └── org
│           │       └── example
│           │           └── LibraryTest.java
│           └── resources
└── settings.gradle.kts
$ git status
On branch main
nothing to commit, working tree clean

Prerequisites