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 with pre-commit and pre-push hooks will also be initialized for the project if git is installed.

This script uses Git commit signing if user.signingKey is set in the global Git configuration.

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 .editorconfig, .gitattributes, .gitignore, gradle.properties, pre-commit or pre-push, e.g.:

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

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

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

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

    cat <<'EOF' >.githooks/pre-commit
...
EOF

    cat <<'EOF' >.githooks/pre-push
...
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
│   └── pre-push
├── .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