Skip to content

Commit

Permalink
add pom.xml generation (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev authored Sep 6, 2023
1 parent f822958 commit 6b0a45b
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br> 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` |
Expand Down
17 changes: 17 additions & 0 deletions hooks/06_chooseMavenOrGradle.js
Original file line number Diff line number Diff line change
@@ -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'));
}
}
};
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
146 changes: 146 additions & 0 deletions template/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>{{ params['userJavaPackage'] }}</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
{%- if asyncapi | isProtocol('amqp') %}
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-amqp</artifactId>
<scope>compile</scope>
</dependency>
{% endif -%}
{%- if asyncapi | isProtocol('mqtt') %}
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-mqtt</artifactId>
<scope>compile</scope>
</dependency>
{% endif -%}
{%- if asyncapi | isProtocol('kafka') %}
{%- if params.springBoot2 %}
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.9.12</version>
<scope>test</scope>
</dependency>
{% else %}
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
{% endif -%}
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
{% endif -%}
{%- if params.springBoot2 %}
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<version>2.7.15</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.15</version>
<scope>test</scope>
</dependency>
{% else %}
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
{% endif -%}
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>{%- if params.springBoot2 %}2.7.15{% else %}3.1.3{% endif %}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>{%- if params.springBoot2 %}2.7.15{% else %}3.1.3{% endif %}</version>
</plugin>
</plugins>
</build>

</project>
134 changes: 134 additions & 0 deletions tests/__snapshots__/parameters.test.js.snap
Original file line number Diff line number Diff line change
@@ -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`] = `
"<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.asyncapi</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.16.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.3</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.3</version>
</plugin>
</plugins>
</build>
</project>"
`;
exports[`integration tests for generated files under different template parameters should generate spring 2 code with parameter 1`] = `
"package com.asyncapi.service;
Expand Down
50 changes: 50 additions & 0 deletions tests/parameters.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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();
}
});
});

0 comments on commit 6b0a45b

Please sign in to comment.