Skip to content

Commit

Permalink
add code quality gradle convention plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
atomfrede committed Dec 28, 2023
1 parent 71abf51 commit f4c73c1
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 273 deletions.
6 changes: 6 additions & 0 deletions generators/server/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,10 @@ export default function cleanupOldServerFilesTask(this: BaseGenerator, taskParam
this.removeFile(`${application.javaPackageSrcDir}security/oauth2/OAuthIdpTokenResponseDTO.java`);
}
}

if (this.isJhipsterVersionLessThan('8.1.0')) {
if (application.buildToolGradle) {
this.removeFile('gradle/sonar.gradle');
}
}
}
10 changes: 8 additions & 2 deletions generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
* limitations under the License.
*/
import cleanupOldServerFiles from './cleanup.js';
import { SERVER_MAIN_SRC_DIR, SERVER_MAIN_RES_DIR, SERVER_TEST_SRC_DIR, SERVER_TEST_RES_DIR } from '../generator-constants.js';
import {
SERVER_MAIN_SRC_DIR,
SERVER_MAIN_RES_DIR,
SERVER_TEST_SRC_DIR,
SERVER_TEST_RES_DIR,
GRADLE_BUILD_SRC_MAIN_DIR,
} from '../generator-constants.js';
import { addSectionsCondition, mergeSections } from '../base/support/index.js';
import { moveToJavaPackageSrcDir, moveToJavaPackageTestDir, moveToSrcMainResourcesDir } from './support/index.js';

Expand Down Expand Up @@ -305,12 +311,12 @@ export const baseServerFiles = {
'build.gradle',
'settings.gradle',
'gradle.properties',
'gradle/sonar.gradle',
'gradle/docker.gradle',
'gradle/profile_dev.gradle',
'gradle/profile_prod.gradle',
'gradle/war.gradle',
'gradle/zipkin.gradle',
`${GRADLE_BUILD_SRC_MAIN_DIR}/jhipster.code-quality-conventions.gradle`,
],
},
{
Expand Down
68 changes: 68 additions & 0 deletions generators/server/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,74 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator {
});
}
},
customizeGradle({ application, source }) {
if (!application.buildToolGradle) return;
source.addGradleDependencyCatalogVersion?.({ name: 'checkstyle', version: application.javaDependencies.checkstyle });
source.addGradleBuildSrcDependencyCatalogVersion?.({ name: 'checkstyle', version: application.javaDependencies.checkstyle });
source.addGradleDependencyCatalogVersion?.({ name: 'jacoco', version: application.javaDependencies?.['jacoco-maven-plugin'] });
source.addGradleBuildSrcDependencyCatalogVersion?.({
name: 'jacoco',
version: application.javaDependencies?.['jacoco-maven-plugin'],
});
source.addGradleDependencyCatalogVersion?.({
name: 'sonarqube-plugin',
version: application.javaDependencies?.['gradle-sonarqube'],
});
source.addGradleBuildSrcDependencyCatalogVersion?.({
name: 'sonarqube-plugin',
version: application.javaDependencies?.['gradle-sonarqube'],
});
source.addGradleDependencyCatalogVersion?.({
name: 'spotless-plugin',
version: application.javaDependencies?.['spotless-gradle-plugin'],
});
source.addGradleBuildSrcDependencyCatalogVersion?.({
name: 'spotless-plugin',
version: application.javaDependencies?.['spotless-gradle-plugin'],
});
source.addGradleDependencyCatalogVersion?.({
name: 'modernizer-plugin',
version: application.javaDependencies?.['gradle-modernizer-plugin'],
});
source.addGradleBuildSrcDependencyCatalogVersion?.({
name: 'modernizer-plugin',
version: application.javaDependencies?.['gradle-modernizer-plugin'],
});
source.addGradleDependencyCatalogVersion?.({ name: 'nohttp-plugin', version: application.javaDependencies?.['nohttp-checkstyle'] });
source.addGradleBuildSrcDependencyCatalogVersion?.({
name: 'nohttp-plugin',
version: application.javaDependencies?.['nohttp-checkstyle'],
});
source.addGradleBuildSrcDependency?.({
groupId: 'org.sonarsource.scanner.gradle',
artifactId: 'sonarqube-gradle-plugin',
// eslint-disable-next-line no-template-curly-in-string
version: '${libs.versions.sonarqube.plugin.get()}',
scope: 'implementation',
});
source.addGradleBuildSrcDependency?.({
groupId: 'com.diffplug.spotless',
artifactId: 'spotless-plugin-gradle',
// eslint-disable-next-line no-template-curly-in-string
version: '${libs.versions.spotless.plugin.get()}',
scope: 'implementation',
});
source.addGradleBuildSrcDependency?.({
groupId: 'com.github.andygoossens',
artifactId: 'gradle-modernizer-plugin',
// eslint-disable-next-line no-template-curly-in-string
version: '${libs.versions.modernizer.plugin.get()}',
scope: 'implementation',
});
source.addGradleBuildSrcDependency?.({
groupId: 'io.spring.nohttp',
artifactId: 'nohttp-gradle',
// eslint-disable-next-line no-template-curly-in-string
version: '${libs.versions.nohttp.plugin.get()}',
scope: 'implementation',
});
source.addGradlePlugin?.({ id: 'jhipster.code-quality-conventions' });
},
packageJsonScripts({ application }) {
const packageJsonConfigStorage = this.packageJson.createStorage('config').createProxy();
packageJsonConfigStorage.backend_port = application.gatewayServerPort || application.serverPort;
Expand Down
42 changes: 0 additions & 42 deletions generators/server/templates/build.gradle.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ plugins {
id "maven-publish"
id "idea"
id "eclipse"
id "jacoco"
id "org.springframework.boot"
id "com.google.cloud.tools.jib"
id "com.gorylenko.gradle-git-properties"
Expand All @@ -31,10 +30,6 @@ plugins {
<%_ if (!skipClient) { _%>
id "com.github.node-gradle.node"
<%_ } _%>
id "org.sonarqube"
id "com.diffplug.spotless"
id "io.spring.nohttp"
id "com.github.andygoossens.gradle-modernizer-plugin"
// jhipster-needle-gradle-plugins - JHipster will add additional gradle plugins here
}

Expand All @@ -60,12 +55,6 @@ ext {
apply from: "gradle/docker.gradle"
apply from: "gradle/sonar.gradle"

spotless {
java {
target 'src/*/java/**/*.java'
// removeUnusedImports()
}
}

<%_ if (enableSwaggerCodegen) { _%>
apply from: "gradle/swagger.gradle"
Expand Down Expand Up @@ -144,10 +133,6 @@ test {
<%_ } _%>
}

modernizer {
failOnViolations = true
includeTestClasses = true
}

check.dependsOn integrationTest
task testReport(type: TestReport) {
Expand All @@ -169,26 +154,11 @@ tasks.withType(com.gorylenko.GenerateGitPropertiesTask).configureEach {
outputs.doNotCacheIf("Task is always executed") { true }
}

checkstyle {
toolVersion "${checkstyleVersion}"
configFile file("checkstyle.xml")
checkstyleTest.enabled = false
}
nohttp {
source.include "build.gradle", "README.md"
}

configurations {
providedRuntime
implementation.exclude module: "spring-boot-starter-tomcat"
}

// workaround for https://github.com/checkstyle/checkstyle/issues/14123
configurations.checkstyle {
resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
select("com.google.guava:guava:0")
}
}

repositories {
// Local maven repository is required for libraries built locally with maven like development jhipster-bom.
Expand Down Expand Up @@ -410,18 +380,6 @@ if (addSpringMilestoneRepository) { _%>
// jhipster-needle-gradle-dependency - JHipster will add additional dependencies here
}

if (project.hasProperty("gae")) {
task createPom {
def basePath = 'build/resources/main/META-INF/maven'
doLast {
pom {
withXml(dependencyManagement.pomConfigurer)
}.writeTo("${basePath}/${project.group}/${project.name}/pom.xml")
}
}
bootJar.dependsOn = [createPom]
}

task cleanResources(type: Delete) {
delete "build/resources"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
plugins {
id "jacoco"
id "org.sonarqube"
id "com.diffplug.spotless"
id "com.github.andygoossens.gradle-modernizer-plugin"
id "io.spring.nohttp"
}

jacoco {
toolVersion = "${libs.versions.jacoco.get()}"
}

jacocoTestReport {
executionData tasks.withType(Test)
classDirectories.from = files(sourceSets.main.output.classesDirs)
sourceDirectories.from = files(sourceSets.main.java.srcDirs)

reports {
xml.required = true
}
}

file("sonar-project.properties").withReader {
Properties sonarProperties = new Properties()
sonarProperties.load(it)

sonarProperties.each { key, value ->
sonarqube {
properties {
property key, value
}
}
}
}

spotless {
java {
target 'src/*/java/**/*.java'
// removeUnusedImports()
}
}

modernizer {
failOnViolations = true
includeTestClasses = true
}

checkstyle {
toolVersion "${libs.versions.checkstyle.get()}"
configFile file("checkstyle.xml")
checkstyleTest.enabled = false
}

nohttp {
source.include "build.gradle", "README.md"
}

// workaround for https://github.com/checkstyle/checkstyle/issues/14123
configurations.checkstyle {
resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
select("com.google.guava:guava:0")
}
}
5 changes: 0 additions & 5 deletions generators/server/templates/gradle.properties.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ gitPropertiesPluginVersion=<%- javaDependencies['gradle-git-properties'] %>
<%_ if (!skipClient) { _%>
gradleNodePluginVersion=<%- javaDependencies['node-gradle'] %>
<%_ } _%>
sonarqubePluginVersion=<%- javaDependencies['gradle-sonarqube'] %>
spotlessPluginVersion=<%- javaDependencies['spotless-gradle-plugin'] %>
<%_ if (enableSwaggerCodegen) { _%>
openapiPluginVersion=<%- javaDependencies['openapi-generator-maven-plugin'] %>
<%_ } _%>
noHttpCheckstyleVersion=<%- javaDependencies['nohttp-checkstyle'] %>
checkstyleVersion=<%- javaDependencies.checkstyle %>
modernizerPluginVersion=<%- javaDependencies['gradle-modernizer-plugin'] %>
<%_ if (enableGradleEnterprise) { _%>
gradleEnterprisePluginVersion=<%- javaDependencies['gradle-enterprise'] %>
gradleCommonCustomUserDataPluginVersion=<%- javaDependencies['common-custom-user-data-gradle-plugin'] %>
Expand Down
44 changes: 0 additions & 44 deletions generators/server/templates/gradle/sonar.gradle.ejs

This file was deleted.

Loading

0 comments on commit f4c73c1

Please sign in to comment.