Skip to content

Commit

Permalink
Fixed multithreaded gradle
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Asmann <[email protected]>
  • Loading branch information
malice00 committed Aug 29, 2024
1 parent d109eb5 commit a81e113
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
1 change: 1 addition & 0 deletions docs/ENV.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ The following environment variables are available to configure the bom generatio
| GRADLE_HOME | Specify gradle home |
| GRADLE_CMD | Set to override gradle command |
| GRADLE_DEPENDENCY_TASK | By default cdxgen use the task "dependencies" to collect packages. Set to override the task name. |
| GRADLE_SKIP_MODULES | Comma-separated list of modules to skip during the "dependencies" task. This can be useful if you have modules that would fail the gradle build, eg when they do not have dependencies in the given configuration. Use "root" if the top most module should be skipped, use the name (without leading ":") for all others. |
| SBT_CACHE_DIR | Specify sbt cache directory. Useful for class name resolving |
| FETCH_LICENSE | Set this variable to `true` or `1` to fetch license information from the registry. npm and golang |
| SEARCH_MAVEN_ORG | If maven metadata is missing in jar file, a search is performed on search.maven.org. Set to `false` or `0` to disable search. (defaults to `true`) |
Expand Down
66 changes: 43 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1748,50 +1748,66 @@ export async function createJavaBom(path, options) {
isPackageManagerAllowed("gradle", ["maven", "bazel", "sbt"], options)
) {
const gradleCmd = getGradleCommand(gradleRootPath, null);
const defaultDepTaskArgs = ["--console", "plain", "--build-cache"];
const defaultTaskArgs = ["--console", "plain", "--build-cache"];
allProjects.push(parentComponent);
let depTaskWithArgs = ["dependencies"];
let relevantTasks = ["dependencies"];
if (process.env.GRADLE_DEPENDENCY_TASK) {
depTaskWithArgs = process.env.GRADLE_DEPENDENCY_TASK.split(" ");
relevantTasks = process.env.GRADLE_DEPENDENCY_TASK.split(" ");
}
let gradleDepArgs = [];
gradleDepArgs = gradleDepArgs
.concat(depTaskWithArgs.slice(1))
.concat(defaultDepTaskArgs);
let gradleArgs = [];
gradleArgs = gradleArgs.concat(defaultTaskArgs);
// Support custom GRADLE_ARGS such as --configuration runtimeClassPath (used for all tasks)
if (process.env.GRADLE_ARGS) {
const addArgs = process.env.GRADLE_ARGS.split(" ");
gradleDepArgs = gradleDepArgs.concat(addArgs);
gradleArgs = gradleArgs.concat(addArgs);
}
// gradle args only for the dependencies task
let gradleDepArgs = [];
gradleDepArgs = gradleDepArgs.concat(depTaskWithArgs.slice(1));
if (process.env.GRADLE_ARGS_DEPENDENCIES) {
const addArgs = process.env.GRADLE_ARGS_DEPENDENCIES.split(" ");
gradleDepArgs = gradleDepArgs.concat(addArgs);
}

if (process.env.GRADLE_MULTI_THREADED) {
gradleDepArgs.push(depTaskWithArgs[0]);
const modulesToSkip = process.env.GRADLE_SKIP_MODULES
? process.env.GRADLE_SKIP_MODULES.split(",")
: [];
let gradleSubProjectDepArgs = [];
if (!modulesToSkip.includes("root")) {
gradleSubProjectDepArgs.push(depTaskWithArgs[0]);
gradleSubProjectDepArgs = gradleSubProjectDepArgs.concat(gradleDepArgs);
}
for (const sp of allProjects) {
//create single command for dependencies tasks on all subprojects
if (sp.purl !== parentComponent.purl) {
gradleDepArgs.push(`:${sp.name}:${depTaskWithArgs[0]}`);
if (
sp.purl !== parentComponent.purl &&
!modulesToSkip.includes(sp.name)
) {
gradleSubProjectDepArgs.push(`:${sp.name}:${depTaskWithArgs[0]}`);
gradleSubProjectDepArgs =
gradleSubProjectDepArgs.concat(gradleDepArgs);
}
}
console.log(
"Executing",
gradleCmd,
gradleDepArgs.join(" "),
gradleArgs.concat(gradleSubProjectDepArgs).join(" "),
"in",
gradleRootPath,
);
const sresult = spawnSync(gradleCmd, gradleDepArgs, {
cwd: gradleRootPath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER,
});
const sresult = spawnSync(
gradleCmd,
gradleArgs.concat(gradleSubProjectDepArgs),
{
cwd: gradleRootPath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER,
},
);

if (sresult.status !== 0 || sresult.error) {
if (options.failOnError || DEBUG_MODE) {
Expand Down Expand Up @@ -1857,16 +1873,20 @@ export async function createJavaBom(path, options) {
console.log(
"Executing",
gradleCmd,
gradleSubProjectDepArgs.join(" "),
gradleArgs.concat(gradleSubProjectDepArgs).join(" "),
"in",
gradleRootPath,
);
const sresult = spawnSync(gradleCmd, gradleSubProjectDepArgs, {
cwd: gradleRootPath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER,
});
const sresult = spawnSync(
gradleCmd,
gradleArgs.concat(gradleSubProjectDepArgs),
{
cwd: gradleRootPath,
encoding: "utf-8",
timeout: TIMEOUT_MS,
maxBuffer: MAX_BUFFER,
},
);
if (sresult.status !== 0 || sresult.error) {
if (options.failOnError || DEBUG_MODE) {
console.error(sresult.stdout, sresult.stderr);
Expand Down
31 changes: 14 additions & 17 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2878,40 +2878,37 @@ export function parseGradleProperties(rawOutput) {
*/
export function executeParallelGradleProperties(dir, rootPath, allProjectsStr) {
const defaultProps = {
rootProject: subProject,
rootProject: "root",
projects: [],
metadata: {
version: "latest",
},
};
let parallelPropTaskArgs = ["properties"];
for (const spstr of allProjectsStr) {
parallelPropTaskArgs.push(`${spstr}:properties`);
}

let gradlePropertiesArgs = ["--console", "plain", "--build-cache"];
const gradleCmd = getGradleCommand(dir, rootPath);

// common gradle args, used for all tasks
let gradleArgs = ["--console", "plain", "--build-cache"];
if (process.env.GRADLE_ARGS) {
const addArgs = process.env.GRADLE_ARGS.split(" ");
gradlePropertiesArgs = gradlePropertiesArgs.concat(addArgs);
gradleArgs = gradleArgs.concat(addArgs);
}

// gradle args only for the properties task
let gradlePropertiesArgs = [];
if (process.env.GRADLE_ARGS_PROPERTIES) {
const addArgs = process.env.GRADLE_ARGS_PROPERTIES.split(" ");
gradlePropertiesArgs = gradlePropertiesArgs.concat(addArgs);
}
parallelPropTaskArgs = parallelPropTaskArgs.concat(gradlePropertiesArgs);

console.log(
"Executing",
gradleCmd,
parallelPropTaskArgs.join(" "),
"in",
dir,
);
const result = spawnSync(gradleCmd, parallelPropTaskArgs, {
gradleArgs.push("properties");
gradleArgs = gradleArgs.concat(gradlePropertiesArgs);
for (const spstr of allProjectsStr) {
gradleArgs.push(`${spstr}:properties`);
gradleArgs = gradleArgs.concat(gradlePropertiesArgs);
}

console.log("Executing", gradleCmd, gradleArgs.join(" "), "in", dir);
const result = spawnSync(gradleCmd, gradleArgs, {
cwd: dir,
encoding: "utf-8",
shell: isWin,
Expand Down

0 comments on commit a81e113

Please sign in to comment.