{"record":{"id":"21b57749a03d29f1","repo":"microsoft/aspire","slug":"methodname-cannot-be-used-when-the-application-is-already","errorCode":null,"errorMessage":"{methodName} cannot be used when the application is already configured to launch with {existing.Tool}. A Java application is launched by a single build tool.","messagePattern":"(.+?) cannot be used when the application is already configured to launch with (.+?)\\. A Java application is launched by a single build tool\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaHostingExtensions.cs","lineNumber":700,"sourceCode":"        this IResourceBuilder<T> builder,\n        JavaBuildTool tool,\n        string goalOrTask,\n        string[] args,\n        string methodName) where T : JavaAppResource\n    {\n        // A prebuilt JAR and a build-tool launch are mutually exclusive: the build tool decides what to\n        // 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","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaHostingExtensions.cs#L682-L718","documentation":"A Java application is launched by exactly one build tool. If WithBuildToolLaunch detects an existing JavaBuildToolAnnotation whose Tool differs from the requested tool, it throws to prevent two competing launch configurations. This guards against, e.g., configuring Gradle as the launcher after Maven was already set.","triggerScenarios":"Calling WithGradleTask on a resource that already has a Maven launch configured (or vice versa) via WithMavenGoal/WithGradleTask.","commonSituations":"Migrating a project from Maven to Gradle (or back) without removing the previous launch call; combining sample code from different sources that use different build tools.","solutions":["Remove the earlier WithMavenGoal or WithGradleTask call so only one launch tool remains","Change the new call to match the already-configured tool (e.g. use WithMavenGoal instead of WithGradleTask)","Inspect the resource annotations to find which tool was configured first and keep that one"],"exampleFix":"// before\nvar java = builder.AddJavaApp(\"app\", dir).WithMavenGoal(\"spring-boot:run\").WithGradleTask(\"bootRun\");\n// after\nvar java = builder.AddJavaApp(\"app\", dir).WithGradleTask(\"bootRun\");","handlingStrategy":"validation","validationCode":"if (builder.Resource.TryGetLastAnnotation<JavaBuildToolAnnotation>(out var t) && t.Tool != JavaBuildTool.Gradle)\n{\n    throw new InvalidOperationException(\"Launch already configured with a different build tool.\");\n}","typeGuard":"builder.Resource.TryGetLastAnnotation<JavaBuildToolAnnotation>(out var existingTool)","tryCatchPattern":"try { java.WithGradleTask(\"bootRun\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already configured to launch with\"))\n{\n    // keep only one launch tool\n}","preventionTips":["Decide on Maven or Gradle for the whole app host and stick to it","Search the codebase for all WithMavenGoal/WithGradleTask calls per resource","Migrate tools by removing the old call in the same change"],"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"}