Skip to content

Commit

Permalink
The maven.multiModuleProjectDirectory is badly set when using -f [pat…
Browse files Browse the repository at this point in the history
…h-to-pom], fixes apache#484
  • Loading branch information
gnodet committed Oct 6, 2021
1 parent 33b72a6 commit c6b4049
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import java.util.function.Supplier;
Expand Down Expand Up @@ -204,9 +205,13 @@ public Path daemonOutLog(String daemon) {
}

public Path multiModuleProjectDirectory() {
return multiModuleProjectDirectory(userDir());
}

public Path multiModuleProjectDirectory(Path projectDir) {
return value(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY)
.orSystemProperty()
.orDefault(() -> findDefaultMultimoduleProjectDirectory(userDir()))
.orDefault(() -> findDefaultMultimoduleProjectDirectory(projectDir))
.asPath()
.toAbsolutePath().normalize();
}
Expand Down Expand Up @@ -257,6 +262,13 @@ public Path settings() {
return property(Environment.MAVEN_SETTINGS).asPath();
}

/**
* @return path to {@code pom.xml} or {@code null}
*/
public Path file() {
return value(Environment.MAVEN_FILE).asPath();
}

/**
* @return absolute normalized path to local Maven repository or {@code null} if the server is supposed to use the
* default
Expand Down Expand Up @@ -292,16 +304,18 @@ public boolean serial() {
* @return a new {@link DaemonParameters} with {@code userDir} set to the given {@code newUserDir}
*/
public DaemonParameters cd(Path newUserDir) {
return new DaemonParameters(new PropertiesBuilder()
.putAll(this.properties)
.put(Environment.USER_DIR, newUserDir));
return derive(b -> b.put(Environment.USER_DIR, newUserDir));
}

public DaemonParameters withJdkJavaOpts(String opts) {
String org = this.properties.getOrDefault(Environment.JDK_JAVA_OPTIONS.getProperty(), "");
return new DaemonParameters(new PropertiesBuilder()
.putAll(this.properties)
.put(Environment.JDK_JAVA_OPTIONS, org + opts));
return derive(b -> b.put(Environment.JDK_JAVA_OPTIONS, org + opts));
}

protected DaemonParameters derive(Consumer<PropertiesBuilder> customizer) {
PropertiesBuilder builder = new PropertiesBuilder().putAll(this.properties);
customizer.accept(builder);
return new DaemonParameters(builder);
}

public Duration keepAlive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
}
Environment.MVND_TERMINAL_WIDTH.addCommandLineOption(args, Integer.toString(output.getTerminalWidth()));

Path dir;
if (Environment.MAVEN_FILE.hasCommandLineOption(args)) {
dir = parameters.userDir().resolve(Environment.MAVEN_FILE.getCommandLineOption(args));
if (Files.isRegularFile(dir)) {
dir = dir.getParent();
}
dir = dir.normalize();
} else {
dir = parameters.userDir();
}

final DaemonConnector connector = new DaemonConnector(parameters, registry);
try (DaemonClientConnection daemon = connector.connect(output)) {
output.setDaemonId(daemon.getDaemon().getId());
Expand All @@ -260,7 +271,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
daemon.dispatch(new Message.BuildRequest(
args,
parameters.userDir().toString(),
parameters.multiModuleProjectDirectory().toString(),
parameters.multiModuleProjectDirectory(dir).toString(),
System.getenv()));

output.accept(Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public enum Environment {
MAVEN_REPO_LOCAL("maven.repo.local", null, null, OptionType.PATH, Flags.NONE),
/** The location of the maven settings file */
MAVEN_SETTINGS("maven.settings", null, null, OptionType.PATH, Flags.NONE, "mvn:-s", "mvn:--settings"),
/** The pom or directory to build */
MAVEN_FILE(null, null, null, OptionType.PATH, Flags.NONE, "mvn:-f", "mvn:--file"),
/** The root directory of the current multi module Maven project */
MAVEN_MULTIMODULE_PROJECT_DIRECTORY("maven.multiModuleProjectDirectory", null, null, OptionType.PATH, Flags.NONE),
/** Log file */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void testMessageOrdering() {
Message.projectStopped("projectId"),
Message.projectStarted("projectId"),
Message.log("projectId", "message"),
Message.executionFailure("projectId", true, "error")
));
Message.executionFailure("projectId", true, "error")));

assertEquals(Message.PROJECT_STARTED, messages.remove().getType());
assertEquals(Message.EXECUTION_FAILURE, messages.remove().getType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.it;

import java.io.IOException;
import java.nio.file.Path;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
import org.mvndaemon.mvnd.assertj.TestClientOutput;
import org.mvndaemon.mvnd.client.Client;
import org.mvndaemon.mvnd.junit.ClientFactory;
import org.mvndaemon.mvnd.junit.MvndNativeTest;
import org.mvndaemon.mvnd.junit.TestParameters;

import static org.junit.jupiter.api.Assertions.assertTrue;

@MvndNativeTest(projectDir = "src/test/projects/specific-file")
public class SpecificFileNativeIT {

@Inject
ClientFactory clientFactory;

@Inject
TestParameters parameters;

@Test
void version() throws IOException, InterruptedException {
TestClientOutput output = new TestClientOutput();


Path prj1 = parameters.getTestDir().resolve("project/prj1").toAbsolutePath();
Path prj2 = parameters.getTestDir().resolve("project/prj2").toAbsolutePath();

Client cl = clientFactory.newClient(parameters.clearMavenMultiModuleProjectDirectory().cd(prj1));

cl.execute(output, "org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate",
"-Dexpression=maven.multiModuleProjectDirectory", "-f", "../prj2/pom.xml", "-e").assertSuccess();
assertTrue(output.getMessages().stream()
.anyMatch(m -> m.toString().contains(prj2.toString())), "Output should contain " + prj2);
}

protected boolean isNative() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.it;

import org.mvndaemon.mvnd.junit.MvndTest;

@MvndTest(projectDir = "src/test/projects/specific-file")
public class SpecificFileTest extends SpecificFileNativeIT {
protected boolean isNative() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public TestParameters(Path testDir, Path mvndPropertiesPath, Path mavenHome, Pat
.put(Environment.MVND_MAX_LOST_KEEP_ALIVE, maxLostKeepAlive)
.put(Environment.MVND_MIN_THREADS, TEST_MIN_THREADS));
this.testDir = testDir;
}

public DaemonParameters clearMavenMultiModuleProjectDirectory() {
return derive(b -> b.put(Environment.MAVEN_MULTIMODULE_PROJECT_DIRECTORY, null));
}

public Path getTestDir() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-Dmaven.wagon.httpconnectionManager.ttlSeconds=120
-Dmaven.wagon.http.retryHandler.requestSentEnabled=true
-Dmaven.wagon.http.retryHandler.count=10
27 changes: 27 additions & 0 deletions integration-tests/src/test/projects/specific-file/prj1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Copyright 2021 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.mvndaemon.mvnd.test.specific-file</groupId>
<artifactId>specific-file-prj1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-Dmaven.wagon.httpconnectionManager.ttlSeconds=120
-Dmaven.wagon.http.retryHandler.requestSentEnabled=true
-Dmaven.wagon.http.retryHandler.count=10
27 changes: 27 additions & 0 deletions integration-tests/src/test/projects/specific-file/prj2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
Copyright 2021 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.mvndaemon.mvnd.test.specific-file</groupId>
<artifactId>specific-file-prj2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

</project>

0 comments on commit c6b4049

Please sign in to comment.