Skip to content

Commit

Permalink
add addJavaDependencies api to sources
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Aug 13, 2024
1 parent 1fa1cea commit 5fba4bd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 40 deletions.
19 changes: 18 additions & 1 deletion generators/java/generators/build-tool/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import BaseApplicationGenerator from '../../../base-application/index.js';
import { GENERATOR_GRADLE, GENERATOR_MAVEN } from '../../../generator-list.js';
import type { MavenDependency } from '../../../maven/types.js';
import { javaScopeToGradleScope } from '../../support/index.js';
import type { JavaDependency } from '../../types.js';
import type { ConditionalJavaDefinition, JavaDependency, JavaNeedleOptions } from '../../types.js';

const { GRADLE, MAVEN } = buildToolTypes;

Expand Down Expand Up @@ -132,6 +132,23 @@ export default class BuildToolGenerator extends BaseApplicationGenerator {
}
}
};

source.addJavaDefinitions = (
optionsOrDefinition: JavaNeedleOptions | ConditionalJavaDefinition,
...definitions: ConditionalJavaDefinition[]
) => {
let options: JavaNeedleOptions | undefined = undefined;
if ('gradleFile' in optionsOrDefinition || 'gradleVersionCatalogFile' in optionsOrDefinition) {
options = optionsOrDefinition;
} else {
definitions.unshift(optionsOrDefinition as ConditionalJavaDefinition);
}
for (const definition of definitions) {
if (definition.condition ?? true) {
source.addJavaDefinition!(definition, options);
}
}
};
},
});
}
Expand Down
6 changes: 6 additions & 0 deletions generators/java/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ export type JavaApplication = BaseApplication &
addOpenapiGeneratorPlugin: boolean;
};

export type ConditionalJavaDefinition = JavaDefinition & { condition?: boolean };

export type JavaSourceType = {
/**
* Add a JavaDefinition to the application.
* A version requires a valid version otherwise it will be ignored.
* A dependency with versionRef requires a valid referenced version at `versions` otherwise it will be ignored.
*/
addJavaDefinition?(definition: JavaDefinition, options?: JavaNeedleOptions): void;
addJavaDefinitions?(
optionsOrDefinition: JavaNeedleOptions | ConditionalJavaDefinition,
...definitions: ConditionalJavaDefinition[]
): void;
addJavaDependencies?(dependency: JavaDependency[], options?: JavaNeedleOptions): void;
hasJavaProperty?(propertyName: string): boolean;
hasJavaManagedProperty?(propertyName: string): boolean;
Expand Down
23 changes: 12 additions & 11 deletions generators/spring-cache/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
import BaseApplicationGenerator from '../base-application/index.js';
import { createNeedleCallback } from '../base/support/needles.js';
import { JavaDependency, JavaDependencyVersion } from '../java/types.js';
import writeTask from './files.js';
import cleanupTask from './cleanup.js';
import { getCacheProviderMavenDefinition } from './internal/dependencies.js';
Expand Down Expand Up @@ -137,18 +136,20 @@ export default class SpringCacheGenerator extends BaseApplicationGenerator {
const { javaDependencies } = application;
const { cacheProvider, enableHibernateCache } = application as any;

const dependencies: JavaDependency[] = [{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-cache' }];
const versions: JavaDependencyVersion[] = [];
const definition = getCacheProviderMavenDefinition(cacheProvider, javaDependencies);
versions.push(...(definition.base.versions ?? []));
dependencies.push(...definition.base.dependencies);
if (enableHibernateCache && definition.hibernateCache) {
versions.push(...(definition.hibernateCache.versions ?? []));
dependencies.push(...definition.hibernateCache.dependencies);
}
source.addJavaDefinition?.(
{ dependencies, versions },
source.addJavaDefinitions?.(
{ gradleFile: 'buildSrc/src/main/groovy/jhipster.spring-cache-conventions.gradle' },
{
versions: definition.base.versions,
dependencies: [
{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-cache' },
...definition.base.dependencies,
],
},
{
condition: enableHibernateCache && definition.hibernateCache,
...definition.hibernateCache,
},
);
},
});
Expand Down
59 changes: 31 additions & 28 deletions generators/spring-data-relational/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,37 @@ export default class SqlGenerator extends BaseApplicationGenerator<SpringBootGen
addDependencies({ application, source }) {
const { reactive, javaDependencies, packageFolder } = application;

if (reactive) {
source.addJavaDependencies?.([
{ groupId: 'commons-beanutils', artifactId: 'commons-beanutils', version: javaDependencies['commons-beanutils'] },
{ groupId: 'jakarta.persistence', artifactId: 'jakarta.persistence-api' },
{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-data-r2dbc' },
]);
} else {
source.addJavaDependencies?.([
{ groupId: 'com.fasterxml.jackson.datatype', artifactId: 'jackson-datatype-hibernate6' },
{ groupId: 'org.hibernate.orm', artifactId: 'hibernate-core' },
{ groupId: 'org.hibernate.validator', artifactId: 'hibernate-validator' },
{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-data-jpa' },
{ groupId: 'org.springframework.security', artifactId: 'spring-security-data' },
{ scope: 'annotationProcessor', groupId: 'org.hibernate.orm', artifactId: 'hibernate-jpamodelgen' },
]);
}

source.addJavaDependencies?.([
{ groupId: 'com.fasterxml.jackson.module', artifactId: 'jackson-module-jaxb-annotations' },
{ groupId: 'com.zaxxer', artifactId: 'HikariCP' },
{ scope: 'annotationProcessor', groupId: 'org.glassfish.jaxb', artifactId: 'jaxb-runtime' },
]);

source.addJavaDependencies?.([
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'jdbc' },
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'junit-jupiter' },
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'testcontainers' },
]);
source.addJavaDefinitions?.(
{
condition: reactive,
dependencies: [
{ groupId: 'commons-beanutils', artifactId: 'commons-beanutils', version: javaDependencies['commons-beanutils'] },
{ groupId: 'jakarta.persistence', artifactId: 'jakarta.persistence-api' },
{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-data-r2dbc' },
],
},
{
condition: !reactive,
dependencies: [
{ groupId: 'com.fasterxml.jackson.datatype', artifactId: 'jackson-datatype-hibernate6' },
{ groupId: 'org.hibernate.orm', artifactId: 'hibernate-core' },
{ groupId: 'org.hibernate.validator', artifactId: 'hibernate-validator' },
{ groupId: 'org.springframework.boot', artifactId: 'spring-boot-starter-data-jpa' },
{ groupId: 'org.springframework.security', artifactId: 'spring-security-data' },
{ scope: 'annotationProcessor', groupId: 'org.hibernate.orm', artifactId: 'hibernate-jpamodelgen' },
],
},
{
dependencies: [
{ groupId: 'com.fasterxml.jackson.module', artifactId: 'jackson-module-jaxb-annotations' },
{ groupId: 'com.zaxxer', artifactId: 'HikariCP' },
{ scope: 'annotationProcessor', groupId: 'org.glassfish.jaxb', artifactId: 'jaxb-runtime' },
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'jdbc' },
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'junit-jupiter' },
{ scope: 'test', groupId: 'org.testcontainers', artifactId: 'testcontainers' },
],
},
);

if (application.buildToolMaven) {
const { prodDatabaseType, devDatabaseTypeH2Any } = application as any;
Expand Down

0 comments on commit 5fba4bd

Please sign in to comment.