Repository: jhipster/jhipster-lite (java)

Issue:
Implement the option to add imports using the GradlePlugin API in the build.gradle.kts
The GradlePlugin API change:

```java
      .id("com.github.node-gradle.node")
      .pluginSlug("node-gradle")
      .versionSlug("node-gradle")
      .gradleImports()
        .import("com.github.gradle.node.npm.task.NpmTask")
        .and()
      .configuration(
        """
        node {
          version.set("v20.12.2")
          npmVersion.set("10.5.2")
          npmWorkDir.set(file("build"))
        }
        
        val buildTaskUsingNpm = tasks.register<NpmTask>("buildNpm") {
          description = "Build the frontend project using NPM"
          group = "Build"
          dependsOn("npmInstall")
          npmCommand.set(listOf("run", "build"))
          environment.set(mapOf("APP_VERSION" to project.version.toString()))
        }
        
        val testTaskUsingNpm = tasks.register<NpmTask>("testNpm") {
          description = "Test the frontend project using NPM"
          group = "verification"
          dependsOn("npmInstall", "buildNpm")
          npmCommand.set(listOf("run", "test"))
          ignoreExitValue.set(false)
          workingDir.set(projectDir)
          execOverrides {
            standardOutput = System.out
          }
        }
        
        tasks.bootJar {
          dependsOn("buildNpm")
          from("build/classes/static") {
              into("BOOT-INF/classes/static") // This will place the content under 'static' directory in the JAR
          }
        }
        """
      )
      .build();
```

**note:** the configuration is an example, wont be hard coded the versions and etc... the focus in this issue is the `gradleImports()`.

After enable the GradlePlugin API to handle imports, [here is the result](https://github.com/renanfranca/sample-frontend-gradle/pull/2/files#diff-c0dfa6bc7a8685217f70a860145fbdf416d449eaff052fa28352c5cec1a98c06R1-R2) in the `build.gradle.kts`.

Pull request:
add imports option at gradle plugin and gradle profile plugin API
Fix #9788

Patch:
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/BuildGradleImport.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/BuildGradleImport.java
new file mode 100644
index 000000000..ca10f9c3c
--- /dev/null
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/BuildGradleImport.java
@@ -0,0 +1,13 @@
+package tech.jhipster.lite.module.domain.gradleplugin;
+
+import tech.jhipster.lite.shared.error.domain.Assert;
+
+public record BuildGradleImport(String gradleImport) {
+  public BuildGradleImport {
+    Assert.notBlank("gradleImport", gradleImport);
+  }
+
+  public String get() {
+    return gradleImport();
+  }
+}
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityPlugin.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityPlugin.java
index a2a5e1c08..3492e4487 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityPlugin.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityPlugin.java
@@ -1,5 +1,7 @@
 package tech.jhipster.lite.module.domain.gradleplugin;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Optional;
 import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
 import tech.jhipster.lite.shared.error.domain.Assert;
@@ -7,6 +9,7 @@ import tech.jhipster.lite.shared.error.domain.Assert;
 public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
 
   private final GradlePluginId id;
+  private final GradlePluginImports imports;
   private final Optional<GradlePluginConfiguration> configuration;
   private final Optional<VersionSlug> versionSlug;
   private final Optional<GradlePluginSlug> pluginSlug;
@@ -14,6 +17,7 @@ public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
   private GradleCommunityPlugin(GradleCommunityPluginBuilder builder) {
     Assert.notNull("id", builder.id);
     id = builder.id;
+    imports = new GradlePluginImports(builder.imports);
     configuration = Optional.ofNullable(builder.configuration);
     versionSlug = Optional.ofNullable(builder.versionSlug);
     pluginSlug = Optional.ofNullable(builder.pluginSlug);
@@ -24,6 +28,11 @@ public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
     return id;
   }
 
+  @Override
+  public GradlePluginImports imports() {
+    return imports;
+  }
+
   @Override
   public Optional<GradlePluginConfiguration> configuration() {
     return configuration;
@@ -44,6 +53,7 @@ public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
   private static final class GradleCommunityPluginBuilder implements GradleCommunityPluginIdBuilder, GradleCommunityPluginOptionalBuilder {
 
     private GradlePluginId id;
+    private final Collection<BuildGradleImport> imports = new ArrayList<>();
     private GradlePluginConfiguration configuration;
     private VersionSlug versionSlug;
     private GradlePluginSlug pluginSlug;
@@ -55,6 +65,13 @@ public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
       return this;
     }
 
+    @Override
+    public GradleCommunityPluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport) {
+      imports.add(gradleImport);
+
+      return this;
+    }
+
     @Override
     public GradleCommunityPluginOptionalBuilder configuration(GradlePluginConfiguration configuration) {
       this.configuration = configuration;
@@ -91,6 +108,12 @@ public final class GradleCommunityPlugin implements GradleMainBuildPlugin {
   }
 
   public interface GradleCommunityPluginOptionalBuilder {
+    GradleCommunityPluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport);
+
+    default GradleCommunityPluginOptionalBuilder withBuildGradleImport(String gradleImport) {
+      return withBuildGradleImport(new BuildGradleImport(gradleImport));
+    }
+
     GradleCommunityPluginOptionalBuilder configuration(GradlePluginConfiguration configuration);
 
     default GradleCommunityPluginOptionalBuilder configuration(String configuration) {
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityProfilePlugin.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityProfilePlugin.java
index 16badaafe..96beaf602 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityProfilePlugin.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCommunityProfilePlugin.java
@@ -1,5 +1,7 @@
 package tech.jhipster.lite.module.domain.gradleplugin;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Optional;
 import tech.jhipster.lite.module.domain.javabuild.ArtifactId;
 import tech.jhipster.lite.module.domain.javabuild.GroupId;
@@ -10,6 +12,7 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
 
   private final GradlePluginId id;
   private final GradlePluginDependency dependency;
+  private final GradlePluginImports imports;
   private final Optional<GradlePluginConfiguration> configuration;
   private final Optional<VersionSlug> versionSlug;
 
@@ -19,6 +22,7 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
 
     id = builder.id;
     this.dependency = builder.dependency;
+    imports = new GradlePluginImports(builder.imports);
     this.configuration = Optional.ofNullable(builder.configuration);
     this.versionSlug = Optional.ofNullable(builder.versionSlug);
   }
@@ -28,6 +32,11 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
     return id;
   }
 
+  @Override
+  public GradlePluginImports imports() {
+    return imports;
+  }
+
   @Override
   public Optional<GradlePluginConfiguration> configuration() {
     return configuration;
@@ -51,6 +60,7 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
 
     private GradlePluginId id;
     private GradlePluginDependency dependency;
+    private final Collection<BuildGradleImport> imports = new ArrayList<>();
     private GradlePluginConfiguration configuration;
     private VersionSlug versionSlug;
 
@@ -68,6 +78,13 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
       return this;
     }
 
+    @Override
+    public GradleCommunityProfilePluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport) {
+      imports.add(gradleImport);
+
+      return this;
+    }
+
     @Override
     public GradleCommunityProfilePluginOptionalBuilder configuration(GradlePluginConfiguration configuration) {
       this.configuration = configuration;
@@ -105,6 +122,12 @@ public final class GradleCommunityProfilePlugin implements GradleProfilePlugin {
   }
 
   public interface GradleCommunityProfilePluginOptionalBuilder {
+    GradleCommunityProfilePluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport);
+
+    default GradleCommunityProfilePluginOptionalBuilder withBuildGradleImport(String gradleImport) {
+      return withBuildGradleImport(new BuildGradleImport(gradleImport));
+    }
+
     GradleCommunityProfilePluginOptionalBuilder configuration(GradlePluginConfiguration configuration);
 
     default GradleCommunityProfilePluginOptionalBuilder configuration(String configuration) {
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCorePlugin.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCorePlugin.java
index 400392844..534a1f3c5 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCorePlugin.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradleCorePlugin.java
@@ -1,5 +1,7 @@
 package tech.jhipster.lite.module.domain.gradleplugin;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Optional;
 import tech.jhipster.lite.module.domain.javabuild.VersionSlug;
 import tech.jhipster.lite.shared.error.domain.Assert;
@@ -7,12 +9,14 @@ import tech.jhipster.lite.shared.error.domain.Assert;
 public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProfilePlugin {
 
   private final GradlePluginId id;
+  private final GradlePluginImports imports;
   private final Optional<GradlePluginConfiguration> configuration;
   private final Optional<VersionSlug> toolVersionSlug;
 
   private GradleCorePlugin(GradleCorePluginBuilder builder) {
     Assert.notNull("id", builder.id);
     id = builder.id;
+    imports = new GradlePluginImports(builder.imports);
     configuration = Optional.ofNullable(builder.configuration);
     toolVersionSlug = Optional.ofNullable(builder.toolVersionSlug);
   }
@@ -21,6 +25,11 @@ public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProf
     return id;
   }
 
+  @Override
+  public GradlePluginImports imports() {
+    return imports;
+  }
+
   @Override
   public Optional<GradlePluginConfiguration> configuration() {
     return configuration;
@@ -37,6 +46,7 @@ public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProf
   private static final class GradleCorePluginBuilder implements GradleCorePluginIdBuilder, GradleCorePluginOptionalBuilder {
 
     private GradlePluginId id;
+    private final Collection<BuildGradleImport> imports = new ArrayList<>();
     private GradlePluginConfiguration configuration;
     private VersionSlug toolVersionSlug;
 
@@ -47,6 +57,13 @@ public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProf
       return this;
     }
 
+    @Override
+    public GradleCorePluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport) {
+      imports.add(gradleImport);
+
+      return this;
+    }
+
     @Override
     public GradleCorePluginOptionalBuilder configuration(GradlePluginConfiguration configuration) {
       this.configuration = configuration;
@@ -76,6 +93,12 @@ public final class GradleCorePlugin implements GradleMainBuildPlugin, GradleProf
   }
 
   public interface GradleCorePluginOptionalBuilder {
+    GradleCorePluginOptionalBuilder withBuildGradleImport(BuildGradleImport gradleImport);
+
+    default GradleCorePluginOptionalBuilder withBuildGradleImport(String gradleImport) {
+      return withBuildGradleImport(new BuildGradleImport(gradleImport));
+    }
+
     GradleCorePluginOptionalBuilder configuration(GradlePluginConfiguration configuration);
 
     default GradleCorePluginOptionalBuilder configuration(String configuration) {
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePlugin.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePlugin.java
index a17626b00..6b0a7f621 100644
--- a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePlugin.java
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePlugin.java
@@ -4,5 +4,6 @@ import java.util.Optional;
 
 public sealed interface GradlePlugin permits GradleMainBuildPlugin, GradleProfilePlugin {
   GradlePluginId id();
+  GradlePluginImports imports();
   Optional<GradlePluginConfiguration> configuration();
 }
diff --git a/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePluginImports.java b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePluginImports.java
new file mode 100644
index 000000000..a57ad4134
--- /dev/null
+++ b/src/main/java/tech/jhipster/lite/module/domain/gradleplugin/GradlePluginImports.java
@@ -0,0 +1,17 @@
+package tech.jhipster.lite.module.domain.gradleplugin;
+
+import java.util.Collection;
+import java.util.function.Consumer;
+import tech.jhipster.lite.shared.error.domain.Assert;
+
+public record GradlePluginImports(Collection<BuildGradleImport> pluginImports) {
+  public GradlePluginImports {
+    Assert.field("pluginImports", pluginImports).notNull().noNullElement();
+  }
+
+  public void forEach(Consumer<BuildGradleImport> consumer) {
+    Assert.notNull("consumer", consumer);
+
+    pluginImports.forEach(consumer);
+  }
+}
diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java
index 6d6b7e2bc..a781c8102 100644
--- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java
+++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandler.java
@@ -22,10 +22,7 @@ import tech.jhipster.lite.module.domain.ProjectFiles;
 import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
 import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
 import tech.jhipster.lite.module.domain.file.*;
-import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityPlugin;
-import tech.jhipster.lite.module.domain.gradleplugin.GradleCommunityProfilePlugin;
-import tech.jhipster.lite.module.domain.gradleplugin.GradleCorePlugin;
-import tech.jhipster.lite.module.domain.gradleplugin.GradlePluginConfiguration;
+import tech.jhipster.lite.module.domain.gradleplugin.*;
 import tech.jhipster.lite.module.domain.javabuild.DependencySlug;
 import tech.jhipster.lite.module.domain.javabuild.command.*;
 import tech.jhipster.lite.module.domain.javabuildprofile.BuildProfileActivation;
@@ -46,6 +43,7 @@ public class GradleCommandHandler implements JavaDependenciesCommandHandler {
   private static final String COMMAND = "command";
   private static final String BUILD_GRADLE_FILE = "build.gradle.kts";
   private static final String PLUGIN_BUILD_GRADLE_FILE = "buildSrc/build.gradle.kts";
+  private static final Pattern GRADLE_IMPORT_NEEDLE = Pattern.compile("^// jhipster-needle-gradle-imports$", Pattern.MULTILINE);
   private static final Pattern GRADLE_PLUGIN_NEEDLE = Pattern.compile("^\\s+// jhipster-needle-gradle-plugins$", Pattern.MULTILINE);
   private static final Pattern GRADLE_PLUGIN_PROJECT_EXTENSION_CONFIGURATION_NEEDLE = Pattern.compile(
     "^// jhipster-needle-gradle-plugins-configurations$",
@@ -461,6 +459,7 @@ public class GradleCommandHandler implements JavaDependenciesCommandHandler {
         addDependencyToBuildGradle(dependencyFrom(plugin), projectFolder.filePath(PLUGIN_BUILD_GRADLE_FILE), false);
       }
     }
+    command.plugin().imports().forEach(pluginImport -> addPluginImport(pluginImport, command.buildProfile()));
     command.plugin().configuration().ifPresent(pluginConfiguration -> addPluginConfiguration(pluginConfiguration, command.buildProfile()));
     command.toolVersion().ifPresent(version -> handle(new SetVersion(version)));
     command.pluginVersion().ifPresent(version -> handle(new SetVersion(version)));
@@ -481,6 +480,21 @@ public class GradleCommandHandler implements JavaDependenciesCommandHandler {
     );
   }
 
+  private void addPluginImport(BuildGradleImport gradleImport, Optional<BuildProfileId> buildProfile) {
+    MandatoryReplacer replacer = new MandatoryReplacer(
+      new RegexNeedleBeforeReplacer(
+        (contentBeforeReplacement, newText) -> !contentBeforeReplacement.contains(newText),
+        GRADLE_IMPORT_NEEDLE
+      ),
+      "import %s".formatted(gradleImport.get())
+    );
+    fileReplacer.handle(
+      projectFolder,
+      ContentReplacers.of(new MandatoryFileReplacer(projectFolderRelativePathFrom(buildGradleFile(buildProfile)), replacer)),
+      context
+    );
+  }
+
   private void addPluginConfiguration(GradlePluginConfiguration pluginConfiguration, Optional<BuildProfileId> buildProfile) {
     MandatoryReplacer replacer = new MandatoryReplacer(
       new RegexNeedleBeforeReplacer(
diff --git a/src/main/resources/generator/buildtool/gradle/build.gradle.kts.mustache b/src/main/resources/generator/buildtool/gradle/build.gradle.kts.mustache
index efd159c78..706dc6152 100644
--- a/src/main/resources/generator/buildtool/gradle/build.gradle.kts.mustache
+++ b/src/main/resources/generator/buildtool/gradle/build.gradle.kts.mustache
@@ -1,3 +1,5 @@
+// jhipster-needle-gradle-imports
+
 plugins {
   java
   // jhipster-needle-gradle-plugins
diff --git a/src/main/resources/generator/buildtool/gradle/buildSrc/src/main/kotlin/profile.gradle.kts.template b/src/main/resources/generator/buildtool/gradle/buildSrc/src/main/kotlin/profile.gradle.kts.template
index f2a1fac1e..4f101c808 100644
--- a/src/main/resources/generator/buildtool/gradle/buildSrc/src/main/kotlin/profile.gradle.kts.template
+++ b/src/main/resources/generator/buildtool/gradle/buildSrc/src/main/kotlin/profile.gradle.kts.template
@@ -1,3 +1,5 @@
+// jhipster-needle-gradle-imports
+
 plugins {
   java
   // jhipster-needle-gradle-plugins


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java
index eaf19d1a9..ec6810639 100644
--- a/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java
+++ b/src/test/java/tech/jhipster/lite/module/domain/JHipsterModulesFixture.java
@@ -12,6 +12,7 @@ import tech.jhipster.lite.module.domain.buildproperties.BuildProperty;
 import tech.jhipster.lite.module.domain.buildproperties.PropertyKey;
 import tech.jhipster.lite.module.domain.buildproperties.PropertyValue;
 import tech.jhipster.lite.module.domain.gradleplugin.GradleMainBuildPlugin;
+import tech.jhipster.lite.module.domain.gradleplugin.GradlePlugin;
 import tech.jhipster.lite.module.domain.gradleplugin.GradleProfilePlugin;
 import tech.jhipster.lite.module.domain.javabuild.ArtifactId;
 import tech.jhipster.lite.module.domain.javabuild.GroupId;
@@ -437,6 +438,7 @@ public final class JHipsterModulesFixture {
     return gradleCorePlugin()
       .id("checkstyle")
       .toolVersionSlug("checkstyle")
+      .withBuildGradleImport("java.util.Properties")
       .configuration(
         """
         checkstyle {
@@ -451,6 +453,7 @@ public final class JHipsterModulesFixture {
     return gradleCorePlugin()
       .id("checkstyle")
       .toolVersionSlug("checkstyle")
+      .withBuildGradleImport("java.util.Properties")
       .configuration(
         """
         checkstyle {
@@ -461,6 +464,51 @@ public final class JHipsterModulesFixture {
       .build();
   }
 
+  public static GradlePlugin nodeGradlePlugin() {
+    return gradleCommunityPlugin()
+      .id("com.github.node-gradle.node")
+      .pluginSlug("node-gradle")
+      .versionSlug("node-gradle")
+      .withBuildGradleImport("com.github.gradle.node.npm.task.NpmTask")
+      .configuration(
+        """
+        node {
+          version.set("v20.12.2")
+          npmVersion.set("10.5.2")
+          npmWorkDir.set(file("build"))
+        }
+
+        val buildTaskUsingNpm = tasks.register<NpmTask>("buildNpm") {
+          description = "Build the frontend project using NPM"
+          group = "Build"
+          dependsOn("npmInstall")
+          npmCommand.set(listOf("run", "build"))
+          environment.set(mapOf("APP_VERSION" to project.version.toString()))
+        }
+
+        val testTaskUsingNpm = tasks.register<NpmTask>("testNpm") {
+          description = "Test the frontend project using NPM"
+          group = "verification"
+          dependsOn("npmInstall", "buildNpm")
+          npmCommand.set(listOf("run", "test"))
+          ignoreExitValue.set(false)
+          workingDir.set(projectDir)
+          execOverrides {
+            standardOutput = System.out
+          }
+        }
+
+        tasks.bootJar {
+          dependsOn("buildNpm")
+          from("build/classes/static") {
+              into("BOOT-INF/classes/static")
+          }
+        }
+        """
+      )
+      .build();
+  }
+
   public static GradleProfilePlugin gitPropertiesGradleProfilePlugin() {
     return gradleProfilePlugin()
       .id("com.gorylenko.gradle-git-properties")
@@ -482,6 +530,7 @@ public final class JHipsterModulesFixture {
       .id("com.bmuschko.docker-remote-api")
       .dependency(groupId("com.bmuschko"), artifactId("gradle-docker-plugin"))
       .versionSlug("docker-plugin")
+      .withBuildGradleImport("java.util.Properties")
       .build();
   }
 
diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java
index 307bc71f3..50877ae99 100644
--- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java
+++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/FileSystemJHipsterModulesRepositoryTest.java
@@ -427,6 +427,12 @@ class FileSystemJHipsterModulesRepositoryTest {
       .containing("testImplementation(libs.junit.jupiter.engine)")
       .containing("implementation(libs.spring.boot.starter)")
       .notContaining("testImplementation(libs.assertj.core)")
+      .containing(
+        """
+        import java.util.Properties
+        // jhipster-needle-gradle-imports
+        """
+      )
       .containing("""
         plugins {
           java
@@ -487,6 +493,12 @@ class FileSystemJHipsterModulesRepositoryTest {
       )
       .and()
       .hasFile("buildSrc/src/main/kotlin/profile-local.gradle.kts")
+      .containing(
+        """
+        import java.util.Properties
+        // jhipster-needle-gradle-imports
+        """
+      )
       .containing(
         """
         plugins {
diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandlerTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandlerTest.java
index b2a03c804..b3a0cfc99 100644
--- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandlerTest.java
+++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/javadependency/gradle/GradleCommandHandlerTest.java
@@ -1104,12 +1104,18 @@ class GradleCommandHandlerTest {
     private final JHipsterProjectFolder projectFolder = projectFrom("src/test/resources/projects/empty-gradle");
 
     @Test
-    void shouldDeclareAndConfigurePluginInBuildGradleFile() {
+    void shouldDeclareAndConfigureCorePluginAndAddImportInBuildGradleFile() {
       new GradleCommandHandler(Indentation.DEFAULT, projectFolder, emptyModuleContext(), filesReader, templateRenderer).handle(
         AddGradlePlugin.builder().plugin(checkstyleGradlePlugin()).build()
       );
 
       assertThat(buildGradleContent(projectFolder))
+        .contains(
+          """
+          import java.util.Properties
+          // jhipster-needle-gradle-imports
+          """
+        )
         .contains(
           """
           plugins {
@@ -1131,6 +1137,66 @@ class GradleCommandHandlerTest {
         );
     }
 
+    @Test
+    void shouldDeclareAndConfigureCommunityPluginAndAddImportInBuildGradleFile() {
+      new GradleCommandHandler(Indentation.DEFAULT, projectFolder, emptyModuleContext(), filesReader, templateRenderer).handle(
+        AddGradlePlugin.builder().plugin(nodeGradlePlugin()).build()
+      );
+
+      assertThat(buildGradleContent(projectFolder))
+        .contains(
+          """
+          import com.github.gradle.node.npm.task.NpmTask
+          // jhipster-needle-gradle-imports
+          """
+        )
+        .contains(
+          """
+          plugins {
+            java
+            alias(libs.plugins.node.gradle)
+            // jhipster-needle-gradle-plugins
+          }
+          """
+        )
+        .contains(
+          """
+          node {
+            version.set("v20.12.2")
+            npmVersion.set("10.5.2")
+            npmWorkDir.set(file("build"))
+          }
+
+          val buildTaskUsingNpm = tasks.register<NpmTask>("buildNpm") {
+            description = "Build the frontend project using NPM"
+            group = "Build"
+            dependsOn("npmInstall")
+            npmCommand.set(listOf("run", "build"))
+            environment.set(mapOf("APP_VERSION" to project.version.toString()))
+          }
+
+          val testTaskUsingNpm = tasks.register<NpmTask>("testNpm") {
+            description = "Test the frontend project using NPM"
+            group = "verification"
+            dependsOn("npmInstall", "buildNpm")
+            npmCommand.set(listOf("run", "test"))
+            ignoreExitValue.set(false)
+            workingDir.set(projectDir)
+            execOverrides {
+              standardOutput = System.out
+            }
+          }
+
+          tasks.bootJar {
+            dependsOn("buildNpm")
+            from("build/classes/static") {
+                into("BOOT-INF/classes/static")
+            }
+          }
+          """
+        );
+    }
+
     @Test
     void shouldApplyVersionCatalogReferenceConvention() {
       GradlePlugin plugin = gradleCommunityPlugin().id("org.springframework.boot").pluginSlug("spring-boot").build();
@@ -1181,6 +1247,11 @@ class GradleCommandHandlerTest {
       gradleCommandHandler.handle(command);
 
       assertThat(buildGradleContent(projectFolder))
+        .containsOnlyOnce(
+          """
+          import java.util.Properties
+          """
+        )
         .contains(
           """
           plugins {
@@ -1269,19 +1340,26 @@ class GradleCommandHandlerTest {
         """
       );
       assertThat(pluginBuildGradleContent(projectFolder)).contains("implementation(libs.gradle.docker.plugin");
-      assertThat(scriptPluginContent(projectFolder, localBuildProfile())).contains(
-        """
-        plugins {
-          java
-          id("com.bmuschko.docker-remote-api")
-          // jhipster-needle-gradle-plugins
-        }
-        """
-      );
+      assertThat(scriptPluginContent(projectFolder, localBuildProfile()))
+        .contains(
+          """
+          import java.util.Properties
+          // jhipster-needle-gradle-imports
+          """
+        )
+        .contains(
+          """
+          plugins {
+            java
+            id("com.bmuschko.docker-remote-api")
+            // jhipster-needle-gradle-plugins
+          }
+          """
+        );
     }
 
     @Test
-    void shouldDeclareAndConfigureCorePluginInBuildGradleProfileFile() {
+    void shouldDeclareAndConfigureCorePluginAndAddImportInBuildGradleProfileFile() {
       JHipsterProjectFolder projectFolder = projectFrom("src/test/resources/projects/gradle-with-local-profile");
 
       new GradleCommandHandler(Indentation.DEFAULT, projectFolder, emptyModuleContext(), filesReader, templateRenderer).handle(
@@ -1289,6 +1367,12 @@ class GradleCommandHandlerTest {
       );
 
       assertThat(scriptPluginContent(projectFolder, localBuildProfile()))
+        .contains(
+          """
+          import java.util.Properties
+          // jhipster-needle-gradle-imports
+          """
+        )
         .contains(
           """
           plugins {
diff --git a/src/test/resources/projects/empty-gradle/build.gradle.kts b/src/test/resources/projects/empty-gradle/build.gradle.kts
index e5675f384..89cc51dc6 100644
--- a/src/test/resources/projects/empty-gradle/build.gradle.kts
+++ b/src/test/resources/projects/empty-gradle/build.gradle.kts
@@ -1,3 +1,5 @@
+// jhipster-needle-gradle-imports
+
 plugins {
   java
   // jhipster-needle-gradle-plugins
diff --git a/src/test/resources/projects/gradle-with-local-profile/buildSrc/src/main/kotlin/profile-local.gradle.kts b/src/test/resources/projects/gradle-with-local-profile/buildSrc/src/main/kotlin/profile-local.gradle.kts
index f2a1fac1e..4f101c808 100644
--- a/src/test/resources/projects/gradle-with-local-profile/buildSrc/src/main/kotlin/profile-local.gradle.kts
+++ b/src/test/resources/projects/gradle-with-local-profile/buildSrc/src/main/kotlin/profile-local.gradle.kts
@@ -1,3 +1,5 @@
+// jhipster-needle-gradle-imports
+
 plugins {
   java
   // jhipster-needle-gradle-plugins
diff --git a/src/test/resources/projects/init-gradle/build.gradle.kts b/src/test/resources/projects/init-gradle/build.gradle.kts
index 4f495c030..08b7d8907 100644
--- a/src/test/resources/projects/init-gradle/build.gradle.kts
+++ b/src/test/resources/projects/init-gradle/build.gradle.kts
@@ -1,3 +1,5 @@
+// jhipster-needle-gradle-imports
+
 plugins {
   java
   // jhipster-needle-gradle-plugins