Skip to content

Commit

Permalink
feat: add spotless formatter
Browse files Browse the repository at this point in the history
automatically remove unused imports

Fixes jhipster#22734
  • Loading branch information
spotlesscoder committed Jul 2, 2023
1 parent 1626a09 commit 5420098
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions generators/app/__snapshots__/generator.spec.mts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ exports[`generator - app with default config should match snapshot 1`] = `
"picocli": "'PICOCLI-VERSION'",
"properties-maven-plugin": "'PROPERTIES-MAVEN-PLUGIN-VERSION'",
"sonar-maven-plugin": "'SONAR-MAVEN-PLUGIN-VERSION'",
"spotless-gradle-plugin": "'SPOTLESS-GRADLE-PLUGIN-VERSION'",
"spotless-maven-plugin": "'SPOTLESS-MAVEN-PLUGIN-VERSION'",
"spring-boot": "'SPRING-BOOT-VERSION'",
"spring-pulsar": "'SPRING-PULSAR-VERSION'",
"typesafe": "'TYPESAFE-VERSION'",
Expand Down Expand Up @@ -908,6 +910,8 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"picocli": "'PICOCLI-VERSION'",
"properties-maven-plugin": "'PROPERTIES-MAVEN-PLUGIN-VERSION'",
"sonar-maven-plugin": "'SONAR-MAVEN-PLUGIN-VERSION'",
"spotless-gradle-plugin": "'SPOTLESS-GRADLE-PLUGIN-VERSION'",
"spotless-maven-plugin": "'SPOTLESS-MAVEN-PLUGIN-VERSION'",
"spring-boot": "'SPRING-BOOT-VERSION'",
"spring-pulsar": "'SPRING-PULSAR-VERSION'",
"typesafe": "'TYPESAFE-VERSION'",
Expand Down Expand Up @@ -1445,6 +1449,8 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"picocli": "'PICOCLI-VERSION'",
"properties-maven-plugin": "'PROPERTIES-MAVEN-PLUGIN-VERSION'",
"sonar-maven-plugin": "'SONAR-MAVEN-PLUGIN-VERSION'",
"spotless-gradle-plugin": "'SPOTLESS-GRADLE-PLUGIN-VERSION'",
"spotless-maven-plugin": "'SPOTLESS-MAVEN-PLUGIN-VERSION'",
"spring-boot": "'SPRING-BOOT-VERSION'",
"spring-pulsar": "'SPRING-PULSAR-VERSION'",
"typesafe": "'TYPESAFE-VERSION'",
Expand Down
2 changes: 2 additions & 0 deletions generators/server/resources/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ gradle-liquibase = { id = 'org.liquibase.gradle', version = '2.2.0' }

gradle-sonarqube = { id = 'org.sonarqube', version = '4.2.1.3168' }

spotless-gradle-plugin = { id = 'com.diffplug.spotless', version = '6.19.0' }

gradle-modernizer-plugin = { id = 'com.github.andygoossens.gradle-modernizer-plugin', version = '1.8.0' }

gradle-enterprise = { id = 'com.gradle.enterprise', version = '3.13.4' }
Expand Down
6 changes: 6 additions & 0 deletions generators/server/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<openapi-generator-maven-plugin.version>6.6.0</openapi-generator-maven-plugin.version>
<properties-maven-plugin.version>1.1.0</properties-maven-plugin.version>
<sonar-maven-plugin.version>3.9.1.2184</sonar-maven-plugin.version>
<spotless-maven-plugin.version>2.37.0</spotless-maven-plugin.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -283,6 +284,11 @@
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
8 changes: 8 additions & 0 deletions generators/server/templates/build.gradle.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ plugins {
id "com.github.node-gradle.node"
<%_ } _%>
id "org.sonarqube"
id "com.diffplug.spotless" version "<%- javaDependencies['spotless-gradle-plugin'] %>"
id "io.spring.nohttp"
id "com.github.andygoossens.gradle-modernizer-plugin"
<%_ if (gatlingTests) { _%>
Expand Down Expand Up @@ -61,6 +62,13 @@ ext {

apply from: "gradle/docker.gradle"
apply from: "gradle/sonar.gradle"

spotless {
java {
removeUnusedImports()
}
}

<%_ if (enableSwaggerCodegen) { _%>
apply from: "gradle/swagger.gradle"
<%_ } _%>
Expand Down
1 change: 1 addition & 0 deletions generators/server/templates/gradle.properties.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ gitPropertiesPluginVersion=<%- javaDependencies['gradle-git-properties'] %>
gradleNodePluginVersion=<%- javaDependencies['node-gradle'] %>
<%_ } _%>
sonarqubePluginVersion=<%- javaDependencies['gradle-sonarqube'] %>
spotlessPluginVersion=<%- javaDependencies['spotless-gradle-plugin'] %>
<%_ if (enableSwaggerCodegen) { _%>
openapiPluginVersion=<%- javaDependencies['openapi-generator-maven-plugin'] %>
<%_ } _%>
Expand Down
6 changes: 6 additions & 0 deletions generators/server/templates/npmw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ basedir=`dirname "$0"`
if [ -f "$basedir/mvnw" ]; then
bindir="$basedir/target/node"
repodir="$basedir/target/node/node_modules"
spotlessApplyCommand="$basedir/mvnw process-sources"
installCommand="$basedir/mvnw -Pwebapp frontend:install-node-and-npm@install-node-and-npm"

PATH="$basedir/$builddir/:$PATH"
Expand All @@ -13,6 +14,7 @@ if [ -f "$basedir/mvnw" ]; then
elif [ -f "$basedir/gradlew" ]; then
bindir="$basedir/build/node/bin"
repodir="$basedir/build/node/lib/node_modules"
spotlessApplyCommand="$basedir/gradlew spotlessApply"
installCommand="$basedir/gradlew npmSetup"
else
echo "Using npm installed globally"
Expand All @@ -22,6 +24,10 @@ fi
NPM_EXE="$repodir/npm/bin/npm-cli.js"
NODE_EXE="$bindir/node"

if [ ! -z "$spotlessApplyCommand" ]; then
$spotlessApplyCommand || true
fi

if [ ! -x "$NPM_EXE" ] || [ ! -x "$NODE_EXE" ]; then
$installCommand || true
fi
Expand Down
6 changes: 6 additions & 0 deletions generators/server/templates/npmw.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ if exist "%NPMW_DIR%mvnw.cmd" (
set NODE_EXE=^"^"
set NODE_PATH=%NPMW_DIR%target\node\
set NPM_EXE=^"%NPMW_DIR%target\node\npm.cmd^"
set SPOTLESS_APPLY_COMMAND=^"%NPMW_DIR%mvnw.cmd^" process-sources
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%mvnw.cmd^" -Pwebapp frontend:install-node-and-npm@install-node-and-npm
) else (
set NODE_EXE=^"%NPMW_DIR%build\node\bin\node.exe^"
set NODE_PATH=%NPMW_DIR%build\node\bin\
set NPM_EXE=^"%NPMW_DIR%build\node\lib\node_modules\npm\bin\npm-cli.js^"
set SPOTLESS_APPLY_COMMAND=^"%NPMW_DIR%gradlew.bat^" spotlessApply
set INSTALL_NPM_COMMAND=^"%NPMW_DIR%gradlew.bat^" npmSetup
)

if exist %SPOTLESS_APPLY_COMMAND% (
call %SPOTLESS_APPLY_COMMAND%
)

if not exist %NPM_EXE% (
call %INSTALL_NPM_COMMAND%
)
Expand Down
24 changes: 24 additions & 0 deletions generators/server/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<%_ } _%>
<properties-maven-plugin.version><%- javaDependencies['properties-maven-plugin'] %></properties-maven-plugin.version>
<sonar-maven-plugin.version><%- javaDependencies['sonar-maven-plugin'] %></sonar-maven-plugin.version>
<spotless-maven-plugin.version><%- javaDependencies['spotless-maven-plugin'] %></spotless-maven-plugin.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -406,6 +407,10 @@
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down Expand Up @@ -845,6 +850,25 @@
<artifactId>sonar-maven-plugin</artifactId>
<version>${sonar-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>${spotless-maven-plugin.version}</version>
<configuration>
<java>
<removeUnusedImports />
</java>
</configuration>
<executions>
<execution>
<id>spotless</id>
<phase>process-sources</phase>
<goals>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down

0 comments on commit 5420098

Please sign in to comment.