Skip to content

Commit

Permalink
[MNG-8594] Add atFile option
Browse files Browse the repository at this point in the history
Where user can create ad-hoc command line parms. The difference
between .mvn/maven.conf and this file is that this file allws
goals as well. The CLI and atFile are merged, in this order,
hence, goals in atFile will come AFTER goals specified on CLI.

---

https://issues.apache.org/jira/browse/MNG-8594
  • Loading branch information
cstamas committed Feb 26, 2025
1 parent 48c66e3 commit 19ba3af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public interface MavenOptions extends Options {
@Nonnull
Optional<Boolean> ignoreTransitiveRepositories();

/**
* Allows {@code @filename} to read options from file.
*/
Optional<String> atFile();

/**
* Returns the list of goals and phases to execute.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ public Optional<Boolean> ignoreTransitiveRepositories() {
return Optional.empty();
}

@Override
public Optional<String> atFile() {
if (commandLine.hasOption(CLIManager.AT_FILE)) {
return Optional.of(commandLine.getOptionValue(CLIManager.AT_FILE));
}
return Optional.empty();
}

@Override
public Optional<List<String>> goals() {
if (!commandLine.getArgList().isEmpty()) {
Expand Down Expand Up @@ -273,6 +281,7 @@ protected static class CLIManager extends CommonsCliOptions.CLIManager {
public static final String CACHE_ARTIFACT_NOT_FOUND = "canf";
public static final String STRICT_ARTIFACT_DESCRIPTOR_POLICY = "sadp";
public static final String IGNORE_TRANSITIVE_REPOSITORIES = "itr";
public static final String AT_FILE = "af";

@Override
protected void prepareOptions(org.apache.commons.cli.Options options) {
Expand Down Expand Up @@ -375,6 +384,11 @@ protected void prepareOptions(org.apache.commons.cli.Options options) {
.longOpt("ignore-transitive-repositories")
.desc("If set, Maven will ignore remote repositories introduced by transitive dependencies.")
.build());
options.addOption(Option.builder(AT_FILE)
.longOpt("at-file")
.hasArg()
.desc("If set, Maven load and merge command line options from the file as well.")
.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ public Optional<Boolean> ignoreTransitiveRepositories() {
return returnFirstPresentOrEmpty(MavenOptions::ignoreTransitiveRepositories);
}

@Override
public Optional<String> atFile() {
return returnFirstPresentOrEmpty(MavenOptions::atFile);
}

@Override
public Optional<List<String>> goals() {
return collectListIfPresentOrEmpty(MavenOptions::goals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ public class MavenParser extends BaseParser {
protected List<Options> parseCliOptions(LocalContext context) {
ArrayList<Options> result = new ArrayList<>();
// CLI args
result.add(parseMavenCliOptions(context.parserRequest.args()));
MavenOptions cliOptions = parseMavenCliOptions(context.parserRequest.args());
result.add(cliOptions);
// atFile option
if (cliOptions.atFile().isPresent()) {
Path file = context.cwd.resolve(cliOptions.atFile().orElseThrow());
if (Files.isRegularFile(file)) {
result.add(parseMavenAtFileOptions(file));
} else {
throw new IllegalArgumentException("Specified atFile does not exists (" + file + ")");
}
}
// maven.config; if exists
Path mavenConfig = context.rootDirectory != null ? context.rootDirectory.resolve(".mvn/maven.config") : null;
if (mavenConfig != null && Files.isRegularFile(mavenConfig)) {
Expand All @@ -54,6 +64,19 @@ protected MavenOptions parseMavenCliOptions(List<String> args) {
}
}

protected MavenOptions parseMavenAtFileOptions(Path atFile) {
try (Stream<String> lines = Files.lines(atFile, Charset.defaultCharset())) {
List<String> args =
lines.filter(arg -> !arg.isEmpty() && !arg.startsWith("#")).toList();
return parseArgs("atFile", args);
} catch (ParseException e) {
throw new IllegalArgumentException(
"Failed to parse arguments from file (" + atFile + "): " + e.getMessage(), e.getCause());
} catch (IOException e) {
throw new IllegalStateException("Error reading config file: " + atFile, e);
}
}

protected MavenOptions parseMavenConfigOptions(Path configFile) {
try (Stream<String> lines = Files.lines(configFile, Charset.defaultCharset())) {
List<String> args =
Expand Down

0 comments on commit 19ba3af

Please sign in to comment.