An open API service indexing awesome lists of open source software.

https://github.com/josef-friedrich/java-boilerplate

Boilerplate code snippets for my java projects
https://github.com/josef-friedrich/java-boilerplate

Last synced: 3 months ago
JSON representation

Boilerplate code snippets for my java projects

Awesome Lists containing this project

README

          

[![Maven Central](https://img.shields.io/maven-central/v/de.pirckheimer-gymnasium/engine-pi.svg?style=flat)](https://central.sonatype.com/artifact/de.pirckheimer-gymnasium/engine-pi)
[![javadoc](https://javadoc.io/badge2/de.pirckheimer-gymnasium/engine-pi/javadoc.svg)](https://javadoc.io/doc/de.pirckheimer-gymnasium/engine-pi)

# java-project-boilerplate

Boilerplate code snippets for my java projects

## Maven Standard Directory Layout

`src/main/java` Application/Library sources

`src/main/resources` Application/Library resources

`src/test/java` Test sources

`src/test/resources` Test resources

[Source](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html)

## pom.xml

```xml

4.0.0

rocks.friedrich
boilerplate
0.1.0


17
17
UTF-8

```


[Source](https://maven.apache.org/guides/introduction/introduction-to-the-pom.html)

## naming conventions on groupId, artifactId, and version

### groupId

uniquely identifies your project across all projects. A group ID should follow
Java's package name rules. This means it starts with a reversed domain name you
control. For example

org.apache.maven, org.apache.commons, de.pirckheimer-gymnasium

## artifactId

is the **name of the jar** without version. If you created it, then you can
choose whatever name you want with **lowercase letters** and no strange symbols.

eg. maven, commons-math

## version

if you distribute it, then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, ...). For example,

2.0, 2.0.1, 1.3.1

[Source](https://maven.apache.org/guides/mini/guide-naming-conventions.html)

## format

`make format` or `mvn formatter:format`

https://github.com/revelc/formatter-maven-plugin

[DefaultCodeFormatterConstants](https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java)

[Configuration examples of Maven formatter plugin](https://code.revelc.net/formatter-maven-plugin/examples.html#custom-configuration-file)

It is best to make or edit the XML formatter files in Eclipse: Window > Preferences > Java > Code Style > Formatter

```xml




net.revelc.code.formatter
formatter-maven-plugin
2.23.0

${project.basedir}/.eclipse-formatter/school.xml


```

```xml




net.revelc.code.formatter
formatter-maven-plugin
2.29.0

eclipse-formatter.xml




format





de.pirckheimer-gymnasium
engine-pi-build-tools
0.1.0



```

`.vscode/settings.json`

```json
{
"java.format.settings.url": ".eclipse-formatter/school.xml",
"editor.tabSize": 4,
"editor.insertSpaces": true
}
```

[Comparsion of the different code formatters](https://jqno.nl/post/2024/08/24/why-are-there-no-decent-code-formatters-for-java/)

Java → Code Style → Formatter → Edit → Off/On Tags

```
@formatter:off
@formatter:on
```

## Maven (JAR files) repository inside a project

```xml
wget -O commons-lang3-sources.jar https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0-sources.jar
wget -O commons-lang3.jar https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0.jar

mvn install:install-file \
-Dfile=commons-lang3.jar \
-Dsources=commons-lang3-sources.jar \
-DgroupId=org.apache.commons \
-DartifactId=commons-lang3 \
-Dversion=3.14.0 \
-Dpackaging=jar \
-DlocalRepositoryPath=repository \
-DcreateChecksum=true
```

`pom.xml`

```xml


project-repository
file://${project.basedir}/repository

```

```xml

org.apache.commons
commons-lang3
3.14.0

```

## Remove unused imports

Not working?

mvn site

```xml



org.apache.maven.plugins
maven-pmd-plugin
3.21.0

```

mvn com.xenoamess:remove-unused-imports-maven-plugin:0.0.6:process

```xml



com.xenoamess
remove-unused-imports-maven-plugin
0.0.6



process



UnusedImports
DuplicateImports
UnnecessaryReturn
ImportFromSamePackage
DontImportJavaLang

${basedir}/target/pmd.xml
false




```

## Reference JAR lib in VScode

```json
{
"java.project.referencedLibraries": ["+libs/**/*.jar"]
}
```

## Publish to Maven Central

https://central.sonatype.org/publish-ea/publish-ea-guide/#introduction

https://github.com/simpligility/ossrh-demo

`~/.m2/settings.xml`

```xml




ossrh
xxx
xxx




ossrh

true


gpg2



```

`pom.xml`

```xml





org.apache.maven.plugins
maven-gpg-plugin
3.2.4


sign-artifacts
verify

sign




--pinentry-mode
loopback







org.sonatype.central
central-publishing-maven-plugin
0.5.0
true


central
true
true

true



```

`mvn deploy`

## javadoc

Multiline code snippets

https://reflectoring.io/howto-format-code-snippets-in-javadoc/

## versioning

https://www.mojohaus.org/versions/versions-maven-plugin/

```xml





org.codehaus.mojo
versions-maven-plugin
2.19.1


```

### set

[versions:set](https://www.mojohaus.org/versions/versions-maven-plugin/set-mojo.html) can be used to set the project version from the command line.

Interactively:

`mvn versions:set`

`mvn versions:set -DnewVersion=1.0.1-SNAPSHOT`

`mvn versions:set -f modules/engine/pom.xml`

### commit

`mvn versions:commit`

### display-dependency-updates

[versions:display-dependency-updates](https://www.mojohaus.org/versions/versions-maven-plugin/display-dependency-updates-mojo.html) scans a project's dependencies and produces a report of those dependencies which have newer versions available.

### display-plugin-updates

[versions:display-plugin-updates](https://www.mojohaus.org/versions/versions-maven-plugin/display-plugin-updates-mojo.html) scans a project's plugins and produces a report of those plugins which have newer versions available, taking care of Maven version prerequisites.

## junit

from the [junit5-jupiter-starter-maven project](https://github.com/junit-team/junit5-samples/blob/main/junit5-jupiter-starter-maven/pom.xml):

`pom.xml`

```xml



org.junit
junit-bom
5.11.1
pom
import


org.junit.jupiter
junit-jupiter
test



maven-surefire-plugin
3.5.0

```

## Copyright

```
/*
* Copyright (c) 2024 Josef Friedrich.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
```

## maven multi module

https://maven.apache.org/guides/mini/guide-multiple-modules.html

https://maven.apache.org/plugins/maven-help-plugin/effective-pom-mojo.html

```
mvn help:effective-pom
```

or

```
mvn help:effective-pom -Dverbose
```

## Maven Plugin to sort pom.xml

```xml

com.github.ekryd.sortpom
sortpom-maven-plugin
4.0.0



false
true

4




sort




```