{"record":{"id":"00b7837739978947","repo":"microsoft/aspire","slug":"resource-builder-resource-name-is-already-configured-to","errorCode":null,"errorMessage":"Resource '{builder.Resource.Name}' is already configured to build with {existing.Tool}. Call either WithMavenBuild or WithGradleBuild, not both.","messagePattern":"Resource '(.+?)' is already configured to build with (.+?)\\. Call either WithMavenBuild or WithGradleBuild, not both\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaHostingExtensions.cs","lineNumber":810,"sourceCode":"\n        return builder.WithJavaBuildStep(\n            JavaBuildTool.Gradle,\n            buildResourceName: $\"{builder.Resource.Name}-gradle-build\",\n            buildArgs: args.Length > 0 ? args : [\"clean\", \"build\"]);\n    }\n\n    private static IResourceBuilder<T> WithJavaBuildStep<T>(\n        this IResourceBuilder<T> builder,\n        JavaBuildTool tool,\n        string buildResourceName,\n        string[] buildArgs)\n        where T : JavaAppResource\n    {\n        // Building with both tools would produce two artifacts and leave the container build with no way\n        // to choose between them, so it is rejected the same way conflicting launch modes are.\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var existing) && existing.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"Resource '{builder.Resource.Name}' is already configured to build with {existing.Tool}. \" +\n                $\"Call either WithMavenBuild or WithGradleBuild, not both.\");\n        }\n\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildToolAnnotation>(out var launchTool) && launchTool.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"Resource '{builder.Resource.Name}' is already configured to launch with {launchTool.Tool}. \" +\n                \"A Java application is built and launched by a single build tool.\");\n        }\n\n        // A launch goal such as spring-boot:run or bootRun compiles the application on its way to running\n        // it, so a build resource in front of it would only repeat work. That holds only while the launch\n        // goal is what actually starts the application and nothing else needs the build's output first.\n        var createRunResource = builder.ApplicationBuilder.ExecutionContext.IsRunMode\n            && (RequiresBuildBeforeLaunch(builder)\n                || launchTool is null && !builder.Resource.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>());\n","sourceCodeStart":792,"sourceCodeEnd":828,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaHostingExtensions.cs#L792-L828","documentation":"A Java resource can be built by only one tool; building with both would produce two artifacts and the container build would have no way to pick between them. WithJavaBuildStep (backing WithMavenBuild/WithGradleBuild) throws when a JavaBuildStepAnnotation for a different tool already exists.","triggerScenarios":"Calling WithGradleBuild on a resource that already called WithMavenBuild (or vice versa).","commonSituations":"Trying to support both Maven and Gradle users by configuring both; switching build systems without removing the old WithMavenBuild/WithGradleBuild call.","solutions":["Remove the earlier WithMavenBuild or WithGradleBuild call so only one build tool is configured","Keep only the call matching the project's actual build files (pom.xml vs build.gradle)","Use WithMavenBuild if the project is Maven-based, WithGradleBuild if Gradle-based — not both"],"exampleFix":"// before\nvar java = builder.AddJavaApp(\"app\", dir).WithMavenBuild(\"package\").WithGradleBuild(\"build\");\n// after\nvar java = builder.AddJavaApp(\"app\", dir).WithMavenBuild(\"package\");","handlingStrategy":"validation","validationCode":"if (builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var existing))\n{\n    throw new InvalidOperationException(\"Build step already configured; do not call WithMavenBuild and WithGradleBuild together.\");\n}","typeGuard":"builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var existingBuildStep)","tryCatchPattern":"try { java.WithGradleBuild(\"build\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already configured to build with\"))\n{\n    // remove the earlier WithMavenBuild/WithGradleBuild call\n}","preventionTips":["Pick the build tool matching the repo's pom.xml/build.gradle presence","Do not add both build calls 'for flexibility' — the model rejects it","Remove the old build call when migrating between Maven and Gradle"],"tags":["java","aspire-hosting","build-tool","mutually-exclusive"],"backgroundTag":"mutually-exclusive-options","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}