Skip to content

Commit

Permalink
feat(core): add gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jan 8, 2025
1 parent 71653dc commit 659bafb
Show file tree
Hide file tree
Showing 49 changed files with 3,147 additions and 1,135 deletions.
2 changes: 0 additions & 2 deletions e2e/gradle/gradle/libs.versions.toml

This file was deleted.

Binary file removed e2e/gradle/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
27 changes: 24 additions & 3 deletions e2e/gradle/src/gradle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
runCLI,
uniq,
updateFile,
updateJson,
} from '@nx/e2e/utils';

import { createGradleProject } from './utils/create-gradle-project';
Expand All @@ -30,9 +31,9 @@ describe('Gradle', () => {
expect(projects).toContain(gradleProjectName);

const buildOutput = runCLI('build app', { verbose: true });
expect(buildOutput).toContain('nx run list:build');
expect(buildOutput).toContain('nx run list:');
expect(buildOutput).toContain(':list:classes');
expect(buildOutput).toContain('nx run utilities:build');
expect(buildOutput).toContain('nx run utilities:');
expect(buildOutput).toContain(':utilities:classes');

checkFilesExist(
Expand Down Expand Up @@ -82,8 +83,28 @@ dependencies {

let buildOutput = runCLI('build app2', { verbose: true });
// app2 depends on app
expect(buildOutput).toContain('nx run app:build');
expect(buildOutput).toContain('nx run app:');
expect(buildOutput).toContain(':app:classes');
expect(buildOutput).toContain('nx run list:');
expect(buildOutput).toContain(':list:classes');
expect(buildOutput).toContain('nx run utilities:');
expect(buildOutput).toContain(':utilities:classes');

checkFilesExist(`app2/build/libs/app2.jar`);
});

it('should run atomized test target', () => {
updateJson('nx.json', (json) => {
json.plugins.find((p) => p.plugin === '@nx/gradle').options[
'ciTargetName'
] = 'test-ci';
return json;
});

expect(() => {
runCLI('run app:test-ci--MessageUtilsTest', { verbose: true });
runCLI('run list:test-ci--LinkedListTest', { verbose: true });
}).not.toThrow();
});
}
);
Expand Down
46 changes: 38 additions & 8 deletions e2e/gradle/src/utils/create-gradle-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
tmpProjPath,
} from '@nx/e2e/utils';
import { execSync } from 'child_process';
import { readFileSync } from 'fs';
import { createFileSync, writeFileSync } from 'fs-extra';
import { join, resolve } from 'path';

Expand All @@ -15,14 +16,10 @@ export function createGradleProject(
packageName: string = 'gradleProject',
addProjectJsonNamePrefix: string = ''
) {
e2eConsoleLogger(
`Using java version: ${execSync('java -version')} ${execSync(
'echo $JAVA_HOME'
)}`
);
e2eConsoleLogger(`Using java version: ${execSync('java -version')}`);
const gradleCommand = isWindows()
? resolve(`${__dirname}/../../gradlew.bat`)
: resolve(`${__dirname}/../../gradlew`);
? resolve(`${__dirname}/../../../../packages/gradle/native/gradlew.bat`)
: resolve(`${__dirname}/../../../../packages/gradle/native/gradlew`);
e2eConsoleLogger(
'Using gradle version: ' +
execSync(`${gradleCommand} --version`, {
Expand All @@ -36,7 +33,7 @@ export function createGradleProject(
);
e2eConsoleLogger(
runCommand(
`${gradleCommand} init --type ${type}-application --dsl ${type} --project-name ${projectName} --package ${packageName} --no-incubating --split-project`,
`${gradleCommand} init --type ${type}-application --dsl ${type} --project-name ${projectName} --package ${packageName} --no-incubating --split-project --overwrite`,
{
cwd,
}
Expand All @@ -60,4 +57,37 @@ export function createGradleProject(
`{"name": "${addProjectJsonNamePrefix}utilities"}`
);
}

addLocalPluginManagement(
join(cwd, `settings.gradle${type === 'kotlin' ? '.kts' : ''}`)
);
addLocalPluginManagement(
join(cwd, `buildSrc/settings.gradle${type === 'kotlin' ? '.kts' : ''}`)
);
// Disable configuration cache to avoid issues with the createNodes task
writeFileSync(
join(cwd, `gradle.properties`),
'org.gradle.configuration-cache=false'
);

e2eConsoleLogger(
execSync(`${gradleCommand} publishToMavenLocal`, {
cwd: `${__dirname}/../../../../packages/gradle/native`,
}).toString()
);
}

function addLocalPluginManagement(filePath: string) {
let content = readFileSync(filePath).toString();
content =
`pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
mavenCentral()
// Add other repositories if needed
}
}
` + content;
writeFileSync(filePath, content);
}
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/gradle/native/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
7 changes: 7 additions & 0 deletions packages/gradle/native/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties

org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=false
version=0.0.1
11 changes: 11 additions & 0 deletions packages/gradle/native/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[plugins]
jvm = { id = "org.jetbrains.kotlin.jvm", version = "1.9.20" }

[versions]
kotlin-gradle-plugin = "2.0.21"

[libraries]
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin-gradle-plugin" }
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
validateDistributionUrl=false
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 4 additions & 2 deletions e2e/gradle/gradlew → packages/gradle/native/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,7 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
22 changes: 12 additions & 10 deletions e2e/gradle/gradlew.bat → packages/gradle/native/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
33 changes: 33 additions & 0 deletions packages/gradle/native/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Gradle plugin project to get you started.
* For more details on writing Custom Plugins, please refer to https://docs.gradle.org/8.5/userguide/custom_plugins.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/

plugins {
// Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
`java-gradle-plugin`
`maven-publish`

// Apply the Kotlin JVM plugin to add support for Kotlin.
alias(libs.plugins.jvm)
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

gradlePlugin {
// Define the plugin
val Nodes by plugins.creating {
id = "io.nx.gradle.plugin.Nodes"
implementationClass = "io.nx.gradle.plugin.Nodes"
}
}

dependencies {
implementation("com.google.code.gson:gson:2.11.0")
}
Loading

0 comments on commit 659bafb

Please sign in to comment.