Skip to content

Commit

Permalink
Merge pull request #25498 from mshima/jhipster-bom
Browse files Browse the repository at this point in the history
inject jhipster-bom gradle/maven dependencies using api.
  • Loading branch information
DanielFran authored Mar 16, 2024
2 parents 4bb1d8c + 0f0f35f commit a2dde89
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 79 deletions.
14 changes: 14 additions & 0 deletions generators/gradle/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import {
addGradleBuildSrcDependencyCallback,
addGradleDependencyCatalogVersionCallback,
addGradleBuildSrcDependencyCatalogVersionCallback,
addGradleDependencyCatalogLibrariesCallback,
addGradleDependencyCatalogPluginsCallback,
addGradleDependencyFromCatalogCallback,
addGradlePluginFromCatalogCallback,
} from './internal/needles.js';

export default class GradleGenerator extends BaseApplicationGenerator {
Expand Down Expand Up @@ -81,6 +85,16 @@ export default class GradleGenerator extends BaseApplicationGenerator {
this.editFile(`${GRADLE_BUILD_SRC_DIR}/build.gradle`, addGradleBuildSrcDependencyCallback(dependency));
source.addGradleDependencyCatalogVersion = version =>
this.editFile('gradle/libs.versions.toml', addGradleDependencyCatalogVersionCallback(version));
source.addGradleDependencyCatalogLibraries = libs => {
this.editFile('gradle/libs.versions.toml', addGradleDependencyCatalogLibrariesCallback(libs));
this.editFile('build.gradle', addGradleDependencyFromCatalogCallback(libs));
};
source.addGradleDependencyCatalogLibrary = lib => source.addGradleDependencyCatalogLibraries!([lib]);
source.addGradleDependencyCatalogPlugins = plugins => {
this.editFile('gradle/libs.versions.toml', addGradleDependencyCatalogPluginsCallback(plugins));
this.editFile('build.gradle', addGradlePluginFromCatalogCallback(plugins));
};
source.addGradleDependencyCatalogPlugin = plugin => source.addGradleDependencyCatalogPlugins!([plugin]);
source.addGradleBuildSrcDependencyCatalogVersion = version =>
this.editFile(`${GRADLE_BUILD_SRC_DIR}/gradle/libs.versions.toml`, addGradleBuildSrcDependencyCatalogVersionCallback(version));
},
Expand Down
56 changes: 55 additions & 1 deletion generators/gradle/internal/needles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,24 @@
* limitations under the License.
*/
import { createNeedleCallback } from '../../base/support/index.js';
import { GradleScript, GradleDependency, GradlePlugin, GradleProperty, GradleRepository, GradleTomlVersion } from '../types.js';
import type {
GradleScript,
GradleDependency,
GradlePlugin,
GradleProperty,
GradleRepository,
GradleTomlVersion,
GradleLibrary,
GradleTomlPlugin,
} from '../types.js';

const tomlItemToString = (item: Record<string, string>) =>
`{ ${Object.entries(item)
.filter(([_key, value]) => value !== undefined)
.map(([key, value]) => `${key} = "${value}"`)
.join(', ')} }`;

const gradleNameToReference = (name: string) => name.replaceAll('-', '.');

export const applyFromGradleCallback = ({ script }: GradleScript) =>
createNeedleCallback({
Expand All @@ -43,11 +60,48 @@ export const addGradleDependencyCatalogVersionCallback = ({ name, version }: Gra
contentToAdd: `${name} = "${version}"`,
});

export const addGradleDependencyCatalogLibrariesCallback = (libraries: GradleLibrary[]) =>
createNeedleCallback({
needle: 'gradle-dependency-catalog-libraries',
contentToAdd: libraries.map(({ libraryName, scope: _scope, ...others }) =>
'library' in others ? `${libraryName} = "${others.library}"` : `${libraryName} = ${tomlItemToString(others)}`,
),
});

export const addGradleDependencyFromCatalogCallback = (libraries: GradleLibrary[]) =>
createNeedleCallback({
needle: 'gradle-dependency',
contentToAdd: libraries
.filter(({ scope }) => scope)
.map(({ libraryName, scope }) =>
scope === 'implementation platform'
? `${scope}(libs.${gradleNameToReference(libraryName)})`
: `${scope} libs.${gradleNameToReference(libraryName)}`,
),
});

export const addGradleDependencyCatalogPluginsCallback = (plugins: GradleTomlPlugin[]) =>
createNeedleCallback({
needle: 'gradle-dependency-catalog-plugins',
contentToAdd: plugins.map(({ pluginName, addToBuild: _addToBuild, ...others }) =>
'plugin' in others ? `${pluginName} = "${others.plugin}"` : `${pluginName} = ${tomlItemToString(others)}`,
),
});

export const addGradlePluginFromCatalogCallback = (plugins: GradleTomlPlugin[]) =>
createNeedleCallback({
needle: 'gradle-plugins',
contentToAdd: plugins
.filter(({ addToBuild }) => addToBuild)
.map(({ pluginName }) => `alias(libs.plugins.${gradleNameToReference(pluginName)})`),
});

export const addGradleBuildSrcDependencyCatalogVersionCallback = ({ name, version }: GradleTomlVersion) =>
createNeedleCallback({
needle: 'gradle-build-src-dependency-catalog-version',
contentToAdd: `${name} = "${version}"`,
});

export const addGradlePluginCallback = ({ id, version }: GradlePlugin) =>
createNeedleCallback({
needle: 'gradle-plugins',
Expand Down
47 changes: 47 additions & 0 deletions generators/gradle/needles.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ class mockBlueprintSubGen extends BaseApplicationGenerator {
source.addGradleBuildSrcDependency?.({ scope: 'scope5', groupId: 'group5', artifactId: 'name5', version: 'version5' });
source.addGradleDependencyCatalogVersion?.({ name: 'version-name', version: 'version' });
source.addGradleBuildSrcDependencyCatalogVersion?.({ name: 'version-name', version: 'version' });

source.addGradleDependencyCatalogLibraries?.([
{ libraryName: 'library-foo1', module: 'group-foo1:artifact-foo1', version: 'version1' },
{ libraryName: 'library-foo2', module: 'group-foo2:artifact-foo2', version: 'version2', scope: 'implementation' },
{ libraryName: 'library-foo3', module: 'group-foo3:artifact-foo3', version: 'version3', scope: 'implementation platform' },
{ libraryName: 'library-foo4', library: 'group-foo4:artifact-foo4:version4', scope: 'implementation' },
{ libraryName: 'library-foo5', group: 'group-foo5', name: 'artifact-foo5', version: 'version5', scope: 'implementation' },
]);
source.addGradleDependencyCatalogPlugins?.([
{ pluginName: 'plugin-foo1', id: 'group-foo1:plugin-foo1', version: 'version1' },
{ pluginName: 'plugin-foo2', id: 'group-foo2:plugin-foo2', version: 'version2', addToBuild: true },
{ pluginName: 'plugin-foo3', plugin: 'group-foo3:plugin-foo3:version3', addToBuild: true },
]);
},
});
}
Expand Down Expand Up @@ -87,4 +100,38 @@ describe('needle API server gradle: JHipster server generator with blueprint', (
it('Assert buildSrc/gradle/libs.versions.toml has the version added', () => {
runResult.assertFileContent('buildSrc/gradle/libs.versions.toml', 'version-name = "version"');
});

it('Assert gradle/libs.versions.toml has the library added', () => {
runResult.assertFileContent(
'gradle/libs.versions.toml',
'library-foo1 = { module = "group-foo1:artifact-foo1", version = "version1" }',
);
runResult.assertFileContent(
'gradle/libs.versions.toml',
'library-foo2 = { module = "group-foo2:artifact-foo2", version = "version2" }',
);
runResult.assertFileContent(
'gradle/libs.versions.toml',
'library-foo3 = { module = "group-foo3:artifact-foo3", version = "version3" }',
);
runResult.assertFileContent('gradle/libs.versions.toml', 'library-foo4 = "group-foo4:artifact-foo4:version4"');
runResult.assertFileContent(
'gradle/libs.versions.toml',
'library-foo5 = { group = "group-foo5", name = "artifact-foo5", version = "version5" }',
);

runResult.assertFileContent('build.gradle', 'implementation libs.library.foo2');
runResult.assertFileContent('build.gradle', 'implementation platform(libs.library.foo3)');
runResult.assertFileContent('build.gradle', 'implementation libs.library.foo4');
runResult.assertFileContent('build.gradle', 'implementation libs.library.foo5');
});

it('Assert gradle/libs.versions.toml has the plugin added', () => {
runResult.assertFileContent('gradle/libs.versions.toml', 'plugin-foo1 = { id = "group-foo1:plugin-foo1", version = "version1" }');
runResult.assertFileContent('gradle/libs.versions.toml', 'plugin-foo2 = { id = "group-foo2:plugin-foo2", version = "version2" }');
runResult.assertFileContent('gradle/libs.versions.toml', 'plugin-foo3 = "group-foo3:plugin-foo3:version3"');

runResult.assertFileContent('build.gradle', 'alias(libs.plugins.plugin.foo2)');
runResult.assertFileContent('build.gradle', 'alias(libs.plugins.plugin.foo3)');
});
});
8 changes: 7 additions & 1 deletion generators/gradle/templates/gradle/libs.versions.toml.ejs
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[versions]
# jhipster-needle-gradle-dependency-catalog-version - JHipster will add additional versions for convention plugins here
# jhipster-needle-gradle-dependency-catalog-version - JHipster will add additional versions for convention plugins heref

[libraries]
# jhipster-needle-gradle-dependency-catalog-libraries - JHipster will add additional libraries versions

[plugins]
# jhipster-needle-gradle-dependency-catalog-plugins - JHipster will add additional plugins versions
18 changes: 18 additions & 0 deletions generators/gradle/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ export type GradleRepository = { url: string; username?: string; password?: stri

export type GradleTomlVersion = { name: string; version?: string };

export type GradleTomlAnyItemVersion = { version: string } | { ['version.ref']: string };

export type GradleTomlLibraryId = { module: string } | { group: string; name: string };

export type GradleLibrary = { libraryName: string; scope?: string } & (
| { library: string }
| (GradleTomlLibraryId & GradleTomlAnyItemVersion)
);

export type GradleTomlPlugin = { pluginName: string; addToBuild?: boolean } & (
| { plugin: string }
| ({ id: string } & GradleTomlAnyItemVersion)
);

export type GradleSourceType = {
applyFromGradle?(script: GradleScript): void;
addGradleDependency?(dependency: GradleDependency): void;
Expand All @@ -19,5 +33,9 @@ export type GradleSourceType = {
addGradleMavenRepository?(repository: GradleRepository): void;
addGradleBuildSrcDependency?(dependency: GradleDependency): void;
addGradleDependencyCatalogVersion?(catalogVersion: GradleTomlVersion): void;
addGradleDependencyCatalogLibrary?(catalogVersion: GradleLibrary): void;
addGradleDependencyCatalogLibraries?(catalogVersion: GradleLibrary[]): void;
addGradleDependencyCatalogPlugin?(catalogVersion: GradleTomlPlugin): void;
addGradleDependencyCatalogPlugins?(catalogVersion: GradleTomlPlugin[]): void;
addGradleBuildSrcDependencyCatalogVersion?(catalogVersion: GradleTomlVersion): void;
};
29 changes: 1 addition & 28 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 "org.springframework.boot"
id "com.gorylenko.gradle-git-properties"
<%_ if (enableSwaggerCodegen) { _%>
id "org.openapi.generator"
Expand Down Expand Up @@ -56,7 +55,7 @@ apply from: "gradle/swagger.gradle"
<%_ } _%>
// jhipster-needle-gradle-apply-from - JHipster will add additional gradle scripts to be applied here

if (project.hasProperty("prod") || project.hasProperty("gae")) {
if (project.hasProperty("prod")) {
apply from: "gradle/profile_prod.gradle"
} else {
apply from: "gradle/profile_dev.gradle"
Expand All @@ -66,19 +65,6 @@ if (project.hasProperty("war")) {
apply from: "gradle/war.gradle"
}

if (project.hasProperty("gae")) {
apply plugin: 'maven'
apply plugin: 'org.springframework.boot.experimental.thin-launcher'
apply plugin: 'io.spring.dependency-management'

dependencyManagement {
imports {
mavenBom "tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}"
}
}
appengineStage.dependsOn thinResolve
}

<%_ if (serviceDiscoveryAny || applicationTypeGateway || applicationTypeMicroservice) { _%>
if (project.hasProperty("zipkin")) {
apply from: "gradle/zipkin.gradle"
Expand Down Expand Up @@ -177,10 +163,6 @@ repositories {
}

dependencies {
// import JHipster dependencies BOM
if (!project.hasProperty("gae")) {
implementation platform("tech.jhipster:jhipster-dependencies:${jhipsterDependenciesVersion}")
}
<%_ if (databaseTypeCouchbase) { _%>
implementation "com.couchbase.client:java-client"
<%_ } _%>
Expand Down Expand Up @@ -282,12 +264,6 @@ dependencies {
implementation "org.reflections:reflections:0.10.1", {
exclude group: "org.javassist", module: "javassist"
}
<%_ } _%>
<%_ if (reactive) { _%>
implementation "org.springdoc:springdoc-openapi-starter-webflux-api"
<%_ } _%>
<%_ if (!reactive) { _%>
implementation "org.springdoc:springdoc-openapi-starter-webmvc-api"
<%_ } _%>
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor:${springBootVersion}"
implementation "org.springframework.boot:spring-boot-loader-tools"
Expand Down Expand Up @@ -403,9 +379,6 @@ if (addSpringMilestoneRepository) { _%>
<%_ if (databaseTypeMongodb || databaseTypeCouchbase || databaseTypeNeo4j || databaseTypeCassandra || searchEngineElasticsearch || databaseTypeSql) { _%>
testImplementation "org.testcontainers:testcontainers"
<%_ } _%>
// Use ", version: jhipsterDependenciesVersion, changing: true" if you want
// to use a SNAPSHOT release instead of a stable release
implementation "tech.jhipster:jhipster-framework"
<%_ if (reactive) { _%>
OperatingSystem os = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.getCurrentOperatingSystem();
Architecture arch = org.gradle.nativeplatform.platform.internal.DefaultNativePlatform.getCurrentArchitecture();
Expand Down
3 changes: 0 additions & 3 deletions generators/server/templates/gradle.properties.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ rootProject.name=<%= dasherizedBaseName %>
profile=dev

# Dependency versions
jhipsterDependenciesVersion=<%= jhipsterDependenciesVersion %>
# The spring-boot version should match the one managed by
# https://mvnrepository.com/artifact/tech.jhipster/jhipster-dependencies/<%= jhipsterDependenciesVersion %>
springBootVersion=<%- javaDependencies['spring-boot'] %>
<%_ if (databaseTypeSql && !reactive) { _%>
# The hibernate version should match the one managed by
Expand Down
26 changes: 0 additions & 26 deletions generators/server/templates/pom.xml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
<profile.e2e />
<profile.test />

<jhipster-dependencies.version><%= jhipsterDependenciesVersion %></jhipster-dependencies.version>
<spring-boot.version><%- javaDependencies['spring-boot'] %></spring-boot.version>
<%_ if (reactive) { _%>
<blockhound-junit-platform.version><%- javaDependencies['blockhound-junit-platform'] %></blockhound-junit-platform.version>
<micrometer-context-propagation.version><%- javaDependencies['micrometer-context-propagation'] %></micrometer-context-propagation.version>
Expand Down Expand Up @@ -102,18 +100,6 @@
<spotless-maven-plugin.version><%- javaDependencies['spotless-maven-plugin'] %></spotless-maven-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>tech.jhipster</groupId>
<artifactId>jhipster-dependencies</artifactId>
<version>${jhipster-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
Expand Down Expand Up @@ -216,14 +202,6 @@
<version>${jackson-databind-nullable.version}</version>
</dependency>
<%_ } _%>
<dependency>
<groupId>org.springdoc</groupId>
<%_ if (reactive) { _%>
<artifactId>springdoc-openapi-starter-webflux-api</artifactId>
<%_ } else { _%>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<%_ } _%>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
Expand Down Expand Up @@ -362,10 +340,6 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>tech.jhipster</groupId>
<artifactId>jhipster-framework</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
1 change: 0 additions & 1 deletion generators/server/templates/settings.gradle.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pluginManagement {
// jhipster-needle-gradle-plugin-management-repositories - JHipster will add additional entries here
}
plugins {
id 'org.springframework.boot' version "${springBootVersion}"
id 'com.google.cloud.tools.jib' version "${jibPluginVersion}"
id 'com.gorylenko.gradle-git-properties' version "${gitPropertiesPluginVersion}"
<%_ if (enableSwaggerCodegen) { _%>
Expand Down
Loading

0 comments on commit a2dde89

Please sign in to comment.