Skip to content

Commit

Permalink
Better support for relative alternate POM resolution
Browse files Browse the repository at this point in the history
(cherry picked from commit 4a4d775)
  • Loading branch information
aloubyansky authored and gsmet committed Aug 3, 2021
1 parent 13499bb commit d9deec8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -800,31 +800,28 @@ private Path resolveCurrentPom() {
if (alternatePom != null) {
return pomXmlOrNull(basedir.resolve(alternatePom));
}
final Path pom = basedir.resolve("pom.xml");
final Path pom = basedir.resolve(LocalProject.POM_XML);
return Files.exists(pom) ? pom : null;
}

static Path getPomForDirOrNull(final Path basedir, Path alternatePom) {
// if the basedir matches the parent of the alternate pom, it's the alternate pom
if (alternatePom != null
&& alternatePom.isAbsolute()
&& alternatePom.getParent().equals(basedir)) {
return alternatePom;
}
// even if the alternate pom has been specified we try the default pom.xml first
// since unlike Maven CLI we don't know which project originated the build
Path pom = basedir.resolve("pom.xml");
if (Files.exists(pom)) {
return pom;
if (alternatePom != null) {
if (alternatePom.getNameCount() == 1 || basedir.endsWith(alternatePom.getParent())) {
if (alternatePom.isAbsolute()) {
// if the basedir matches the parent of the alternate pom, it's the alternate pom
return alternatePom;
}
// if the basedir ends with the alternate POM parent relative path, we can try it as the base dir
final Path pom = basedir.resolve(alternatePom.getFileName());
if (Files.exists(pom)) {
return pom;
}
}
}

// if alternate pom path has a single element we can try it
// if it has more, it won't match the basedir
if (alternatePom != null && !alternatePom.isAbsolute() && alternatePom.getNameCount() == 1) {
pom = basedir.resolve(alternatePom);
if (Files.exists(pom)) {
return pom;
}
final Path pom = basedir.resolve(LocalProject.POM_XML);
if (Files.exists(pom)) {
return pom;
}

// give up
Expand All @@ -833,7 +830,7 @@ static Path getPomForDirOrNull(final Path basedir, Path alternatePom) {

private static Path pomXmlOrNull(Path path) {
if (Files.isDirectory(path)) {
path = path.resolve("pom.xml");
path = path.resolve(LocalProject.POM_XML);
}
return Files.exists(path) ? path : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class LocalProject {
private static final String PROJECT_BASEDIR = "${project.basedir}";
private static final String PROJECT_BUILD_DIR = "${project.build.directory}";

static final String POM_XML = "pom.xml";
public static final String POM_XML = "pom.xml";

public static LocalProject load(Path path) throws BootstrapMavenException {
return load(path, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AddExtensionIT extends QuarkusPlatformAwareMojoTestBase {
private static final String QUARKUS_GROUPID = "io.quarkus";
private static final String BOM_ARTIFACT_ID = "quarkus-bom";
private static final String VERTX_ARTIFACT_ID = "quarkus-vertx";
private static final String COMMONS_IO = "commons-io";
private static final String COMMONS_CODEC = "commons-codec";
private static final String PROJECT_SOURCE_DIR = "projects/classic";
private File testDir;
private Invoker invoker;
Expand Down Expand Up @@ -69,16 +69,16 @@ void testAddExtensionWithASingleExtensionToSubmodule() throws MavenInvocationExc
void testAddExtensionWithMultipleExtension() throws MavenInvocationException, IOException {
testDir = initProject(PROJECT_SOURCE_DIR, "projects/testAddExtensionWithMultipleExtension");
invoker = initInvoker(testDir);
addExtension(false, "quarkus-vertx, commons-io:commons-io:2.6");
addExtension(false, "quarkus-vertx, commons-codec:commons-codec:1.15");

Model model = loadPom(testDir);
Dependency expected1 = new Dependency();
expected1.setGroupId(QUARKUS_GROUPID);
expected1.setArtifactId(VERTX_ARTIFACT_ID);
Dependency expected2 = new Dependency();
expected2.setGroupId(COMMONS_IO);
expected2.setArtifactId(COMMONS_IO);
expected2.setVersion("2.6");
expected2.setGroupId(COMMONS_CODEC);
expected2.setArtifactId(COMMONS_CODEC);
expected2.setVersion("1.15");
assertThat(contains(model.getDependencies(), expected1)).isTrue();
assertThat(contains(model.getDependencies(), expected2)).isTrue();
}
Expand All @@ -102,24 +102,24 @@ void testAddExtensionWithMultipleExtensionsAndPluralForm() throws MavenInvocatio
testDir = initProject(PROJECT_SOURCE_DIR,
"projects/testAddExtensionWithMultipleExtensionAndPluralForm");
invoker = initInvoker(testDir);
addExtension(true, "quarkus-vertx, commons-io:commons-io:2.6");
addExtension(true, "quarkus-vertx, commons-codec:commons-codec:1.15");

Model model = loadPom(testDir);
Dependency expected1 = new Dependency();
expected1.setGroupId(QUARKUS_GROUPID);
expected1.setArtifactId(VERTX_ARTIFACT_ID);
Dependency expected2 = new Dependency();
expected2.setGroupId(COMMONS_IO);
expected2.setArtifactId(COMMONS_IO);
expected2.setVersion("2.6");
expected2.setGroupId(COMMONS_CODEC);
expected2.setArtifactId(COMMONS_CODEC);
expected2.setVersion("1.15");
assertThat(contains(model.getDependencies(), expected1)).isTrue();
assertThat(contains(model.getDependencies(), expected2)).isTrue();
}

private boolean contains(List<Dependency> dependencies, Dependency expected) {
return dependencies.stream().anyMatch(dep -> dep.getGroupId().equals(expected.getGroupId())
&& dep.getArtifactId().equals(expected.getArtifactId())
&& (dep.getVersion() == null && expected.getVersion() == null || dep.getVersion().equals(expected.getVersion()))
&& (expected.getVersion() == null ? dep.getVersion() == null : expected.getVersion().equals(dep.getVersion()))
&& (dep.getScope() == null || dep.getScope().equals(expected.getScope()))
&& dep.isOptional() == expected.isOptional()
&& dep.getType().equals(expected.getType()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,39 +409,20 @@ public void testAlternatePom() throws Exception {
if (alternatePom.exists()) {
alternatePom.delete();
}
pom.renameTo(alternatePom);
if (pom.exists()) {
throw new IllegalStateException(pom + " was expected to be renamed to " + alternatePom);
}
runAndCheck("-f", alternatePomName);

// Edit a Java file too
final File javaSource = new File(testDir, "src/main/java/org/acme/HelloResource.java");
final String uuid = UUID.randomUUID().toString();
filter(javaSource, Collections.singletonMap("return \"hello\";", "return \"hello " + uuid + "\";"));

// edit the application.properties too
final File applicationProps = new File(testDir, "src/main/resources/application.properties");
filter(applicationProps, Collections.singletonMap("greeting=bonjour", "greeting=" + uuid + ""));

Files.copy(pom.toPath(), alternatePom.toPath());
// Now edit the pom.xml to trigger the dev mode restart
filter(alternatePom, Collections.singletonMap("<!-- insert test dependencies here -->",
" <dependency>\n" +
" <groupId>io.quarkus</groupId>\n" +
" <artifactId>quarkus-smallrye-openapi</artifactId>\n" +
" </dependency>"));

// Wait until we get the updated responses
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> DevModeTestUtils.getHttpResponse("/app/hello").contains("hello " + uuid));

await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> DevModeTestUtils.getHttpResponse("/app/hello/greeting").contains(uuid));
runAndCheck();
assertThat(DevModeTestUtils.getHttpResponse("/q/openapi", true)).contains("Resource not found");
shutdownTheApp();

runAndCheck("-f", alternatePomName);
DevModeTestUtils.getHttpResponse("/q/openapi").contains("hello");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class RunAndCheckMojoTestBase extends MojoTestBase {

@AfterEach
public void cleanup() throws IOException {
shutdownTheApp();
}

public void shutdownTheApp() {
if (running != null) {
running.stop();
}
Expand Down

0 comments on commit d9deec8

Please sign in to comment.