raphiz / buildGradleApplication

A Nix builder function for packaging Gradle applications.
MIT License
22 stars 2 forks source link

The kotlin hello-world application from gradle init can't be built with this project. #22

Closed c2vi closed 1 day ago

c2vi commented 5 days ago

I am doing:

nix shell nixpkgs/2a34566b67bef34c551f204063faeecc444ae9da#jdk nixpkgs/2a34566b67bef34c551f204063faeecc444ae9da#gradle
cd $(mktemp -d)
gradle init # answering 2:application, 4:Kotlin, rest-is-default
gradle --refresh-dependencies --write-verification-metadata sha256 --write-locks dependencies
nix build --file build.nix --impure -L

The build.nix file:

let 
  buildGradleFlake = builtins.getFlake "github:raphiz/buildGradleApplication"; 
  pkgs = import (builtins.getFlake "nixpkgs/2a34566b67bef34c551f204063faeecc444ae9da") {overlays = [buildGradleFlake.overlays.default];}; 
in pkgs.buildGradleApplication { 
  pname = "tmp"; 
  src = ./.; 
  meta = {}; 
  version = "0.1.0"; 
  buildTask = "installDist";
}

EDIT: put nix expression into a file.

log ``` @nix { "action": "setPhase", "phase": "unpackPhase" } Running phase: unpackPhase unpacking source archive /nix/store/lhgy83c0rjg415brwbk8ki2n9lnp1sp7-tmp.sH3n22dcoA source root is tmp.sH3n22dcoA @nix { "action": "setPhase", "phase": "patchPhase" } Running phase: patchPhase @nix { "action": "setPhase", "phase": "updateAutotoolsGnuConfigScriptsPhase" } Running phase: updateAutotoolsGnuConfigScriptsPhase @nix { "action": "setPhase", "phase": "configurePhase" } Running phase: configurePhase no configure script, doing nothing @nix { "action": "setPhase", "phase": "buildPhase" } Running phase: buildPhase Using maven repository at: /nix/store/3m3lz9dgcvlsf43d7c54bybajg6gcp32-tmp-0.1.0-m2-repository Welcome to Gradle 8.6! Here are the highlights of this release: - Configurable encryption key for configuration cache - Build init improvements - Build authoring improvements For more details see https://docs.gradle.org/8.6/release-notes.html To honour the JVM settings for this build a single-use Daemon process will be forked. For more on this, please refer to https://docs.gradle.org/8.6/userguide/gradle_daemon.html#sec:disabling_the_daemon in the Gradle documentation. Daemon will be stopped at the end of the build Adding maven repository in settings 'tmp.sH3n22dcoA': /nix/store/3m3lz9dgcvlsf43d7c54bybajg6gcp32-tmp-0.1.0-m2-repository... Replace all Maven repositories in settings 'tmp.sH3n22dcoA' with /nix/store/3m3lz9dgcvlsf43d7c54bybajg6gcp32-tmp-0.1.0-m2-repository... > Task :app:checkKotlinGradlePluginConfigurationErrors > Task :app:compileKotlin FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileKotlin'. > Could not resolve all files for configuration ':app:compileClasspath'. > Could not resolve org.jetbrains.kotlin:kotlin-stdlib:1.9.20. Required by: project :app > No cached version of org.jetbrains.kotlin:kotlin-stdlib:1.9.20 available for offline mode. > Could not resolve com.google.guava:guava:32.1.3-jre. Required by: project :app > No cached version of com.google.guava:guava:32.1.3-jre available for offline mode. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 37s 2 actionable tasks: 2 executed [?12l[?25h ```
raphiz commented 2 days ago

Hi, thanks for reporting this issue. The main issue is that the generated project does not follow Rule #4: Centralized Repository. Sadly, the way the build fails is not very helpful.

diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index b4a8413..ad09a4a 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -13,11 +13,6 @@ plugins {
     application
 }

-repositories {
-    // Use Maven Central for resolving dependencies.
-    mavenCentral()
-}
-
 dependencies {
     // Use the Kotlin JUnit 5 integration.
     testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
diff --git a/settings.gradle.kts b/settings.gradle.kts
index 2bb974f..c933412 100644
--- a/settings.gradle.kts
+++ b/settings.gradle.kts
@@ -4,6 +4,21 @@
  * The settings file is used to specify which projects to include in your build.
  * For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.6/userguide/multi_project_builds.html in the Gradle documentation.
  */
+pluginManagement {
+    repositories {
+        gradlePluginPortal()
+    }
+}
+
+dependencyResolutionManagement {
+    repositories {
+        mavenCentral()
+    }
+
+    // Highly recommended, see https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration
+    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
+}
+
 plugins {
     // Apply the foojay-resolver plugin to allow automatic download of JDKs
     id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"

Once that's out of the way, there is another issue: The generated project consists of a root project and a sub project called app. Since the application plugin is applied in the sub project, the default path for installLocation cannot be found and must also be adapted:

let
  buildGradleFlake = builtins.getFlake "github:raphiz/buildGradleApplication";
  pkgs = import (builtins.getFlake "nixpkgs/2a34566b67bef34c551f204063faeecc444ae9da") {overlays = [buildGradleFlake.overlays.default];};
in
  pkgs.buildGradleApplication {
    pname = "tmp";
    src = ./.;
    meta = {};
    version = "0.1.0";
    buildTask = "installDist";
    installLocation = "app/build/install/*/"; # default is "build/install/*/"
  }

I adapted the error message for this problem in fbfac49.

Hope this helps

c2vi commented 1 day ago

Well thank you very much! It all works for me now!

One more thing though. When gradle init asks for a java version, the default is 21.

Enter target version of Java (min. 7) (default: 21):

So the jdk to buildGradleApplication needs to be overwritten with jdk21. Making this the nix expression:

let
  buildGradleFlake = builtins.getFlake "github:raphiz/buildGradleApplication";
  pkgs = import (builtins.getFlake "nixpkgs/2a34566b67bef34c551f204063faeecc444ae9da") {overlays = [buildGradleFlake.overlays.default];};
in
  pkgs.buildGradleApplication {
    pname = "tmp";
    src = ./.;
    meta = {};
    version = "0.1.0";
    buildTask = "installDist";
    installLocation = "app/build/install/*/"; # default is "build/install/*/"
    jdk = pkgs.jdk21;
  }