A template Kotlin (Java) project configured with Gradle and Spotless plugin for Klean Kotlin Kode.
The template comes with a ktlint, logging, gitignore, gradle and junit preconfigured. See here for more information.
The template was designed and tested in Intellij Idea, but should be useable in other IDEs.
- In the parent directory you want your in project run:
git clone https://github.com/iikirilov/spotless-kotlin <you_project_name>
cd <you_project_name>
rm -rf .git
git init
- Add your
rootProject.name
name tosettings.gradle
- Edit
gradle/spotless.kotlin.license
to inclide your company name - Edit
README.md
git add -A
git remote add origin <your_remote_repo_url>
git commit -m "Initial commit"
- Open your newly created project in you IDE of choice.
- Create your modules. [Optional]
- Set-up logging. [TODO]
- Write some kotlin.
- Run
./gradlew spotlessApply
to automatically klean up you kode. - Commit, push and repeat from 5.
Modular code is awesome!! It makes it easier to build, maintain and re-use code. The root directory is already a module. You do not need to create modules if you feel your project does not need them, instead you can put your src
folder directly in the project root.
└── <module_name>
└── src
├── main
│ ├── kotlin
│ └── resources
└── test
├── kotlin
└── resources
Add a new line to settings.gradle
in the format include '<module_name>'
. Then create the folder structure in the project root as decribed in module structure.
include '<parent_module>:<child_module>'
include `<parent_module>:<child_module_1>`
Then create the parnet_module
folder in the project root and then the folder structure described in module structure in the parnet_module
folder.
The following is an example settings.gradle
file. The file includes the root project name.
rootProject.name = 'spotless-kotlin'
include 'module1'
include 'module2:module2a'
include 'module2:module2b'
Logging is set up using kotlin-logging and logback.
Declare a logger wherever you need it like this: val logger = KotlinLogging.logger {}
Logback configuration should be located in src/main/resources/logback.xml
.
Logback configuration for tests should be located in src/test/resources/logback-test.xml
.
The following is a minimal configuration of a logback config file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
- Created a new Gradle project in Intellij checking the Kotlin (Java) checkbox in the "Additional Libraries and Frameworks" section
- Set up git - in the project directory
git init
git remote add origin https://github.com/iikirilov/spotless-kotlin.git
- Modify
build.gradle
- Removed the
version
label - Adding spotless gradle plugin
- Configured Spotless Defaults
- Added kotlin-logging and logback for logging
- Removed the
- Added Apache License v2 in
gradle/spotless.kotlin.license
Remember to change<insert-company-name-here>
to your company name. - Set up modules in
settings.gradle
- Generate
.gitignore
for Kotlin, JetBrains and Gradle using Intellij plugin
Uses ktlint behind the scenes. By default:
- Max Line Length = 100
- Inserts new line at the end of every
.kt
file - Puts an Apache License v2 at the top of every
.kt
file. You can change the license by modifyinggradle/spotless.kotlin.license
Uses groovy eclipse with the config file specified in gradle/formatter.properties
. By default:
- Uses 4
space
as the indentation - Indentations are not inserted in empty lines
- Inserts new line at the end of every
.gradle
file