{"record":{"id":"17023a4353281f8c","repo":"microsoft/aspire","slug":"methodname-cannot-be-used-when-the-application-is-already-17023a","errorCode":null,"errorMessage":"{methodName} cannot be used when the application is already configured to build with {buildStep.Tool}. A Java application is built and launched by a single build tool.","messagePattern":"(.+?) cannot be used when the application is already configured to build with (.+?)\\. A Java application is built and launched by a single build tool\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaHostingExtensions.cs","lineNumber":707,"sourceCode":"        // run, so -jar would be ignored. CommunityToolkit rejects both combinations; this port only\n        // rejected the Gradle half, letting a Maven+JAR application silently drop its JAR.\n        if (builder.Resource.HasAnnotationOfType<JavaJarPathAnnotation>())\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when a JAR path has been specified. Use either the \" +\n                $\"{nameof(AddJavaApp)} overload that takes a jarPath, or {methodName}, not both.\");\n        }\n\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildToolAnnotation>(out var existing) && existing.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when the application is already configured to launch with \" +\n                $\"{existing.Tool}. A Java application is launched by a single build tool.\");\n        }\n\n        if (builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var buildStep) && buildStep.Tool != tool)\n        {\n            throw new InvalidOperationException(\n                $\"{methodName} cannot be used when the application is already configured to build with \" +\n                $\"{buildStep.Tool}. A Java application is built and launched by a single build tool.\");\n        }\n\n        builder.WithAnnotation(\n            new JavaBuildToolAnnotation(tool, args.Length > 0 ? [goalOrTask, .. args] : [goalOrTask]),\n            ResourceAnnotationMutationBehavior.Replace);\n\n        // The launch goal now compiles the application, so the build resource is redundant — unless\n        // something outside the launch goal needs the build's output before the application starts.\n        if (buildStep is not null\n            && builder.ApplicationBuilder.ExecutionContext.IsRunMode\n            && !RequiresBuildBeforeLaunch(builder))\n        {\n            RemoveRunBuildResource(builder, buildStep);\n        }\n\n        // Set the command in every execution context. Setting it only in run mode left publish emitting","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaHostingExtensions.cs#L689-L725","documentation":"The build step (WithMavenBuild/WithGradleBuild) and the launch configuration must use the same build tool, because one tool builds and launches the application. WithBuildToolLaunch throws when a JavaBuildStepAnnotation exists for a different tool than the one being configured for launch.","triggerScenarios":"Configuring a launch goal with Gradle (WithGradleTask) on a resource whose build step was declared with Maven (WithMavenBuild), or the reverse.","commonSituations":"Mixing WithMavenBuild with WithGradleTask after copy-pasting from different examples; renaming a project from Maven to Gradle and updating only half of the configuration.","solutions":["Align the build step and launch call to the same tool (e.g. replace WithMavenBuild with WithGradleBuild)","Remove the conflicting build-step call if a single tool should own build and launch","Review the resource's WithMavenBuild/WithGradleBuild and WithMavenGoal/WithGradleTask calls as a pair"],"exampleFix":"// before\nvar java = builder.AddJavaApp(\"app\", dir).WithMavenBuild(\"package\").WithGradleTask(\"bootRun\");\n// after\nvar java = builder.AddJavaApp(\"app\", dir).WithGradleBuild(\"build\").WithGradleTask(\"bootRun\");","handlingStrategy":"validation","validationCode":"var hasMavenBuild = builder.Resource.HasAnnotationOfType<JavaBuildStepAnnotation>();\n// ensure build-step tool matches the tool you pass to WithGradleTask/WithMavenGoal","typeGuard":"builder.Resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var step) && step.Tool == desiredTool","tryCatchPattern":"try { java.WithGradleTask(\"bootRun\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already configured to build with\"))\n{\n    // align build step and launch to the same tool\n}","preventionTips":["Configure build and launch calls together, adjacently in code","Use one build tool per resource for both WithMavenBuild/WithGradleBuild and launch calls","Review Java resource config as a pair (build + launch) in code review"],"tags":["java","aspire-hosting","build-tool","conflicting-configuration"],"backgroundTag":"conflicting-config-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"}