{"record":{"id":"aa696ae57bed8698","repo":"microsoft/aspire","slug":"resource-resource-name-has-no-maven-or-gradle-build","errorCode":null,"errorMessage":"Resource '{resource.Name}' has no Maven or Gradle build configured, so the OpenTelemetry agent location cannot be inferred. Call WithMavenBuild or WithGradleBuild, or pass the agent path to WithOtelAgent.","messagePattern":"Resource '(.+?)' has no Maven or Gradle build configured, so the OpenTelemetry agent location cannot be inferred\\. Call WithMavenBuild or WithGradleBuild, or pass the agent path to WithOtelAgent\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Java/JavaHostingExtensions.cs","lineNumber":1157,"sourceCode":"    {\n        if (annotation.AgentPath is { } authored)\n        {\n            return authored;\n        }\n\n        JavaBuildTool tool;\n\n        if (resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var buildStep))\n        {\n            tool = buildStep.Tool;\n        }\n        else if (resource is JavaAppResource app && app.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>())\n        {\n            tool = ResolveDetectedBuildTool(app).Tool;\n        }\n        else\n        {\n            throw new InvalidOperationException(\n                $\"Resource '{resource.Name}' has no Maven or Gradle build configured, so the OpenTelemetry agent location cannot be inferred. \" +\n                $\"Call WithMavenBuild or WithGradleBuild, or pass the agent path to WithOtelAgent.\");\n        }\n\n        var outputDirectory = tool is JavaBuildTool.Gradle ? \"build\" : \"target\";\n\n        return Path.Combine(outputDirectory, \"agent\", \"opentelemetry-javaagent.jar\");\n    }\n\n    /// <summary>\n    /// Runs the application with the OpenTelemetry Java agent so it exports traces, metrics, and logs to Aspire.\n    /// </summary>\n    /// <typeparam name=\"T\">The Java application resource type.</typeparam>\n    /// <param name=\"builder\">The resource builder.</param>\n    /// <param name=\"agentPath\">The path to the <c>opentelemetry-javaagent.jar</c> file.</param>\n    /// <returns>A reference to the <see cref=\"IResourceBuilder{T}\"/>.</returns>\n    /// <exception cref=\"ArgumentNullException\"><paramref name=\"builder\"/> is <see langword=\"null\"/>.</exception>\n    /// <exception cref=\"ArgumentException\"><paramref name=\"agentPath\"/> is <see langword=\"null\"/>, empty, or whitespace.</exception>","sourceCodeStart":1139,"sourceCodeEnd":1175,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Java/JavaHostingExtensions.cs#L1139-L1175","documentation":"When WithOtelAgent is called without an explicit agent path, Aspire tries to infer the agent's location from the build tool's output directory (target for Maven, build for Gradle). If the resource has no Maven or Gradle build configured and no detected build tool, the path cannot be inferred, so ResolveOtelAgentPath throws.","triggerScenarios":"Calling WithOtelAgent() with no path argument on a JavaAppResource that has neither WithMavenBuild/WithGradleBuild nor a detected build tool annotation (e.g. a JAR-based resource).","commonSituations":"Using the jarPath AddJavaApp overload (no build tool) and then adding the OTel agent without a path; forgetting to call WithMavenBuild/WithGradleBuild before WithOtelAgent; a project layout where the build tool could not be detected.","solutions":["Pass the agent path explicitly: WithOtelAgent(\"path/to/otel-agent.jar\")","Call WithMavenBuild or WithGradleBuild so the output directory (target/ or build/) can be inferred","Ensure the project uses a standard build tool so build-tool detection succeeds"],"exampleFix":"// before\nvar java = builder.AddJavaApp(\"app\", dir).WithJarPath(\"app.jar\").WithOtelAgent();\n// after\nvar java = builder.AddJavaApp(\"app\", dir).WithJarPath(\"app.jar\").WithOtelAgent(\"./agent/opentelemetry-javaagent.jar\");","handlingStrategy":"validation","validationCode":"var hasBuild = resource is JavaAppResource app &&\n    (app.HasAnnotationOfType<JavaBuildStepAnnotation>() || app.HasAnnotationOfType<JavaBuildToolAnnotation>() || app.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>());\nif (!hasBuild)\n{\n    // pass an explicit agent path to WithOtelAgent\n}","typeGuard":"var canInferAgentPath = resource.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>() || resource.HasAnnotationOfType<JavaBuildStepAnnotation>();","tryCatchPattern":"try { java.WithOtelAgent(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"agent location cannot be inferred\"))\n{\n    java.WithOtelAgent(\"./agent/opentelemetry-javaagent.jar\");\n}","preventionTips":["Always pass an explicit agent path when the app is JAR-based","Call WithMavenBuild/WithGradleBuild before relying on agent-path inference","Verify the project layout is detectable (pom.xml or build.gradle present)"],"tags":["java","opentelemetry","path-resolution","build-tool"],"backgroundTag":"missing-required-config","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"}