microsoft/aspire · error · InvalidOperationException

Resource ' ' has no Maven or Gradle build configured, so…

Error message

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.

What it means

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.

Solutions

  1. Pass the agent path explicitly: WithOtelAgent("path/to/otel-agent.jar")
  2. Call WithMavenBuild or WithGradleBuild so the output directory (target/ or build/) can be inferred
  3. Ensure the project uses a standard build tool so build-tool detection succeeds

Example fix

// before
var java = builder.AddJavaApp("app", dir).WithJarPath("app.jar").WithOtelAgent();
// after
var java = builder.AddJavaApp("app", dir).WithJarPath("app.jar").WithOtelAgent("./agent/opentelemetry-javaagent.jar");
Defensive patterns

Strategy: validation

Validate before calling

var hasBuild = resource is JavaAppResource app &&
    (app.HasAnnotationOfType<JavaBuildStepAnnotation>() || app.HasAnnotationOfType<JavaBuildToolAnnotation>() || app.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>());
if (!hasBuild)
{
    // pass an explicit agent path to WithOtelAgent
}

Type guard

var canInferAgentPath = resource.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>() || resource.HasAnnotationOfType<JavaBuildStepAnnotation>();

Try / catch

try { java.WithOtelAgent(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("agent location cannot be inferred"))
{
    java.WithOtelAgent("./agent/opentelemetry-javaagent.jar");
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/aa696ae57bed8698. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Java/JavaHostingExtensions.cs:1157

    {
        if (annotation.AgentPath is { } authored)
        {
            return authored;
        }

        JavaBuildTool tool;

        if (resource.TryGetLastAnnotation<JavaBuildStepAnnotation>(out var buildStep))
        {
            tool = buildStep.Tool;
        }
        else if (resource is JavaAppResource app && app.HasAnnotationOfType<JavaDetectedBuildToolAnnotation>())
        {
            tool = ResolveDetectedBuildTool(app).Tool;
        }
        else
        {
            throw new InvalidOperationException(
                $"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.");
        }

        var outputDirectory = tool is JavaBuildTool.Gradle ? "build" : "target";

        return Path.Combine(outputDirectory, "agent", "opentelemetry-javaagent.jar");
    }

    /// <summary>
    /// Runs the application with the OpenTelemetry Java agent so it exports traces, metrics, and logs to Aspire.
    /// </summary>
    /// <typeparam name="T">The Java application resource type.</typeparam>
    /// <param name="builder">The resource builder.</param>
    /// <param name="agentPath">The path to the <c>opentelemetry-javaagent.jar</c> file.</param>
    /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="builder"/> is <see langword="null"/>.</exception>
    /// <exception cref="ArgumentException"><paramref name="agentPath"/> is <see langword="null"/>, empty, or whitespace.</exception>

View on GitHub (pinned to 25830f84bd)