Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add java/build-tool generator #25730

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Options:
--incremental-changelog Creates incremental database changelogs
--skip-user-management Skip the user management module during app generation
--recreate-initial-changelog Recreate the initial database changelog based on the current config
--build <value> Provide build tool for the application when skipping server side generation
--cache-provider <value> Cache provider
--enable-swagger-codegen API first development using OpenAPI-generator
--enable-hibernate-cache Enable hibernate cache
Expand All @@ -52,6 +51,9 @@ Options:
--sync-user-with-idp Allow relationships with User for oauth2 applications
--with-generated-flag Add a GeneratedByJHipster annotation to all generated java classes and interfaces
--package-name <value> The package name for the generated application
--build <value> Provide build tool for the application when skipping server side generation (choices: "maven", "gradle")
--enable-gradle-enterprise Enable Gradle Enterprise integration
--gradle-enterprise-host <value> Gradle Enterprise Host
--dev-database-type <value> Development database
--client-framework <value> Provide client framework for the application (choices: "angular", "react", "vue", "no")
--microfrontend Enable microfrontend support
Expand Down
16 changes: 0 additions & 16 deletions generators/cucumber/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,6 @@ describe(`generator - ${generator}`, () => {
shouldSupportFeatures(Generator);
describe('blueprint support', () => testBlueprintSupport(generator));

describe('with unknown buildTool', () => {
before(async () => {
await helpers
.runJHipster(GENERATOR_CUCUMBER)
.onEnvironment(async env => {
await env.composeWith('jhipster:bootstrap-application');
})
.withMockedGenerators(['jhipster:java'])
.withJHipsterConfig({ buildTool: 'unknown', testFrameworks: ['cucumber'] });
});

it('should match files snapshot', () => {
expect(result.getSnapshot()).toMatchSnapshot();
});
});

describe('with default config', () => {
before(async () => {
await helpers
Expand Down
3 changes: 1 addition & 2 deletions generators/cucumber/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/
import BaseApplicationGenerator from '../base-application/index.js';
import { GENERATOR_JAVA } from '../generator-list.js';
import writeTask from './files.js';
import cleanupTask from './cleanup.js';

Expand All @@ -28,7 +27,7 @@ export default class CucumberGenerator extends BaseApplicationGenerator {
}

if (!this.delegateToBlueprint) {
await this.dependsOnJHipster(GENERATOR_JAVA);
await this.dependsOnJHipster('jhipster:java:build-tool');
}
}

Expand Down
53 changes: 53 additions & 0 deletions generators/gradle/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { JHipsterCommandDefinition } from '../base/api.js';

const command: JHipsterCommandDefinition = {
options: {},
configs: {
enableGradleEnterprise: {
cli: {
type: Boolean,
},
prompt: {
type: 'confirm',
message: 'Do you want to enable Gradle Enterprise integration?',
},
default: false,
description: 'Enable Gradle Enterprise integration',
scope: 'storage',
},
gradleEnterpriseHost: {
cli: {
type: String,
},
prompt: {
when: answers => answers.enableGradleEnterprise,
type: 'input',
message: 'Enter your Gradle Enterprise host',
validate: input => (input.length === 0 ? 'Please enter your Gradle Enterprise host' : true),
},
description: 'Gradle Enterprise Host',
scope: 'storage',
},
},
import: [],
};

export default command;
28 changes: 28 additions & 0 deletions generators/gradle/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ export default class GradleGenerator extends BaseApplicationGenerator {
}
}

get initializing() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
});
}

get [BaseApplicationGenerator.INITIALIZING]() {
return this.delegateTasksToBlueprint(() => this.initializing);
}

get prompting() {
return this.asPromptingTaskGroup({
async promptCommand({ control }) {
if (control.existingProject && this.options.askAnswered !== true) return;
await this.promptCurrentJHipsterCommand();
},
});
}

get [BaseApplicationGenerator.PROMPTING]() {
return this.delegateTasksToBlueprint(() => this.prompting);
}

get configuring() {
return this.asConfiguringTaskGroup({
configure() {
Expand All @@ -73,6 +98,9 @@ export default class GradleGenerator extends BaseApplicationGenerator {

get loading() {
return this.asLoadingTaskGroup({
async loadConfig({ application }) {
await this.loadCurrentJHipsterCommandConfig(application);
},
loadGradleVersion({ application }) {
const propFile = this.readTemplate(this.jhipsterTemplatePath('gradle/wrapper/gradle-wrapper.properties'));
this.gradleVersionFromWrapper = propFile?.toString().match(/gradle-(\d+\.\d+(?:\.\d+)?)-/)?.[1];
Expand Down
1 change: 1 addition & 0 deletions generators/gradle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
* limitations under the License.
*/
export { default } from './generator.js';
export { default as command } from './command.js';
2 changes: 1 addition & 1 deletion generators/java/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { JHipsterCommandDefinition } from '../base/api.js';

const command: JHipsterCommandDefinition = {
options: {},
import: ['jhipster:java:bootstrap', 'jhipster:java:domain'],
import: ['jhipster:java:bootstrap', 'jhipster:java:domain', 'jhipster:java:build-tool'],
};

export default command;
87 changes: 6 additions & 81 deletions generators/java/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/

import BaseApplicationGenerator from '../base-application/index.js';
import { javaScopeToGradleScope } from './support/index.js';
import type { JavaDependency } from './types.js';
import type { MavenDependency } from '../maven/types.js';

export default class JavaGenerator extends BaseApplicationGenerator {
async beforeQueue() {
Expand All @@ -33,87 +30,15 @@ export default class JavaGenerator extends BaseApplicationGenerator {
}
}

get preparing() {
return this.asPreparingTaskGroup({
prepareJavaApplication({ application, source }) {
source.addJavaDependencies = (dependencies, options) => {
if (application.buildToolMaven) {
const annotationProcessors = dependencies.filter(dep => dep.scope === 'annotationProcessor');
const importDependencies = dependencies.filter(dep => dep.scope === 'import');
const commonDependencies = dependencies.filter(dep => !['annotationProcessor', 'import'].includes(dep.scope!));
const convertVersionToRef = ({ version, versionRef, ...artifact }: JavaDependency): MavenDependency =>
version || versionRef ? { ...artifact, version: `\${${versionRef ?? artifact.artifactId}.version}` } : artifact;
const removeScope = ({ scope: _scope, ...artifact }: MavenDependency) => artifact;

source.addMavenDefinition?.({
properties: dependencies
.filter(dep => dep.version)
.map(({ artifactId, version }) => ({ property: `${artifactId}.version`, value: version })),
dependencies: [
...commonDependencies.map(convertVersionToRef),
// Add a provided scope for annotation processors so that version is not required in annotationProcessor dependencies
...annotationProcessors.filter(dep => !dep.version).map(artifact => ({ ...artifact, scope: 'provided' })),
],
dependencyManagement: importDependencies.map(convertVersionToRef),
annotationProcessors: annotationProcessors.map(convertVersionToRef).map(removeScope),
});
}

if (application.buildToolGradle) {
source.addGradleDependencies?.(
dependencies
.filter(dep => !dep.version && !dep.versionRef)
.map(({ scope, type, ...artifact }) => ({
...artifact,
scope: javaScopeToGradleScope({ scope, type }),
})),
options,
);
source.addGradleDependencyCatalogLibraries?.(
dependencies
.filter(dep => dep.version || dep.versionRef)
.map(({ scope, type, groupId, artifactId, version, versionRef }) => {
const library = {
libraryName: artifactId,
module: `${groupId}:${artifactId}`,
scope: javaScopeToGradleScope({ scope, type }),
};
return version ? { ...library, version } : { ...library, 'version.ref': versionRef! };
}),
options,
);
}
};

source.addJavaDefinition = (definition, options) => {
const { dependencies, versions } = definition;
if (dependencies) {
source.addJavaDependencies!(
dependencies.filter(dep => {
if (dep.versionRef) {
return versions?.find(({ name }) => name === dep.versionRef)?.version;
}
return true;
}),
options,
);
}
if (versions) {
if (application.buildToolMaven) {
source.addMavenDefinition!({
properties: versions.filter(v => v.version).map(({ name, version }) => ({ property: `${name}.version`, value: version })),
});
}
if (application.buildToolGradle) {
source.addGradleDependencyCatalogVersions?.(versions, options);
}
}
};
get initializing() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
});
}

get [BaseApplicationGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
get [BaseApplicationGenerator.INITIALIZING]() {
return this.delegateTasksToBlueprint(() => this.initializing);
}
}
46 changes: 46 additions & 0 deletions generators/java/generators/build-tool/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { buildToolTypes } from '../../../../jdl/index.js';
import type { JHipsterCommandDefinition } from '../../../base/api.js';
import { GENERATOR_GRADLE } from '../../../generator-list.js';

const { GRADLE, MAVEN } = buildToolTypes;

const command: JHipsterCommandDefinition = {
options: {},
configs: {
buildTool: {
cli: {
name: 'build',
type: String,
},
prompt: {
type: 'list',
message: 'Would you like to use Maven or Gradle for building the backend?',
},
choices: [MAVEN, GRADLE],
default: MAVEN,
description: 'Provide build tool for the application when skipping server side generation',
scope: 'storage',
},
},
import: [GENERATOR_GRADLE],
};

export default command;
Loading
Loading