Skip to content

Commit a9c8bf2

Browse files
authored
feat: support passing additional arguments to maven (#238)
1 parent cc39167 commit a9c8bf2

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,14 @@ It's also possible to use the lightweight Python PIP utility [pipdeptree](https:
491491
#### Toggle Red Hat Trusted Content recommendations
492492
Both the HTML-based report and JSON response will by default contain recommendations for migrating to Red Hat-based Trusted Content repositories. This feature can be disabled by setting `EXHORT_RECOMMENDATIONS_ENABLED` to 'false' via environment variables or options.
493493

494+
#### Additional CLI arguments
495+
For some ecosystems we support passing additional CLI arguments to the underlying tools. The following table outlines the supported ecosystems and the environment variable/option that configures this. Note that the arguments are expected to be in the format of a JSON array.
496+
497+
|Ecosystem|Key |
498+
|---------|---------------|
499+
|Maven |EXHORT_MVN_ARGS|
500+
501+
494502
<!-- Badge links -->
495503
[0]: https://img.shields.io/github/v/release/trustification/exhort-javascript-api?color=green&label=latest
496504
[1]: https://img.shields.io/github/v/release/trustification/exhort-javascript-api?color=yellow&include_prereleases&label=early-access

src/providers/java_maven.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { EOL } from 'os'
66
import { XMLParser } from 'fast-xml-parser'
77

88
import Sbom from '../sbom.js'
9+
import { getCustom } from '../tools.js'
910

1011
import Base_java, { ecosystem_maven } from "./base_java.js";
1112

@@ -27,14 +28,13 @@ export default class Java_maven extends Base_java {
2728
* @param {string} manifestName - the subject manifest name-type
2829
* @returns {boolean} - return true if `pom.xml` is the manifest name-type
2930
*/
30-
3131
isSupported(manifestName) {
3232
return 'pom.xml' === manifestName
3333
}
3434

3535
/**
3636
* @param {string} manifestDir - the directory where the manifest lies
37-
*/
37+
*/
3838
validateLockFile() { return true; }
3939

4040
/**
@@ -43,8 +43,6 @@ export default class Java_maven extends Base_java {
4343
* @param {{}} [opts={}] - optional various options to pass along the application
4444
* @returns {Provided}
4545
*/
46-
47-
4846
provideStack(manifest, opts = {}) {
4947
return {
5048
ecosystem: ecosystem_maven,
@@ -59,7 +57,6 @@ export default class Java_maven extends Base_java {
5957
* @param {{}} [opts={}] - optional various options to pass along the application
6058
* @returns {Provided}
6159
*/
62-
6360
provideComponent(manifest, opts = {}) {
6461
return {
6562
ecosystem: ecosystem_maven,
@@ -78,20 +75,24 @@ export default class Java_maven extends Base_java {
7875
#createSbomStackAnalysis(manifest, opts = {}) {
7976
const manifestDir = path.dirname(manifest)
8077
const mvn = this.selectToolBinary(manifest, opts)
78+
const mvnArgs = JSON.parse(getCustom('EXHORT_MVN_ARGS', '[]', opts));
79+
if (!Array.isArray(mvnArgs)) {
80+
throw new Error(`configured maven args is not an array, is a ${typeof mvnArgs}`)
81+
}
8182

8283
// clean maven target
8384
try {
84-
this._invokeCommand(mvn, ['-q', 'clean'], {cwd: manifestDir})
85+
this._invokeCommand(mvn, ['-q', 'clean', ...mvnArgs], { cwd: manifestDir })
8586
} catch (error) {
86-
throw new Error(`failed to clean maven target`, {cause: error})
87+
throw new Error(`failed to clean maven target`, { cause: error })
8788
}
8889

8990
// create dependency graph in a temp file
9091
let tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'exhort_'))
9192
let tmpDepTree = path.join(tmpDir, 'mvn_deptree.txt')
9293
// build initial command (dot outputType is not available for verbose mode)
9394
let depTreeCmdArgs = ['-q', 'org.apache.maven.plugins:maven-dependency-plugin:3.6.0:tree',
94-
'-Dscope=compile','-Dverbose',
95+
'-Dscope=compile', '-Dverbose',
9596
'-DoutputType=text', `-DoutputFile=${tmpDepTree}`]
9697
// exclude ignored dependencies, exclude format is groupId:artifactId:scope:version.
9798
// version and scope are marked as '*' if not specified (we do not use scope yet)
@@ -104,14 +105,14 @@ export default class Java_maven extends Base_java {
104105
ignoredDeps.push(this.toPurl(dep.groupId, dep.artifactId))
105106
}
106107
})
107-
if(ignoredArgs.length > 0) {
108+
if (ignoredArgs.length > 0) {
108109
depTreeCmdArgs.push(`-Dexcludes=${ignoredArgs.join(',')}`)
109110
}
110111
// execute dependency tree command
111112
try {
112-
this._invokeCommand(mvn, depTreeCmdArgs, {cwd: manifestDir})
113+
this._invokeCommand(mvn, [...depTreeCmdArgs, ...mvnArgs], { cwd: manifestDir })
113114
} catch (error) {
114-
throw new Error(`failed creating maven dependency tree`, {cause: error})
115+
throw new Error(`failed creating maven dependency tree`, { cause: error })
115116
}
116117
// read dependency tree from temp file
117118
let content = fs.readFileSync(tmpDepTree)
@@ -120,7 +121,7 @@ export default class Java_maven extends Base_java {
120121
}
121122
let sbom = this.createSbomFileFromTextFormat(content.toString(), ignoredDeps, opts);
122123
// delete temp file and directory
123-
fs.rmSync(tmpDir, {recursive: true, force: true})
124+
fs.rmSync(tmpDir, { recursive: true, force: true })
124125
// return dependency graph as string
125126
return sbom
126127
}
@@ -150,14 +151,18 @@ export default class Java_maven extends Base_java {
150151
*/
151152
#getSbomForComponentAnalysis(manifestPath, opts = {}) {
152153
const mvn = this.selectToolBinary(manifestPath, opts)
154+
const mvnArgs = JSON.parse(getCustom('EXHORT_MVN_ARGS', '[]', opts));
155+
if (!Array.isArray(mvnArgs)) {
156+
throw new Error(`configured maven args is not an array, is a ${typeof mvnArgs}`)
157+
}
153158

154159
const tmpEffectivePom = path.resolve(path.join(path.dirname(manifestPath), 'effective-pom.xml'))
155160

156161
// create effective pom and save to temp file
157162
try {
158-
this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`], {cwd: path.dirname(manifestPath)})
163+
this._invokeCommand(mvn, ['-q', 'help:effective-pom', `-Doutput=${tmpEffectivePom}`, ...mvnArgs], { cwd: path.dirname(manifestPath) })
159164
} catch (error) {
160-
throw new Error(`failed creating maven effective pom`, {cause: error})
165+
throw new Error(`failed creating maven effective pom`, { cause: error })
161166
}
162167
// iterate over all dependencies in original pom and collect all ignored ones
163168
let ignored = this.#getDependencies(manifestPath).filter(d => d.ignore)

0 commit comments

Comments
 (0)