Skip to content

Commit 7cc8b93

Browse files
authored
feat: accept customized Maven user settings file and local repository. (#184)
## Description feat: accept customized Maven user settings file and local repository. **Related issue (if any):** redhat-developer/intellij-dependency-analytics#218 intellij-dependency-analytics PR: redhat-developer/intellij-dependency-analytics#220 ## Checklist - [x] I have followed this repository's contributing guidelines. - [x] I will adhere to the project's code of conduct. --------- Signed-off-by: Chao Wang <chaowan@redhat.com>
1 parent e55a8a6 commit 7cc8b93

File tree

3 files changed

+98
-27
lines changed

3 files changed

+98
-27
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ System.setProperty("EXHORT_PYTHON_PATH", "/path/to/python");
328328
System.setProperty("EXHORT_PIP_PATH", "/path/to/pip");
329329
// Configure proxy for all requests
330330
System.setProperty("EXHORT_PROXY_URL", "http://proxy.example.com:8080");
331+
// Configure Maven settings and repository
332+
System.setProperty("EXHORT_MVN_USER_SETTINGS", "/path/to/custom/settings.xml");
333+
System.setProperty("EXHORT_MVN_LOCAL_REPO", "/path/to/custom/local/repository");
331334
```
332335

333336
> Environment variables takes precedence.
@@ -436,6 +439,47 @@ following keys for setting custom paths for the said executables.
436439

437440
</table>
438441

442+
#### Maven Configuration
443+
444+
You can customize Maven behavior by setting additional environment variables or Java properties:
445+
446+
<table>
447+
<tr>
448+
<th>Configuration</th>
449+
<th>Environment Variable</th>
450+
<th>Description</th>
451+
<th>Default</th>
452+
</tr>
453+
<tr>
454+
<td>Maven User Settings</td>
455+
<td>EXHORT_MVN_USER_SETTINGS</td>
456+
<td>Path to custom Maven settings.xml file</td>
457+
<td><em>Uses Maven's default settings</em></td>
458+
</tr>
459+
<tr>
460+
<td>Maven Local Repository</td>
461+
<td>EXHORT_MVN_LOCAL_REPO</td>
462+
<td>Path to custom Maven local repository directory</td>
463+
<td><em>Uses Maven's default local repository</em></td>
464+
</tr>
465+
</table>
466+
467+
**Examples:**
468+
469+
Using environment variables:
470+
```bash
471+
export EXHORT_MVN_USER_SETTINGS=/home/user/.m2/custom-settings.xml
472+
export EXHORT_MVN_LOCAL_REPO=/home/user/custom-maven-repo
473+
```
474+
475+
Using Java properties:
476+
```java
477+
System.setProperty("EXHORT_MVN_USER_SETTINGS", "/home/user/.m2/custom-settings.xml");
478+
System.setProperty("EXHORT_MVN_LOCAL_REPO", "/home/user/custom-maven-repo");
479+
```
480+
481+
> Environment variables take precedence over Java properties.
482+
439483
#### Match Manifest Versions Feature
440484

441485
##### Background

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");
347+
if (userSettingsFile != null) {
348+
args.add("-s");
349+
args.add(userSettingsFile);
350+
}
351+
352+
var localRepository = Operations.getMavenConfig("LOCAL_REPO");
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", "LOCAL_REPO")
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)