I'm trying to introduce a tool to standardise the format of our java code. The requirements are:
I've tried using Spotless, but the way it formats multiline statements is very unsatisfactory. For example, the following code
var token = Jwts.builder()
.signWith(Keys.hmacShaKeyFor(signingKey), SignatureAlgorithm.HS512)
.setHeaderParam("typ", SecurityConstants.TOKEN_TYPE)
.setIssuer(SecurityConstants.TOKEN_ISSUER)
.claim("rol", roles)
.setAudience(SecurityConstants.TOKEN_AUDIENCE)
.setSubject(user.getUsername())
.setExpiration(Timestamp.valueOf(tomorrow))
.addClaims(user.getClaims())
.compact();
Will fail the Spotless checks and if ./gradlew spotlessApply is run, it will be reformatted to this hideous mess
var token = Jwts.builder().signWith(Keys.hmacShaKeyFor(signingKey), SignatureAlgorithm.HS512)
.setHeaderParam("typ", SecurityConstants.TOKEN_TYPE).setIssuer(SecurityConstants.TOKEN_ISSUER)
.claim("rol", roles).setAudience(SecurityConstants.TOKEN_AUDIENCE).setSubject(user.getUsername())
.setExpiration(Timestamp.valueOf(tomorrow)).addClaims(user.getClaims()).compact();
So effectively, Spotless is behaving almost like an obfuscator. Is there a tool available that can satisfy most/all of the above requirements?
I've tried using Spotless, but the way it formats multiline statements is very unsatisfactory.
Spotless allows you specify custom rules to fulfill your requirements: https://github.com/diffplug/spotless/tree/main/plugin-gradle#custom
Checkstyle is another a great tool to enforce formatting. It is used by the Spring Framework team. It also has an excellent plugin for IntelliJ.
So suggestion is either:
If code is submitted that does not conform to the rules, the build should fail.
There are a couple ways you can handle this:
Whichever format plugin you use, there is a associated check task. So for Spotless that would be spotlessCheck and Checkstyle is just check.
You can configure the build task to have a task dependency on those tasks.
Another way is to have a check in your pipeline that invokes which check task you configure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With