diff --git a/README.md b/README.md index 44e4aa202..dbea56727 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ In order for the generator to know what names to use for some parameters it's ne |inverseOperations|Generate an application that will publish messages to `publish` operation of channels and read messages from `subscribe` operation of channels. Literally this flag will simply swap `publish` and `subscribe` operations in the channels.
This flag will be useful when you want to generate a code of mock for your main application. Be aware, generation could be incomplete and manual changes will be required e.g. if bindings are defined only for case of main application.| No | `false` | |javaPackage|The Java package of the generated classes. Alternatively you can set the specification extension `info.x-java-package`. If both extension and parameter are used, parameter has more priority.| No | `com.asyncapi` | |springBoot2|Generate template files for the Spring Boot version 2. For kafka protocol it will also force to use spring-kafka 2.9.9| No | `false` | +|maven|Generate pom.xml Maven build file instead of Gradle build.|No | `false` | |listenerPollTimeout|Only for Kafka. Timeout in ms to use when polling the consumer.| No | `3000` | |listenerConcurrency|Only for Kafka. Number of threads to run in the listener containers.| No | `3` | |addTypeInfoHeader|Only for Kafka. Add type information to message header.| No | `true` | diff --git a/hooks/06_chooseMavenOrGradle.js b/hooks/06_chooseMavenOrGradle.js new file mode 100644 index 000000000..ecb68cd8e --- /dev/null +++ b/hooks/06_chooseMavenOrGradle.js @@ -0,0 +1,17 @@ +const fs = require('fs'); +const path = require('path'); + +module.exports = { + 'generate:after': generator => { + if (generator.templateParams && + (generator.templateParams['maven'] === true || generator.templateParams['maven'] === 'true')) { + fs.unlinkSync(path.resolve(generator.targetDir, 'build.gradle')); + fs.unlinkSync(path.resolve(generator.targetDir, 'gradle.properties')); + fs.unlinkSync(path.resolve(generator.targetDir, 'gradlew')); + fs.unlinkSync(path.resolve(generator.targetDir, 'gradlew.bat')); + fs.rmdirSync(path.resolve(generator.targetDir, 'gradle'), {recursive: true}); + } else { + fs.unlinkSync(path.resolve(generator.targetDir, 'pom.xml')); + } + } +}; \ No newline at end of file diff --git a/package.json b/package.json index 561021980..f0330e7c1 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,11 @@ "description": "Generate template files for the Spring Boot version 2. For kafka protocol it will also force to use spring-kafka 2.9.9", "default": false, "required": false + }, + "maven": { + "description": "Generate pom.xml Maven build file instead of Gradle build", + "default": false, + "required": false } }, "generator": ">=1.8.27 <2.0.0", diff --git a/template/pom.xml b/template/pom.xml new file mode 100644 index 000000000..2d56f9be4 --- /dev/null +++ b/template/pom.xml @@ -0,0 +1,146 @@ + + + 4.0.0 + + {{ params['userJavaPackage'] }} + untitled + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + {%- if asyncapi | isProtocol('amqp') %} + + org.springframework.integration + spring-integration-amqp + compile + + {% endif -%} + {%- if asyncapi | isProtocol('mqtt') %} + + org.springframework.integration + spring-integration-mqtt + compile + + {% endif -%} + {%- if asyncapi | isProtocol('kafka') %} + {%- if params.springBoot2 %} + + org.springframework.kafka + spring-kafka + 2.9.12 + compile + + + org.springframework.kafka + spring-kafka-test + 2.9.12 + test + + {% else %} + + org.springframework.kafka + spring-kafka + compile + + + org.springframework.kafka + spring-kafka-test + test + + {% endif -%} + + junit + junit + 4.13.1 + test + + + org.testcontainers + kafka + 1.16.3 + test + + {% endif -%} + {%- if params.springBoot2 %} + + javax.validation + validation-api + compile + + + org.springframework.boot + spring-boot-starter-integration + 2.7.15 + compile + + + org.springframework.boot + spring-boot-starter-test + 2.7.15 + test + + {% else %} + + jakarta.validation + jakarta.validation-api + compile + + + org.springframework.boot + spring-boot-starter-integration + compile + + + org.springframework.boot + spring-boot-starter-test + test + + {% endif -%} + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + compile + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + compile + + + org.testcontainers + testcontainers + 1.16.3 + test + + + + + + + org.springframework.boot + spring-boot-dependencies + {%- if params.springBoot2 %}2.7.15{% else %}3.1.3{% endif %} + import + pom + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + {%- if params.springBoot2 %}2.7.15{% else %}3.1.3{% endif %} + + + + + \ No newline at end of file diff --git a/tests/__snapshots__/parameters.test.js.snap b/tests/__snapshots__/parameters.test.js.snap index a48552fe9..1551cd663 100644 --- a/tests/__snapshots__/parameters.test.js.snap +++ b/tests/__snapshots__/parameters.test.js.snap @@ -1,5 +1,139 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`integration tests for generated files under different template parameters should generate gradle build 1`] = ` +"plugins { + id 'org.springframework.boot' version "$springBootVersion" + id 'io.spring.dependency-management' version "$springDependencyManager" + id 'java' +} + +group = "com.asyncapi" +version = "0.0.1-SNAPSHOT" +sourceCompatibility = JavaVersion.VERSION_17 + +repositories { + mavenCentral() +} + +dependencies { + implementation('org.springframework.kafka:spring-kafka') + testImplementation('org.springframework.kafka:spring-kafka-test') + testImplementation('junit:junit:4.13.1') + testImplementation('org.testcontainers:kafka:1.16.3') + implementation('com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider') + implementation('com.fasterxml.jackson.datatype:jackson-datatype-jsr310') + implementation('jakarta.validation:jakarta.validation-api') + implementation('org.springframework.boot:spring-boot-starter-integration') + testImplementation('org.springframework.boot:spring-boot-starter-test') + testImplementation('org.testcontainers:testcontainers:1.16.3') +} +" +`; + +exports[`integration tests for generated files under different template parameters should generate gradle build 2`] = ` +"springBootVersion=3.1.3 +springDependencyManager=1.1.3" +`; + +exports[`integration tests for generated files under different template parameters should generate maven build 1`] = ` +" + + 4.0.0 + + com.asyncapi + untitled + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + org.springframework.kafka + spring-kafka + compile + + + org.springframework.kafka + spring-kafka-test + test + + + junit + junit + 4.13.1 + test + + + org.testcontainers + kafka + 1.16.3 + test + + + + jakarta.validation + jakarta.validation-api + compile + + + org.springframework.boot + spring-boot-starter-integration + compile + + + org.springframework.boot + spring-boot-starter-test + test + + + com.fasterxml.jackson.jaxrs + jackson-jaxrs-json-provider + compile + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + compile + + + org.testcontainers + testcontainers + 1.16.3 + test + + + + + + + org.springframework.boot + spring-boot-dependencies + 3.1.3 + import + pom + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.1.3 + + + + +" +`; + exports[`integration tests for generated files under different template parameters should generate spring 2 code with parameter 1`] = ` "package com.asyncapi.service; diff --git a/tests/parameters.test.js b/tests/parameters.test.js index 410ee0212..c15acb6d2 100644 --- a/tests/parameters.test.js +++ b/tests/parameters.test.js @@ -1,5 +1,6 @@ const path = require('path'); const Generator = require('@asyncapi/generator'); +const {existsSync} = require("fs"); const { readFile } = require('fs').promises; const MAIN_TEST_RESULT_PATH = path.join('tests', 'temp', 'integrationTestResult'); @@ -33,4 +34,53 @@ describe('integration tests for generated files under different template paramet expect(file).toMatchSnapshot(); } }); + + it('should generate maven build', async() => { + const outputDir = generateFolderName(); + const params = {"maven": true}; + const kafkaExamplePath = './mocks/kafka.yml'; + + const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params }); + await generator.generateFromFile(path.resolve('tests', kafkaExamplePath)); + + const expectedFiles = [ + '/pom.xml' + ]; + const notExpectedFiles = [ + '/build.gradle', + '/gradle.properties' + ]; + for (const index in expectedFiles) { + const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8'); + expect(file).toMatchSnapshot(); + } + for (const index in notExpectedFiles) { + expect(existsSync(path.join(outputDir, notExpectedFiles[index]))).toBeFalsy(); + } + }); + + + it('should generate gradle build', async() => { + const outputDir = generateFolderName(); + const params = {"maven": false}; + const kafkaExamplePath = './mocks/kafka.yml'; + + const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params }); + await generator.generateFromFile(path.resolve('tests', kafkaExamplePath)); + + const notExpectedFiles = [ + '/pom.xml' + ]; + const expectedFiles = [ + '/build.gradle', + '/gradle.properties' + ]; + for (const index in expectedFiles) { + const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8'); + expect(file).toMatchSnapshot(); + } + for (const index in notExpectedFiles) { + expect(existsSync(path.join(outputDir, notExpectedFiles[index]))).toBeFalsy(); + } + }); });