spring-projects/spring-boot · error · IllegalStateException

Unable to compile generated source{}

Error message

Unable to compile generated source{}

What it means

Thrown by AbstractAotMojo.compileSourceFiles() after the system JavaCompiler fails (task.call() returns false) or the Errors DiagnosticListener collected ERROR diagnostics. The generated AOT source files could not be compiled, and the collected compiler messages are appended. execute() wraps the IllegalStateException into a MojoExecutionException, so the user sees a Maven build failure.

Source

Thrown at build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractAotMojo.java:180

				String source = compilerConfiguration.getSourceMajorVersion();
				if (source != null) {
					args.add("--source");
					args.add(source);
				}
				String target = compilerConfiguration.getTargetMajorVersion();
				if (target != null) {
					args.add("--target");
					args.add(target);
				}
			}
			args.add("-parameters");
			args.addAll(new RunArguments(this.compilerArguments).getArgs());
			Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromPaths(sourceFiles);
			Errors errors = new Errors();
			CompilationTask task = compiler.getTask(null, fileManager, errors, args, null, compilationUnits);
			boolean result = task.call();
			if (!result || errors.hasReportedErrors()) {
				throw new IllegalStateException("Unable to compile generated source" + errors);
			}
		}
	}

	protected final URL[] getClassPath(File[] directories, ArtifactsFilter... artifactFilters)
			throws MojoExecutionException {
		List<URL> urls = new ArrayList<>();
		Arrays.stream(directories).map(this::toURL).forEach(urls::add);
		urls.addAll(getDependencyURLs(artifactFilters));
		return urls.toArray(URL[]::new);
	}

	protected final void copyAll(Path from, Path to) throws IOException {
		if (!Files.exists(from)) {
			return;
		}
		List<Path> files;
		try (Stream<Path> pathStream = Files.walk(from)) {

View on GitHub (pinned to 5b2dbdbb8b)

Solutions

  1. Run with -X to see the full compiler diagnostics embedded in the exception message and fix the offending generated/source reference.
  2. Clean previous AOT output: mvn clean to remove stale generated sources.
  3. Align the build JDK/toolchain with spring-boot.aot.compilerArguments and the configured --release version.
  4. Update or exclude dependencies that the AOT engine cannot process; verify the application compiles normally before enabling AOT/native.
  5. If using a toolchain, confirm <jdk> toolchain is configured and points to a compatible JDK.

Example fix

// before: build fails on AOT compile with mismatched release
//   mvn -Pnative package
//   -> "Unable to compile generated source..."
// after: align toolchain + release, then clean
// pom.xml
//   <maven.compiler.release>21</maven.compiler.release>
// shell:
//   mvn clean package -Pnative
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate generated AOT sources compile cleanly before the native build
// Run: mvn -Pnative compile process-aot first, then inspect target/spring-aot/main/sources
val aotSources = java.io.File("target/spring-aot/main/sources")
if (aotSources.exists()) {
    val ok = aotSources.walkTopDown().filter { it.isFile }.all { it.canRead() }
    if (!ok) error("AOT sources incomplete; run mvn clean process-aot")
}

Try / catch

try {
    // invoke the AOT/native goal
} catch (ex: org.apache.maven.plugin.MojoExecutionException) {
    if (ex.cause is IllegalStateException && ex.message?.contains("Unable to compile generated source") == true) {
        project.logger.error("AOT compile failed; review -X output and align JDK/dependency versions.")
    }
    throw ex
}

Prevention

When it happens

Trigger: An AOT mojo (process-aot/test-aot, or the native-image mojos) calls compileSourceFiles() on the generated sources directory; javac reports errors (result==false or errors.hasReportedErrors()), so line 180 raises IllegalStateException("Unable to compile generated source" + errors).

Common situations: Incompatible dependency versions fed to AOT; generated code references APIs not on the compiler classpath; JDK/toolchain mismatch (e.g., sources target newer language features than the build JDK); --release/source/target misconfiguration; a third-party library not AOT-friendly; corrupted generated sources from a prior run.

Related errors


AI-assisted analysis of spring-projects/spring-boot@5b2dbdbb8b (2026-08-04). Data as JSON: /data/errors/abeeb0764079902a.json. Report an issue: GitHub.