- When writing Akveo Java checkstyle rules, we based on the approach from the Google team. You can download Akveo checkstyle.xml and install this rules for IDEs:
- Initial project structure should be corresponded with Maven standard directory layout
- Project components should be divided by feature. For example:
com.demo.project
└───user
│ │
│ └───component
│ │ │ UserComponent.java
│ │ │ UserMetaDataComponent.java
│ │ │ ...
│ │
│ └───service
│ │ │UserService.java
│ │ │UserPaymentService.java
│ │ │ ...
│ │
│ └───model
│ │User.java
│ ...
- Interface definitions shouldn't be prefixed with
I
. - All public methods should be written at the top of the class, all private method at the bottom
- All ORM Entities should have a postfix entity in the definition. It helps divide dto classes and entities
- Use DTO classes without postfix DTO
- If that possible - use Lombok library for Java boilerplate code generation
- important Don't use Lombok
@EqualsAndHashCode
method for ORM entities.
- important Don't use Lombok
- Use db migration tools when you have to change the db structure. (e.g. flyway or liquibase)
- Use checkstyle plugin with predefined rules.
- Download Akveo checkstyle.xml.
- Add this file to your project.
- Add the Checkstyle Plugin in the section of your pom.xml.
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
</plugin>
</plugins>
</reporting>
- Also need to add a section to your pom.xml with the appropriate configuration. Maven Checkstyle plugin comes with a default Checkstyle version 8.29, but some rules require a newer version.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>10.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
- Then, execute the site phase to generate the report.
mvn site
If there's a need to have stringent checks on the coding style, we can configure the plugin in such a way that the build fails when the code does not follow certain rules.
We do this by adding an execution goal to our plugin definition:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>