Skip to content

Commit 0a35014

Browse files
committed
feat: accept customized Maven user settings file and local repository.
Signed-off-by: Chao Wang <chaowan@redhat.com>
1 parent fa6c0c0 commit 0a35014

2 files changed

Lines changed: 54 additions & 27 deletions

File tree

src/main/java/com/redhat/exhort/providers/JavaMavenProvider.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,23 @@ public JavaMavenProvider(Path manifest) {
6565

6666
@Override
6767
public Content provideStack() throws IOException {
68-
var mvnCleanCmd =
69-
new String[] {mvnExecutable, "clean", "-f", manifest.toString(), "--batch-mode", "-q"};
68+
var mvnCleanCmd = buildMvnCommandArgs("clean", "-f", manifest.toString(), "--batch-mode", "-q");
7069
var mvnEnvs = getMvnExecEnvs();
7170
// execute the clean command
72-
Operations.runProcess(manifest.getParent(), mvnCleanCmd, mvnEnvs);
71+
Operations.runProcess(manifest.getParent(), mvnCleanCmd.toArray(String[]::new), mvnEnvs);
7372
// create a temp file for storing the dependency tree in
7473
var tmpFile = Files.createTempFile("exhort_dot_graph_", null);
7574
// the tree command will build the project and create the dependency tree in the temp file
7675
var mvnTreeCmd =
77-
new ArrayList<String>() {
78-
{
79-
add(mvnExecutable);
80-
add("org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree");
81-
add("-Dverbose");
82-
add("-DoutputType=text");
83-
add(String.format("-DoutputFile=%s", tmpFile.toString()));
84-
add("-f");
85-
add(manifest.toString());
86-
add("--batch-mode");
87-
add("-q");
88-
}
89-
};
76+
buildMvnCommandArgs(
77+
"org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree",
78+
"-Dverbose",
79+
"-DoutputType=text",
80+
String.format("-DoutputFile=%s", tmpFile.toString()),
81+
"-f",
82+
manifest.toString(),
83+
"--batch-mode",
84+
"-q");
9085
// if we have dependencies marked as ignored, exclude them from the tree command
9186
var ignored =
9287
getDependencies(manifest).stream()
@@ -133,18 +128,17 @@ public Content provideComponent() throws IOException {
133128
private Content generateSbomFromEffectivePom() throws IOException {
134129
var tmpEffPom = Files.createTempFile("exhort_eff_pom_", ".xml");
135130
var mvnEffPomCmd =
136-
new String[] {
137-
mvnExecutable,
138-
"clean",
139-
"help:effective-pom",
140-
String.format("-Doutput=%s", tmpEffPom.toString()),
141-
"-f",
142-
manifest.toString(),
143-
"--batch-mode",
144-
"-q"
145-
};
131+
buildMvnCommandArgs(
132+
"clean",
133+
"help:effective-pom",
134+
String.format("-Doutput=%s", tmpEffPom.toString()),
135+
"-f",
136+
manifest.toString(),
137+
"--batch-mode",
138+
"-q");
146139
// execute the effective pom command
147-
Operations.runProcess(manifest.getParent(), mvnEffPomCmd, getMvnExecEnvs());
140+
Operations.runProcess(
141+
manifest.getParent(), mvnEffPomCmd.toArray(String[]::new), getMvnExecEnvs());
148142
if (debugLoggingIsNeeded()) {
149143
String CaEffectivePoM = Files.readString(tmpEffPom);
150144
log.info(
@@ -345,6 +339,28 @@ Map<String, String> getMvnExecEnvs() {
345339
return null;
346340
}
347341

342+
private List<String> buildMvnCommandArgs(String... baseArgs) {
343+
List<String> args = new ArrayList<>();
344+
args.add(mvnExecutable);
345+
346+
var userSettingsFile = Operations.getMavenConfig("USER_SETTINGS_FILE");
347+
if (userSettingsFile != null) {
348+
args.add("-s");
349+
args.add(userSettingsFile);
350+
}
351+
352+
var localRepository = Operations.getMavenConfig("LOCAL_REPOSITORY");
353+
if (localRepository != null) {
354+
args.add("-Dmaven.repo.local=" + localRepository);
355+
}
356+
357+
for (String arg : baseArgs) {
358+
args.add(arg);
359+
}
360+
361+
return args;
362+
}
363+
348364
// NOTE if we want to include "scope" tags in ignore,
349365
// add property here and a case in the start-element-switch in the getIgnored method
350366

src/main/java/com/redhat/exhort/tools/Operations.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,17 @@ public static boolean getWrapperPreference(String name) {
316316
return Environment.get("EXHORT_PREFER_" + name.toUpperCase() + "W") != null;
317317
}
318318

319+
/**
320+
* Retrieves a Maven configuration value from an environment variable.
321+
*
322+
* @param configName the configuration name (e.g., "USER_SETTINGS_FILE", "LOCAL_REPOSITORY")
323+
* @return the configuration value if set and not blank, null otherwise
324+
*/
325+
public static String getMavenConfig(String configName) {
326+
String configValue = Environment.get("EXHORT_MVN_" + configName);
327+
return (configValue != null && !configValue.isBlank()) ? configValue : null;
328+
}
329+
319330
/**
320331
* Attempts to retrieve the root directory of a Git repository for the given working directory.
321332
*

0 commit comments

Comments
 (0)