OpenAPITools/openapi-generator · error · MojoExecutionException
Either 'addCompileSourceRoot' or 'addTestCompileSourceRoot'
Error message
Either 'addCompileSourceRoot' or 'addTestCompileSourceRoot' may be active, not both.
What it means
Thrown by addCompileSourceRootIfConfigured() in CodeGenMojo when both <addCompileSourceRoot> and <addTestCompileSourceRoot> are true. The plugin would otherwise add the same generated-sources root to both the main and test compile roots, causing duplicate classes, so it rejects the combination up front with a MojoExecutionException.
Source
Thrown at modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java:1225
name = Files.getNameWithoutExtension(segments[segments.length - 1]);
}
return new File(output.getPath() + File.separatorChar + ".openapi-generator" + File.separatorChar + name + "-" + mojo.getExecutionId() + ".sha256");
}
private String getCompileSourceRoot() {
final Object sourceFolderObject =
configOptions == null ? null : configOptions
.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder = sourceFolderObject != null ? sourceFolderObject.toString() : "src/main/java";
return output.getPath() + File.separatorChar + sourceFolder;
}
private void addCompileSourceRootIfConfigured() throws MojoExecutionException {
if (addCompileSourceRoot) {
if (addTestCompileSourceRoot) {
throw new MojoExecutionException("Either 'addCompileSourceRoot' or 'addTestCompileSourceRoot' may be active, not both.");
}
project.addCompileSourceRoot(getCompileSourceRoot());
} else if (addTestCompileSourceRoot) {
project.addTestCompileSourceRoot(getCompileSourceRoot());
}
}
/**
* This method enables conversion of true/false strings in
* config.additionalProperties (configuration/configOptions) to proper booleans.
* This enables mustache files to handle the properties better.
*
* @param config
*/
private void adjustAdditionalProperties(final CodegenConfig config) {
Map<String, Object> configAdditionalProperties = config.additionalProperties();
Set<String> keySet = configAdditionalProperties.keySet();
for (String key : keySet) {View on GitHub (pinned to fcec517be3)
Solutions
- Keep exactly one flag true per execution: <addCompileSourceRoot>true</addCompileSourceRoot> for main sources, <addTestCompileSourceRoot>true</addTestCompileSourceRoot> for test sources.
- If you need generated code in both main and test, define two executions with separate <output> directories and one flag each, then let test code depend on the main generated classes.
- In inherited-config situations, override the unwanted flag to false in the child module.
Example fix
<!-- before --> <configuration> <addCompileSourceRoot>true</addCompileSourceRoot> <addTestCompileSourceRoot>true</addTestCompileSourceRoot> </configuration> <!-- after --> <configuration> <addCompileSourceRoot>true</addCompileSourceRoot> <addTestCompileSourceRoot>false</addTestCompileSourceRoot> </configuration>
Defensive patterns
Strategy: validation
Validate before calling
# reject POMs that enable both source-root flags
grep -q '<addCompileSourceRoot>true' pom.xml && grep -q '<addTestCompileSourceRoot>true' pom.xml && {
echo 'addCompileSourceRoot and addTestCompileSourceRoot are mutually exclusive'; exit 1;
} Prevention
- Model main vs test generation as two executions with separate output dirs and one flag each.
- Set the unused flag explicitly to false when inheriting configuration from a parent POM.
- Add a POM-lint rule for this pair — the failure only surfaces at build time, late in configuration review.
When it happens
Trigger: Generate goal configuration containing <addCompileSourceRoot>true</addCompileSourceRoot> and <addTestCompileSourceRoot>true</addTestCompileSourceRoot> simultaneously — commonly after copy-pasting test-generation config into main-generation config, or flipping a single boolean into two booleans during a refactor.
Common situations: One execution generating models for main code and another for tests, where someone enabled both flags in the shared configuration; inheriting plugin configuration from a parent POM that already sets one flag and adding the other in the child.
Related errors
- inputSpec, inputSpecRootDirectory, or inputSpecFiles must be
- mergedFileOutputDir must be set when inputSpecFiles is used
- Invalid mergeMode value '${mergeMode}'. Valid values are: RE
- Invalid mergeConflictStrategy value '${mergeConflictStrategy
- The generator requires 'generatorName'. Refer to documentati
AI-assisted analysis of OpenAPITools/openapi-generator@fcec517be3 (2026-08-22).
Data as JSON: /api/errors/23ed261ff6c83c24.
Report an issue: GitHub.