8000 [MNG-8594] Add atFile option (#2131) · apache/maven@12ff710 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12ff710

Browse files
authored
[MNG-8594] Add atFile option (#2131)
Where user can create ad-hoc command line parms. The difference between .mvn/maven.conf and this file is that this file allows goals as well. The CLI and atFile are merged, in this order, hence, goals in atFile will come AFTER goals specified on CLI, if CLI has them. Source order (first wins for options, while goals are collected/aggregated from sources): * CLI * atFile (if specified, may have goals that are appended) * maven.config (if present, may not have goals) Note: option is called `af` or long `at-file` for similarity with `javac @file`, but commons CLI does not support options without leading hyphen. --- https://issues.apache.org/jira/browse/MNG-8594
1 parent f7ff201 commit 12ff710

File tree

9 files changed

+156
-2
lines changed

9 files changed

+156
-2
lines changed

api/maven-api-cli/src/main/java/org/apache/maven/api/cli/mvn/MavenOptions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ public interface MavenOptions extends Options {
207207
@Nonnull
208208
Optional<Boolean> ignoreTransitiveRepositories();
209209

210+
/**
211+
* Specifies "@file"-like file, to load up command line from. It may contain goals as well. Format is one parameter
212+
* per line (similar to {@code maven.conf}) and {@code '#'} (hash) marked comment lines are allowed. Goals, if
213+
* present, are appended, to those specified on CLI input, if any.
214+
*/
215+
Optional<String> atFile();
216+
210217
/**
211218
* Returns the list of goals and phases to execute.
212219
*

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LayeredOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ protected Optional<Map<String, String>> collectMapIfPresentOrEmpty(
181181
Optional<Map<< 8000 span class=pl-smi>String, String>> up = getter.apply(option);
182182
if (up.isPresent()) {
183183
had++;
184-
items.putAll(up.get());
184+
for (Map.Entry<String, String> entry : up.get().entrySet()) {
185+
items.putIfAbsent(entry.getKey(), entry.getValue());
186+
}
185187
}
186188
}
187189
return had == 0 ? Optional.empty() : Optional.of(Map.copyOf(items));

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/CommonsCliMavenOptions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ public Optional<Boolean> ignoreTransitiveRepositories() {
238238
return Optional.empty();
239239
}
240240

241+
@Override
242+
public Optional<String> atFile() {
243+
if (commandLine.hasOption(CLIManager.AT_FILE)) {
244+
return Optional.of(commandLine.getOptionValue(CLIManager.AT_FILE));
245+
}
246+
return Optional.empty();
247+
}
248+
241249
@Override
242250
public Optional<List<String>> goals() {
243251
if (!commandLine.getArgList().isEmpty()) {
@@ -273,6 +281,7 @@ protected static class CLIManager extends CommonsCliOptions.CLIManager {
273281
public static final String CACHE_ARTIFACT_NOT_FOUND = "canf";
274282
public static final String STRICT_ARTIFACT_DESCRIPTOR_POLICY = "sadp";
275283
public static final String IGNORE_TRANSITIVE_REPOSITORIES = "itr";
284+
public static final String AT_FILE = "af";
276285

277286
@Override
278287
protected void prepareOptions(org.apache.commons.cli.Options options) {
@@ -375,6 +384,12 @@ protected void prepareOptions(org.apache.commons.cli.Options options) {
375384
.longOpt("ignore-transitive-repositories")
376385
.desc("If set, Maven will ignore remote repositories introduced by transitive dependencies.")
377386
.build());
387+
options.addOption(Option.builder(AT_FILE)
388+
.longOpt("at-file")
389+
.hasArg()
390+
.desc(
391+
"If set, Maven will load command line options from the specified file and merge with CLI specified ones.")
392+
.build());
378393
}
379394
}
380395
}

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/LayeredMavenOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ public Optional<Boolean> ignoreTransitiveRepositories() {
154154
return returnFirstPresentOrEmpty(MavenOptions::ignoreTransitiveRepositories);
155155
}
156156

157+
@Override
158+
public Optional<String> atFile() {
159+
return returnFirstPresentOrEmpty(MavenOptions::atFile);
< A93C /code>
160+
}
161+
157162
@Override
158163
public Optional<List<String>> goals() {
159164
return collectListIfPresentOrEmpty(MavenOptions::goals);

impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvn/MavenParser.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,17 @@ public class MavenParser extends BaseParser {
3737
protected List<Options> parseCliOptions(LocalContext context) {
3838
ArrayList<Options> result = new ArrayList<>();
3939
// CLI args
40-
result.add(parseMavenCliOptions(context.parserRequest.args()));
40+
MavenOptions cliOptions = parseMavenCliOptions(context.parserRequest.args());
41+
result.add(cliOptions);
42+
// atFile option
43+
if (cliOptions.atFile().isPresent()) {
44+
Path file = context.cwd.resolve(cliOptions.atFile().orElseThrow());
45+
if (Files.isRegularFile(file)) {
46+
result.add(parseMavenAtFileOptions(file));
47+
} else {
48+
throw new IllegalArgumentException("Specified file does not exists (" + file + ")");
49+
}
50+
}
4151
// maven.config; if exists
4252
Path mavenConfig = context.rootDirectory != null ? context.rootDirectory.resolve(".mvn/maven.config") : null;
4353
if (mavenConfig != null && Files.isRegularFile(mavenConfig)) {
@@ -54,6 +64,19 @@ protected MavenOptions parseMavenCliOptions(List<String> args) {
5464
}
5565
}
5666

67+
protected MavenOptions parseMavenAtFileOptions(Path atFile) {
68+
try (Stream<String> lines = Files.lines(atFile, Charset.defaultCharset())) {
69+
List<String> args =
70+
lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#")).toList();
71+
return parseArgs("atFile", args);
72+
} catch (ParseException e) {
73+
throw new IllegalArgumentException(
74+
"Failed to parse arguments from file (" + atFile + "): " + e.getMessage(), e.getCause());
75+
} catch (IOException e) {
76+
throw new IllegalStateException("Error reading config file: " + atFile, e);
77+
}
78+
}
79+
5780
protected MavenOptions parseMavenConfigOptions(Path configFile) {
5881
try (Stream<String> lines = Files.lines(configFile, Charset.defaultCharset())) {
5982
List<String> args =
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.it;
20+
21+
import java.nio.file.Path;
22+
import java.util.List;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.junit.Assert.assertTrue;
27+
28+
/**
29+
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8594">MNG-8594</a>.
30+
*/
31+
class MavenITmng8594AtFileTest extends AbstractMavenIntegrationTestCase {
32+
33+
MavenITmng8594AtFileTest() {
34+
super("[4.0.0-rc-3-SNAPSHOT,)");
35+
}
36+
37+
/**
38+
* Verify Maven picks up params/goals from atFile.
39+
*/
40+
@Test
41+
void testIt() throws Exception {
42+
Path basedir = extractResources("/mng-8594").getAbsoluteFile().toPath();
43+
44+
Verifier verifier = newVerifier(basedir.toString());
45+
verifier.addCliArgument("-af");
46+
verifier.addCliArgument("cmd.txt");
47+
verifier.addCliArgument("-Dcolor1=green");
48+
verifier.addCliArgument("-Dcolor2=blue");
49+
verifier.addCliArgument("clean");
50+
verifier.execute();
51+
verifier.verifyErrorFreeLog();
52+
53+
// clean did run
54+
verifier.verifyTextInLog("(default-clean) @ root");
55+
// validate bound plugin did run
56+
verifier.verifyTextInLog("(eval) @ root");
57+
58+
// validate properties
59+
List<String> properties = verifier.loadLines("target/pom.properties");
60+
assertTrue(properties.contains("session.executionProperties.color1=green")); // CLI only
61+
assertTrue(properties.contains("session.executionProperties.color2=blue")); // both
62+
assertTrue(properties.contains("session.executi EED3 onProperties.color3=yellow")); // cmd.txt only
63+
}
64+
}

its/core-it-suite/src/test/java/org/apache/maven/it/TestSuiteOrdering.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public TestSuiteOrdering() {
100100
* the tests are to finishing. Newer tests are also more likely to fail, so this is
101101
* a fail fast technique as well.
102102
*/
103+
suite.addTestSuite(MavenITmng8594AtFileTest.class);
103104
suite.addTestSuite(MavenITmng8561SourceRootTest.class);
104105
suite.addTestSuite(MavenITmng8523ModelPropertiesTest.class);
105106
suite.addTestSuite(MavenITmng8527ConsumerPomTest.class);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
validate
2+
-Dcolor2=gray
3+
-Dcolor3=yellow
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.apache.maven.it.mng8594</groupId>
7+
<artifactId>root</artifactId>
8+
<version>1.0.0</version>
9+
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.its.plugins</groupId>
14+
<artifactId>maven-it-plugin-expression</artifactId>
15+
<version>2.1-SNAPSHOT</version>
16+
<configuration>
17+
<outputFile>target/pom.properties</outputFile>
18+
<expressions>
19+
<expression>session/executionProperties</expression>
20+
</expressions>
21+
</configuration>
22+
<executions>
23+
<execution>
24+
<id>eval</id>
25+
<goals>
26+
<goal>eval</goal>
27+
</goals>
28+
<phase>validate</phase>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
</project>

0 commit comments

Comments
 (0)
0