Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/using-diffci.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ _Module resource and dependency impact contributed by [@FredApps](https://github
| `Maven analysis found no pom.xml` | A Java repository built with another tool | No |
| `Maven reactor contains no Java sources` | A `pom.xml` with only resources | No |
| `Maven …: custom source roots require full validation` | Set `<sourceDirectory>`, or a `<testSourceDirectory>` that is interpolated (`${…}`), absolute, outside the module, declared more than once, inside a profile, or inherited from a parent POM | Declare a plain relative `<testSourceDirectory>` inside the module, in the module's own `<build>`; DiffCI finds tests there |
| `Maven …: Java sources outside the standard roots require full validation` | A `.java` or `.kt` file in the module outside `src/main/{java,kotlin}`, `src/test/{java,kotlin}` and the module's `<testSourceDirectory>`, such as a `src/it/java` root added by `build-helper-maven-plugin` or a test root set by a parent POM outside the repository | Move the files under a standard root, or declare the test root as a plain relative `<testSourceDirectory>` in the module's own POM |
| `Maven …: parent dependency inheritance requires full validation` | Inherit dependencies from an external parent POM | No |
| `Maven …: unresolved dependency coordinates require full validation` | Use a `${property}` version DiffCI cannot resolve | No |

Expand Down
12 changes: 12 additions & 0 deletions src/repo/adapters/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface Pom {
declaredTestRoots: string[];
/** Whether a `<testSourceDirectory>` sits inside a profile, which may or may not be active. */
profileTestRoot: boolean;
/** Whether the POM configures build-helper-maven-plugin, which can add any folder, resources included, as a source root. */
buildHelper: boolean;
}

const DEFAULT_TEST_ROOT = "src/test/java";
Expand Down Expand Up @@ -86,6 +88,7 @@ function parsePom(path: string, source: string): Pom {
customSourceRoots: tags(xml, "sourceDirectory").some(root => root !== "src/main/java"),
testRoot: profileTestRoot ? undefined : resolveTestRoot(dir, declaredTestRoots),
declaredTestRoots, profileTestRoot,
buildHelper: xml.includes("build-helper-maven-plugin"),
};
}

Expand Down Expand Up @@ -148,6 +151,15 @@ export function analyzeMaven(context: AdapterContext): AdapterContribution {
const sources = owned.filter(file => /\.(?:java|kt)$/.test(file) &&
(/\/src\/(?:main|test)\/(?:java|kotlin)\//.test("/" + file) || (pom.testRoot !== undefined && file.startsWith(pom.testRoot + "/"))));
const assets = owned.filter(file => /\/src\/(?:main|test)\/resources\//.test("/" + file));
// Java or Kotlin outside every root DiffCI recognises may still be compiled and run by Maven: a test root
// added by build-helper-maven-plugin, or one set by a parent POM outside the repository. Fail closed (#327).
// Resources (archetype templates, compiler-test inputs) and the Maven wrapper's downloader are not sources,
// unless build-helper-maven-plugin is configured: it can add a folder under resources as a source root.
const known = new Set(sources);
if (owned.some(file => /\.(?:java|kt)$/.test(file) && !known.has(file) &&
(pom.buildHelper || !/\/src\/(?:main|test)\/resources\//.test("/" + file)) && !/(?:^|\/)\.mvn\//.test(file))) {
result.blockers.push(`Maven ${pom.path}: Java sources outside the standard roots require full validation`);
}
members.set(pom, sources);
resources.set(pom, assets);
const anchor = sources[0] ?? assets[0];
Expand Down
64 changes: 64 additions & 0 deletions tests/repo/maven-test-source-directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,67 @@ for (const [name, parentBuild, childBuild, testPath] of [
} finally { rmSync(root, { recursive: true, force: true }); }
});
}

const outsideRoots = /Java sources outside the standard roots require full validation/;

// #327: Java or Kotlin that Maven may compile and run from a root DiffCI doesn't know falls back, for a library
// change and for a change to that file itself.
for (const [label, extra, buildHelper] of [
["a src/it/java test root added by build-helper-maven-plugin", "application/src/it/java/example/AppIT.java", true],
["a test root set by a parent POM outside the repository", "application/src/integration/java/example/AppIT.java", false],
["a Kotlin file outside the roots", "application/src/it/kotlin/example/AppIT.kt", false],
] as const) {
test(`Maven falls back for ${label} (#327)`, async () => {
const build = buildHelper
? "<plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><executions><execution><goals><goal>add-test-source</goal></goals><configuration><sources><source>src/it/java</source></sources></configuration></execution></executions></plugin></plugins>"
: "";
const files = reactor(build, "application/src/test/java/example/AppTest.java");
files[extra] = "package example; class AppIT {}";
if (label.includes("parent POM")) {
files["application/pom.xml"] = files["application/pom.xml"]!.replace("<project>", "<project><parent><groupId>com.acme</groupId><artifactId>company-parent</artifactId><version>3</version></parent>");
}
const root = fixture(files);
try {
const graph = await buildDependencyGraph({ repoPath: root });
assert.deepEqual(graph.adapterBlockers, ["Maven application/pom.xml: Java sources outside the standard roots require full validation"]);
for (const changed of ["library/src/main/java/example/Library.java", extra]) {
const impact = new ImpactAnalyzer().analyze(delta(changed), graph, graph.profile);
assert.equal(impact.fallbackRequired, true, changed);
assert.ok(impact.fallbackReasons.some((reason) => outsideRoots.test(reason)), impact.fallbackReasons.join("; "));
}
} finally { rmSync(root, { recursive: true, force: true }); }
});
}

test("Maven does not fall back for Java under resources or in the Maven wrapper (#327)", async () => {
const files = reactor("", "application/src/test/java/example/AppTest.java");
files["application/src/main/resources/archetype-resources/src/main/java/App.java"] = "public class App {}";
files["application/src/test/resources/compile/Input.java"] = "class Input {}";
files[".mvn/wrapper/MavenWrapperDownloader.java"] = "public class MavenWrapperDownloader {}";
const root = fixture(files);
try {
const graph = await buildDependencyGraph({ repoPath: root });
assert.deepEqual(graph.adapterBlockers, []);
const impact = new ImpactAnalyzer().analyze(delta("library/src/main/java/example/Library.java"), graph, graph.profile);
assert.equal(impact.fallbackRequired, false, impact.fallbackReasons.join("; "));
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("A file under a resolved <testSourceDirectory> is inside the roots (#327)", async () => {
const root = fixture(reactor("<testSourceDirectory>tests</testSourceDirectory>", "application/tests/example/AppTest.java"));
try {
const graph = await buildDependencyGraph({ repoPath: root });
assert.ok(!graph.adapterBlockers?.some((reason) => outsideRoots.test(reason)), graph.adapterBlockers?.join("; "));
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("Maven falls back for Java under resources when build-helper-maven-plugin could add it as a root (#327)", async () => {
const build = "<plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId><executions><execution><goals><goal>add-test-source</goal></goals><configuration><sources><source>src/test/resources/java</source></sources></configuration></execution></executions></plugin></plugins>";
const files = reactor(build, "application/src/test/java/example/AppTest.java");
files["application/src/test/resources/java/example/ResourceIT.java"] = "package example; class ResourceIT {}";
const root = fixture(files);
try {
const graph = await buildDependencyGraph({ repoPath: root });
assert.deepEqual(graph.adapterBlockers, ["Maven application/pom.xml: Java sources outside the standard roots require full validation"]);
} finally { rmSync(root, { recursive: true, force: true }); }
});
Loading