Skip to content

Commit 3e949d8

Browse files
mok-lieeadnbrownie
andauthored
support JAVA_HOME environment variable (#756)
* support JAVA_HOME environment variable fixes #661 * test(generator-cmd): add consideration of the JAVA_HOME variable * docs: add instructions for the use of JAVA_HOME * fix: add quotes to accept spaces in the JAVA_HOME path * fix: add quotation marks only for windows paths --------- Co-authored-by: Nils Braune <[email protected]>
1 parent 720b405 commit 3e949d8

File tree

6 files changed

+54
-27
lines changed

6 files changed

+54
-27
lines changed

apps/generator-cli/src/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se
1010
configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported). Please see
1111
[OpenAPITools/openapi-generator](https://github.com/OpenAPITools/openapi-generator).
1212

13-
The OpenAPI Generator is a Java project. `openapi-generator-cli` will download the appropriate JAR file and invoke the `java` executable to run the OpenAPI Generator. You must have the `java` binary executable available on your `PATH` for this to work.
13+
The OpenAPI Generator is a Java project. `openapi-generator-cli` will download the appropriate JAR file and invoke the `java` executable to run the OpenAPI Generator. You must have the `java` binary executable available on your `PATH` or have set `JAVA_HOME` correctly for this to work.
1414

1515
If you find this tool useful, please consider sponsoring this project financially via https://opencollective.com/openapi_generator or directly to [Kay Schecker](https://github.com/sponsors/kay-schecker) (the author of this tool) :pray:
1616

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const isWin = () => process.platform === 'win32';
2+
3+
/**
4+
* If JAVA_HOME is set, it returns `$JAVA_HOME/bin/java`
5+
* otherwise it returns `java` and it has to be in the `PATH`
6+
*/
7+
export const javaCmd: string = process.env['JAVA_HOME']
8+
? isWin()
9+
? `"${process.env['JAVA_HOME']}/bin/java"`
10+
: `${process.env['JAVA_HOME']}/bin/java`
11+
: 'java';

apps/generator-cli/src/app/services/generator.service.spec.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Test } from '@nestjs/testing';
22
import { GeneratorService } from './generator.service';
33
import { LOGGER } from '../constants';
4+
import { javaCmd } from '../helpers';
45
import { VersionManagerService } from './version-manager.service';
56
import { ConfigService } from './config.service';
67

@@ -126,20 +127,21 @@ describe('GeneratorService', () => {
126127
});
127128
});
128129

129-
const cmd = (name, appendix: string[]) => ({
130+
const cmd = (name: string, javaCmd: string, appendix: string[]) => ({
130131
name,
131-
command: `java -jar "/path/to/4.2.1.jar" generate ${appendix.join(
132+
command: `${javaCmd} -jar "/path/to/4.2.1.jar" generate ${appendix.join(
132133
' '
133134
)}`,
134135
});
135136

136137
const cmdWithCustomJar = (
137138
name: string,
139+
javaCmd: string,
138140
customJar: string,
139141
appendix: string[]
140142
) => ({
141143
name,
142-
command: `java -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(
144+
command: `${javaCmd} -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(
143145
' '
144146
)}`,
145147
});
@@ -148,19 +150,19 @@ describe('GeneratorService', () => {
148150
[
149151
'foo.json',
150152
[
151-
cmd('[angular] abc/app/pet.yaml', [
153+
cmd('[angular] abc/app/pet.yaml', javaCmd, [
152154
`--input-spec="${cwd}/abc/app/pet.yaml"`,
153155
`--output="${cwd}/generated-sources/openapi/typescript-angular/pet"`,
154156
`--generator-name="typescript-angular"`,
155157
`--additional-properties="fileNaming=kebab-case,apiModulePrefix=Pet,npmName=petRestClient,supportsES6=true,withInterfaces=true"`,
156158
]),
157-
cmd('[angular] abc/app/car.yaml', [
159+
cmd('[angular] abc/app/car.yaml', javaCmd, [
158160
`--input-spec="${cwd}/abc/app/car.yaml"`,
159161
`--output="${cwd}/generated-sources/openapi/typescript-angular/car"`,
160162
`--generator-name="typescript-angular"`,
161163
`--additional-properties="fileNaming=kebab-case,apiModulePrefix=Car,npmName=carRestClient,supportsES6=true,withInterfaces=true"`,
162164
]),
163-
cmd('[baz] def/app/pet.yaml', [
165+
cmd('[baz] def/app/pet.yaml', javaCmd, [
164166
`--input-spec="${cwd}/def/app/pet.yaml"`,
165167
`--name="pet"`,
166168
`--name-uc-first="Pet"`,
@@ -174,7 +176,7 @@ describe('GeneratorService', () => {
174176
'--some-bool',
175177
'--some-int=1',
176178
]),
177-
cmd('[baz] def/app/car.json', [
179+
cmd('[baz] def/app/car.json', javaCmd, [
178180
`--input-spec="${cwd}/def/app/car.json"`,
179181
`--name="car"`,
180182
`--name-uc-first="Car"`,
@@ -193,12 +195,12 @@ describe('GeneratorService', () => {
193195
[
194196
'bar.json',
195197
[
196-
cmd('[bar] api/cat.yaml', [
198+
cmd('[bar] api/cat.yaml', javaCmd, [
197199
`--input-spec="${cwd}/api/cat.yaml"`,
198200
`--output="bar/cat"`,
199201
'--some-bool',
200202
]),
201-
cmd('[bar] api/bird.json', [
203+
cmd('[bar] api/bird.json', javaCmd, [
202204
`--input-spec="${cwd}/api/bird.json"`,
203205
`--output="bar/bird"`,
204206
'--some-bool',
@@ -208,16 +210,26 @@ describe('GeneratorService', () => {
208210
[
209211
'bar.json',
210212
[
211-
cmdWithCustomJar('[bar] api/cat.yaml', '../some/custom.jar', [
212-
`--input-spec="${cwd}/api/cat.yaml"`,
213-
`--output="bar/cat"`,
214-
'--some-bool',
215-
]),
216-
cmdWithCustomJar('[bar] api/bird.json', '../some/custom.jar', [
217-
`--input-spec="${cwd}/api/bird.json"`,
218-
`--output="bar/bird"`,
219-
'--some-bool',
220-
]),
213+
cmdWithCustomJar(
214+
'[bar] api/cat.yaml',
215+
javaCmd,
216+
'../some/custom.jar',
217+
[
218+
`--input-spec="${cwd}/api/cat.yaml"`,
219+
`--output="bar/cat"`,
220+
'--some-bool',
221+
]
222+
),
223+
cmdWithCustomJar(
224+
'[bar] api/bird.json',
225+
javaCmd,
226+
'../some/custom.jar',
227+
[
228+
`--input-spec="${cwd}/api/bird.json"`,
229+
`--output="bar/bird"`,
230+
'--some-bool',
231+
]
232+
),
221233
],
222234
'../some/custom.jar',
223235
],
@@ -226,7 +238,7 @@ describe('GeneratorService', () => {
226238
[
227239
'no-glob.json',
228240
[
229-
cmd('[noGlob] http://example.local/openapi.json', [
241+
cmd('[noGlob] http://example.local/openapi.json', javaCmd, [
230242
`--input-spec="http://example.local/openapi.json"`,
231243
`--output="no-glob/openapi"`,
232244
`--name="openapi"`,

apps/generator-cli/src/app/services/generator.service.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as os from 'os';
1010
import { VersionManagerService } from './version-manager.service';
1111
import { ConfigService } from './config.service';
1212
import { LOGGER } from '../constants';
13+
import { javaCmd } from '../helpers';
1314

1415
interface GeneratorConfig {
1516
glob: string;
@@ -241,7 +242,8 @@ export class GeneratorService {
241242
this.isWin() ? ';' : ':'
242243
)}" org.openapitools.codegen.OpenAPIGenerator`
243244
: `-jar "${cliPath}"`;
244-
return ['java', process.env['JAVA_OPTS'], subCmd, 'generate', appendix]
245+
246+
return [javaCmd, process.env['JAVA_OPTS'], subCmd, 'generate', appendix]
245247
.filter(isString)
246248
.join(' ');
247249
};

apps/generator-cli/src/app/services/pass-through.service.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Test } from '@nestjs/testing';
22
import chalk from 'chalk';
33
import { Command, createCommand } from 'commander';
44
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
5+
import { javaCmd } from '../helpers';
56
import { GeneratorService } from './generator.service';
67
import { PassThroughService } from './pass-through.service';
78
import { VersionManagerService } from './version-manager.service';
@@ -192,7 +193,7 @@ describe('PassThroughService', () => {
192193
await program.parseAsync([name, ...argv], { from: 'user' });
193194
expect(childProcess.spawn).toHaveBeenNthCalledWith(
194195
1,
195-
'java -jar "/some/path/to/4.2.1.jar"',
196+
`${javaCmd} -jar "/some/path/to/4.2.1.jar"`,
196197
[name, ...argv],
197198
{
198199
stdio: 'inherit',
@@ -206,7 +207,7 @@ describe('PassThroughService', () => {
206207
await program.parseAsync([name, ...argv], { from: 'user' });
207208
expect(childProcess.spawn).toHaveBeenNthCalledWith(
208209
1,
209-
'java java-opt-1=1 -jar "/some/path/to/4.2.1.jar"',
210+
`${javaCmd} java-opt-1=1 -jar "/some/path/to/4.2.1.jar"`,
210211
[name, ...argv],
211212
{
212213
stdio: 'inherit',
@@ -224,7 +225,7 @@ describe('PassThroughService', () => {
224225

225226
expect(childProcess.spawn).toHaveBeenNthCalledWith(
226227
1,
227-
`java -cp "${[
228+
`${javaCmd} -cp "${[
228229
'/some/path/to/4.2.1.jar',
229230
'../some/custom.jar',
230231
].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`,
@@ -303,7 +304,7 @@ describe('PassThroughService', () => {
303304
it('spawns the correct process', () => {
304305
expect(childProcess.spawn).toHaveBeenNthCalledWith(
305306
1,
306-
'java -jar "/some/path/to/4.2.1.jar"',
307+
`${javaCmd} -jar "/some/path/to/4.2.1.jar"`,
307308
cmd.split(' '),
308309
{ stdio: 'inherit', shell: true }
309310
);

apps/generator-cli/src/app/services/pass-through.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { exec, spawn } from 'child_process';
44
import { Command } from 'commander';
55
import { isString, startsWith, trim } from 'lodash';
66
import { COMMANDER_PROGRAM, LOGGER } from '../constants';
7+
import { javaCmd } from '../helpers';
78
import { GeneratorService } from './generator.service';
89
import { VersionManagerService } from './version-manager.service';
910
import { ConfigService } from './config.service';
@@ -142,7 +143,7 @@ export class PassThroughService {
142143
)}" org.openapitools.codegen.OpenAPIGenerator`
143144
: `-jar "${cliPath}"`;
144145

145-
return ['java', process.env['JAVA_OPTS'], subCmd]
146+
return [javaCmd, process.env['JAVA_OPTS'], subCmd]
146147
.filter(isString)
147148
.join(' ');
148149
}

0 commit comments

Comments
 (0)