Skip to content

Commit

Permalink
[MSHARED-1390] Deprecate InvocationRequest#setGoals
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 4, 2024
1 parent d956445 commit aecd032
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 137 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-invoker</artifactId>
<version>3.2.1-SNAPSHOT</version>
<version>3.3.0-SNAPSHOT</version>

<name>Apache Maven Invoker</name>
<description>A component to programmatically invoke Maven.</description>
Expand Down Expand Up @@ -65,7 +65,7 @@ under the License.

<properties>
<javaVersion>8</javaVersion>
<project.build.outputTimestamp>2022-04-05T18:45:23Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2024-05-04T12:59:43Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand All @@ -81,7 +81,7 @@ under the License.
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -113,7 +114,7 @@ public class DefaultInvocationRequest implements InvocationRequest {

private boolean noTransferProgress;

private List<String> args = new ArrayList<>();
private final List<String> args = new ArrayList<>();

/**
* <p>getBaseDirectory.</p>
Expand Down Expand Up @@ -471,6 +472,12 @@ public InvocationRequest addArg(String arg) {
return this;
}

@Override
public InvocationRequest addArgs(Collection<String> args) {
args.stream().filter(arg -> !StringUtils.isBlank(arg)).forEach(this.args::add);
return this;
}

public List<String> getArgs() {
return args;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -552,6 +553,16 @@ enum CheckSumPolicy {
*/
InvocationRequest addArg(String arg);

/**
* Add a raw arguments list to Maven cli command at the end of other arguments.
* Can be called multiple time in order to add many arguments.
*
* @param args a raw Maven args line
* @return This invocation request.
* @since 3.3.0
*/
InvocationRequest addArgs(Collection<String> args);

/**
* Sets the path to the base directory of the POM for the Maven invocation. If {@link #getPomFile()} does not return
* <code>null</code>, this setting only affects the working directory for the Maven invocation.
Expand Down Expand Up @@ -583,7 +594,9 @@ enum CheckSumPolicy {
*
* @param goals The goals for the Maven invocation, may be <code>null</code> to execute the POMs default goal.
* @return This invocation request.
* @deprecated simply {@link #addArg(String)} or {@link #addArgs(Collection)} should be used
*/
@Deprecated
InvocationRequest setGoals(List<String> goals);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,7 @@ protected void setToolchainsLocation(InvocationRequest request, Commandline cli)
* @param cli a {@link org.apache.maven.shared.utils.cli.Commandline} object.
*/
protected void setShellEnvironment(InvocationRequest request, Commandline cli) {
if (request.isShellEnvironmentInherited()) {
cli.addSystemEnvironment();
}
cli.setShellEnvironmentInherited(request.isShellEnvironmentInherited());

if (request.getJavaHome() != null) {
cli.addEnvironment("JAVA_HOME", request.getJavaHome().getAbsolutePath());
Expand All @@ -213,7 +211,7 @@ protected void setProfiles(InvocationRequest request, Commandline cli) {

if ((profiles != null) && !profiles.isEmpty()) {
cli.createArg().setValue("-P");
cli.createArg().setValue(StringUtils.join(profiles.iterator(), ","));
cli.createArg().setValue(String.join(",", profiles));
}
}

Expand All @@ -229,7 +227,7 @@ protected void setGoals(InvocationRequest request, Commandline cli) throws Comma

if ((goals != null) && !goals.isEmpty()) {
try {
cli.createArg().setLine(StringUtils.join(goals.iterator(), " "));
cli.createArg().setLine(String.join(" ", goals));
} catch (CommandLineException e) {
throw new CommandLineConfigurationException("Problem setting goals", e);
}
Expand Down Expand Up @@ -381,7 +379,7 @@ protected void setReactorBehavior(InvocationRequest request, Commandline cli) {
List<String> projectList = request.getProjects();
if (projectList != null) {
cli.createArg().setValue("-pl");
cli.createArg().setValue(StringUtils.join(projectList.iterator(), ","));
cli.createArg().setValue(String.join(",", projectList));

if (request.isAlsoMake()) {
cli.createArg().setValue("-am");
Expand Down Expand Up @@ -471,7 +469,7 @@ protected void setThreads(InvocationRequest request, Commandline cli) {
}
}

private void setArgs(InvocationRequest request, Commandline cli) {
protected void setArgs(InvocationRequest request, Commandline cli) {
for (String arg : request.getArgs()) {
cli.createArg().setValue(arg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

import org.apache.maven.shared.utils.Os;
Expand All @@ -34,19 +33,20 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DefaultInvokerTest {
class DefaultInvokerTest {

private Invoker invoker = newInvoker();
private InvocationRequest request = new DefaultInvocationRequest();

@BeforeEach
public void setUp() throws Exception {
void setUp() {
request.setDebug(true);
request.setProperties(getProperties());
}

@Test
public void testBuildShouldSucceed() throws MavenInvocationException, URISyntaxException {
@SuppressWarnings("deprecation")
void testBuildShouldSucceed() throws MavenInvocationException, URISyntaxException {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setGoals(Arrays.asList("clean", "package"));
Expand All @@ -57,21 +57,21 @@ public void testBuildShouldSucceed() throws MavenInvocationException, URISyntaxE
}

@Test
public void testBuildShouldFail() throws MavenInvocationException, URISyntaxException {
void testBuildShouldFail() throws MavenInvocationException, URISyntaxException {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setGoals(Arrays.asList("clean", "package"));
request.addArgs(Arrays.asList("clean", "package"));

InvocationResult result = invoker.execute(request);

assertEquals(1, result.getExitCode());
}

@Test
public void testBuildShouldTimeout() throws MavenInvocationException, URISyntaxException {
void testBuildShouldTimeout() throws MavenInvocationException, URISyntaxException {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setGoals(Arrays.asList("clean", "package"));
request.addArgs(Arrays.asList("clean", "package"));
request.setTimeoutInSeconds(4);

InvocationResult result = invoker.execute(request);
Expand All @@ -89,95 +89,90 @@ public void testBuildShouldTimeout() throws MavenInvocationException, URISyntaxE
}

@Test
public void testSpacePom() throws Exception {
void testSpacePom() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setPomFileName("pom with spaces.xml");
request.setGoals(Collections.singletonList("clean"));
request.addArg("clean");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testSpaceAndSpecialCharPom() throws Exception {
void testSpaceAndSpecialCharPom() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setPomFileName("pom with spaces & special char.xml");
request.setGoals(Collections.singletonList("clean"));
request.addArg("clean");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testSpaceSettings() throws Exception {
void testSpaceSettings() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setUserSettingsFile(new File(basedir, "settings with spaces.xml"));
request.setGoals(Collections.singletonList("validate"));
request.addArg("validate");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testSpaceLocalRepo() throws Exception {
void testSpaceLocalRepo() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setLocalRepositoryDirectory(new File(basedir, "repo with spaces"));
request.setGoals(Collections.singletonList("validate"));
request.addArg("validate");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testSpaceProperties() throws Exception {
void testSpaceProperties() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
Properties props = getProperties();
props.setProperty("key", "value with spaces");
props.setProperty("key with spaces", "value");
request.setProperties(props);
request.setGoals(Collections.singletonList("validate"));
request.addArg("validate");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testPomOutsideProject() throws Exception {
void testPomOutsideProject() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
File pom = new File(basedir, "temp/pom.xml");
request.setPomFile(pom);
request.setGoals(Collections.singletonList("validate"));
request.addArg("validate");

InvocationResult result = invoker.execute(request);

assertEquals(0, result.getExitCode());
}

@Test
public void testMavenWrapperInProject() throws Exception {
void testMavenWrapperInProject() throws Exception {
File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.setGoals(Collections.singletonList("test-wrapper-goal"));
request.addArg("test-wrapper-goal");
request.setMavenExecutable(new File("./mvnw"));

final StringBuilder outlines = new StringBuilder();
request.setOutputHandler(new InvocationOutputHandler() {
@Override
public void consumeLine(String line) {
outlines.append(line);
}
});
request.setOutputHandler(outlines::append);

InvocationResult result = invoker.execute(request);

Expand Down
Loading

0 comments on commit aecd032

Please sign in to comment.