Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

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

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

Last synced: 23 days 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)

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.24.1



format


eclipse-formatter.xml





de.pirckheimer-gymnasium
engine-pi
0.28.0



```

`.vscode/settings.json`

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

## 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



```

`mvn deploy`

## javadoc

Multiline code snippets

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

## versioning

```xml





org.codehaus.mojo
versions-maven-plugin
2.17.1


```

`mvn versions:set`

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

`mvn versions:commit`

## 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 .
*/
```