Skip to content

Commit

Permalink
fix(gradle): read tasks from properties report (#29124)
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 -->

Some gradle tasks are not real tasks. They exist in `tasks.txt` because it is derived from subprojects should not be a real target for that project.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Gradle projects only have real gradle tasks which are not derived from their subproject tasks. 

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

Fixes #
  • Loading branch information
xiongemi authored Dec 5, 2024
1 parent 7328a8d commit 37adb48
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 44 deletions.
12 changes: 1 addition & 11 deletions e2e/gradle/src/gradle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ describe('Gradle', () => {
expect(projects).toContain('utilities');
expect(projects).toContain(gradleProjectName);

let buildOutput = runCLI('build app', { verbose: true });
// app depends on list and utilities
const buildOutput = runCLI('build app', { verbose: true });
expect(buildOutput).toContain('nx run list:build');
expect(buildOutput).toContain(':list:classes');
expect(buildOutput).toContain('nx run utilities:build');
Expand All @@ -41,15 +40,6 @@ describe('Gradle', () => {
`list/build/libs/list.jar`,
`utilities/build/libs/utilities.jar`
);

buildOutput = runCLI(`build ${gradleProjectName}`, { verbose: true });
// root project depends on app, list and utilities
expect(buildOutput).toContain('nx run app:build');
expect(buildOutput).toContain(':app:classes');
expect(buildOutput).toContain('nx run list:build');
expect(buildOutput).toContain(':list:classes');
expect(buildOutput).toContain('nx run utilities:build');
expect(buildOutput).toContain(':utilities:classes');
});

it('should track dependencies for new app', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ export function TargetConfigurationGroupList({
if (hasGroups) {
return (
<>
{Object.entries(targetsGroup.groups).map(
([targetGroupName, targets]) => {
{Object.entries(targetsGroup.groups)
.sort(([targetGroupName1], [targetGroupName2]) =>
targetGroupName1.localeCompare(targetGroupName2)
)
.map(([targetGroupName, targets]) => {
return (
<TargetConfigurationGroupContainer
targetGroupName={targetGroupName}
Expand All @@ -71,8 +74,7 @@ export function TargetConfigurationGroupList({
</ul>
</TargetConfigurationGroupContainer>
);
}
)}
})}
<TargetConfigurationGroupContainer
targetGroupName="Others"
targetsNumber={targetsGroup.targets.length}
Expand Down
6 changes: 6 additions & 0 deletions packages/gradle/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"cli": "nx",
"description": "This function changes !{projectRoot}/test/**/* in nx.json for production to !{projectRoot}/src/test/**/*",
"factory": "./src/migrations/19-4-1/change-regex-test-production"
},
"add-include-subprojects-tasks": {
"version": "20.2.0-beta.4",
"cli": "nx",
"description": "Add includeSubprojectsTasks to build.gradle file",
"factory": "./src/migrations/20-2-0/add-include-subprojects-tasks"
}
},
"packageJsonUpdates": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
main-branch-name: 'main'
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t build
workflows:
version: 2
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
- uses: nrwl/nx-set-shas@v4
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
- run: ./nx affected -t test build
- run: ./nx affected -t build
"
`;

Expand Down Expand Up @@ -109,7 +109,7 @@ jobs:
main-branch-name: 'main'
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build
- run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t build
workflows:
version: 2
Expand Down Expand Up @@ -160,6 +160,6 @@ jobs:
- uses: nrwl/nx-set-shas@v4
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
- run: ./nx affected -t test build
- run: ./nx affected -t build
"
`;
4 changes: 2 additions & 2 deletions packages/gradle/src/generators/ci-workflow/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getCiCommands(ci: Schema['ci']): Command[] {
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
},
{
command: `./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`,
command: `./nx affected --base=$NX_BASE --head=$NX_HEAD -t build`,
},
];
}
Expand All @@ -28,7 +28,7 @@ function getCiCommands(ci: Schema['ci']): Command[] {
{
comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`,
},
{ command: `./nx affected -t test build` },
{ command: `./nx affected -t build` },
];
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gradle/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('@nx/gradle:init', () => {
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"includeSubprojectsTasks": false,
"testTargetName": "test",
},
"plugin": "@nx/gradle",
Expand All @@ -48,6 +49,7 @@ describe('@nx/gradle:init', () => {
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"includeSubprojectsTasks": false,
"testTargetName": "test",
},
"plugin": "@nx/gradle",
Expand Down
1 change: 1 addition & 0 deletions packages/gradle/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function addPlugin(tree: Tree) {
testTargetName: 'test',
classesTargetName: 'classes',
buildTargetName: 'build',
includeSubprojectsTasks: false,
},
});
updateNxJson(tree, nxJson);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Tree, readNxJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import update from './add-include-subprojects-tasks';

describe('AddIncludeSubprojectsTasks', () => {
let tree: Tree;

beforeAll(() => {
tree = createTreeWithEmptyWorkspace();
});

it('should not change nx.json if @nx/gradle is not added', async () => {
tree.write('nx.json', JSON.stringify({ namedInputs: {} }));
update(tree);
expect(readNxJson(tree)).toMatchInlineSnapshot(`
{
"namedInputs": {},
}
`);
});

it('should add includeSubprojectsTasks to @nx/gradle plugin', async () => {
tree.write('nx.json', JSON.stringify({ plugins: ['@nx/gradle'] }));
update(tree);
expect(readNxJson(tree)).toMatchInlineSnapshot(`
{
"plugins": [
{
"options": {
"includeSubprojectsTasks": true,
},
"plugin": "@nx/gradle",
},
],
}
`);
});

it('should add includeSubprojectsTasks to @nx/gradle plugin with options', async () => {
tree.write(
'nx.json',
JSON.stringify({
plugins: [
{ plugin: '@nx/gradle', options: { testTargetName: 'test' } },
],
})
);
update(tree);
expect(readNxJson(tree)).toMatchInlineSnapshot(`
{
"plugins": [
{
"options": {
"includeSubprojectsTasks": true,
"testTargetName": "test",
},
"plugin": "@nx/gradle",
},
],
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Tree, readNxJson, updateNxJson } from '@nx/devkit';
import { hasGradlePlugin } from '../../utils/has-gradle-plugin';
import { GradlePluginOptions } from '../../plugin/nodes';

// This function add options includeSubprojectsTasks as true in nx.json for gradle plugin
export default function update(tree: Tree) {
const nxJson = readNxJson(tree);
if (!nxJson) {
return;
}
if (!hasGradlePlugin(tree)) {
return;
}
let gradlePluginIndex = nxJson.plugins.findIndex((p) =>
typeof p === 'string' ? p === '@nx/gradle' : p.plugin === '@nx/gradle'
);
let gradlePlugin = nxJson.plugins[gradlePluginIndex];
if (typeof gradlePlugin === 'string') {
gradlePlugin = {
plugin: '@nx/gradle',
options: {
includeSubprojectsTasks: true,
},
};
nxJson.plugins[gradlePluginIndex] = gradlePlugin;
} else {
gradlePlugin.options ??= {};
(gradlePlugin.options as GradlePluginOptions).includeSubprojectsTasks =
true;
}
updateNxJson(tree, nxJson);
}
Loading

0 comments on commit 37adb48

Please sign in to comment.