Skip to content

Commit

Permalink
fix(gradle): check if java is installed (#29572)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
throw an error when `./gradlew wrapper` or `java -version` failed to
run:
<img width="949" alt="Screenshot 2025-01-09 at 5 33 38 PM"
src="https://github.com/user-attachments/assets/f0b99adc-ff80-4962-8dfe-c1ad11944cac"
/>
<img width="933" alt="Screenshot 2025-01-09 at 5 32 20 PM"
src="https://github.com/user-attachments/assets/11d2286a-cc04-48a0-9839-b67cc93d5cfc"
/>

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #

(cherry picked from commit f9c306a)
  • Loading branch information
xiongemi authored and FrozenPandaz committed Jan 17, 2025
1 parent 7bc8269 commit d887446
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
13 changes: 5 additions & 8 deletions packages/gradle/src/utils/exec-gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function execGradleAsync(
args: ReadonlyArray<string>,
execOptions: ExecFileOptions = {}
): Promise<Buffer> {
return new Promise<Buffer>((res, rej) => {
return new Promise<Buffer>((res, rej: (stdout: Buffer) => void) => {
const cp = execFile(gradleBinaryPath, args, {
cwd: dirname(gradleBinaryPath),
shell: true,
Expand All @@ -36,18 +36,15 @@ export function execGradleAsync(
cp.stdout?.on('data', (data) => {
stdout += data;
});
cp.stderr?.on('data', (data) => {
stdout += data;
});

cp.on('exit', (code) => {
if (code === 0) {
res(stdout);
} else {
rej(
new Error(
`Executing Gradle with ${args.join(
' '
)} failed with code: ${code}. \nLogs: ${stdout}`
)
);
rej(stdout);
}
});
});
Expand Down
45 changes: 36 additions & 9 deletions packages/gradle/src/utils/get-project-report-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AggregateCreateNodesError, logger } from '@nx/devkit';
import { execGradleAsync } from './exec-gradle';
import { existsSync } from 'fs';
import { dirname, join } from 'path';
import { execSync } from 'child_process';

export const fileSeparator = process.platform.startsWith('win')
? 'file:///'
Expand Down Expand Up @@ -36,21 +37,47 @@ export async function getProjectReportLines(
projectReportBuffer = await execGradleAsync(gradlewFile, [
'projectReportAll',
]);
} catch (e) {
try {
projectReportBuffer = await execGradleAsync(gradlewFile, [
'projectReport',
]);
logger.warn(
`Could not run 'projectReportAll' task. Ran 'projectReport' instead. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks. ${e.message}`
} catch (e: Buffer | Error | any) {
if (e.toString()?.includes('ERROR: JAVA_HOME')) {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not find Java. Please install Java and try again: https://www.java.com/en/download/help/index_installing.html.\n\r${e.toString()}`
),
],
],
[]
);
} catch (e) {
} else if (e.toString()?.includes(`Task 'projectReportAll' not found`)) {
try {
projectReportBuffer = await execGradleAsync(gradlewFile, [
'projectReport',
]);
logger.warn(
`Could not run 'projectReportAll' task. Ran 'projectReport' instead. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.\n\r${e.toString()}`
);
} catch (e) {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not run 'projectReportAll' or 'projectReport' task. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks.\n\r${e.toString()}`
),
],
],
[]
);
}
} else {
throw new AggregateCreateNodesError(
[
[
gradlewFile,
new Error(
`Could not run 'projectReportAll' or 'projectReport' task. Please run 'nx generate @nx/gradle:init' to generate the necessary tasks. ${e.message}`
`Could not run 'projectReportAll' or 'projectReport' Gradle task. Please install Gradle and try again: https://gradle.org/install/.\r\n${e.toString()}`
),
],
],
Expand Down

0 comments on commit d887446

Please sign in to comment.