Skip to content

Commit

Permalink
improvement(jib): support concurrent builds in maven
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir Vagaytsev <[email protected]>
  • Loading branch information
Ishant Agarwal and vvagaytsev committed Jun 22, 2023
1 parent e50d93a commit bdb9505
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 10 deletions.
13 changes: 13 additions & 0 deletions docs/reference/action-types/Build/jib-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ spec:
# To ensure a system JDK usage, please set `jdkPath` to `${local.env.JAVA_HOME}`.
mavendPath:

# Enable/disable concurrent Maven and Maven Daemon builds
lockAcquire: true

# Specify extra flags to pass to maven/gradle when building the container image.
extraFlags:
```
Expand Down Expand Up @@ -726,6 +729,16 @@ To ensure a system JDK usage, please set `jdkPath` to `${local.env.JAVA_HOME}`.
| -------- | -------- |
| `string` | No |

### `spec.lockAcquire`

[spec](#spec) > lockAcquire

Enable/disable concurrent Maven and Maven Daemon builds

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `true` | No |

### `spec.extraFlags[]`

[spec](#spec) > extraFlags
Expand Down
13 changes: 13 additions & 0 deletions docs/reference/module-types/jib-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ build:
# To ensure a system JDK usage, please set `jdkPath` to `${local.env.JAVA_HOME}`.
mavendPath:

# Enable/disable concurrent Maven and Maven Daemon builds
lockAcquire: true

# Specify extra flags to pass to maven/gradle when building the container image.
extraFlags:

Expand Down Expand Up @@ -1022,6 +1025,16 @@ To ensure a system JDK usage, please set `jdkPath` to `${local.env.JAVA_HOME}`.
| -------- | -------- |
| `string` | No |

### `build.lockAcquire`

[build](#build) > lockAcquire

Enable/disable concurrent Maven and Maven Daemon builds

| Type | Default | Required |
| --------- | ------- | -------- |
| `boolean` | `true` | No |

### `build.extraFlags[]`

[build](#build) > extraFlags
Expand Down
9 changes: 8 additions & 1 deletion plugins/jib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ const jibBuildSchemaKeys = () => ({
**Note!** Either \`jdkVersion\` or \`jdkPath\` will be used to define \`JAVA_HOME\` environment variable for the custom Maven Daemon.
To ensure a system JDK usage, please set \`jdkPath\` to \`${systemJdkGardenEnvVar}\`.
`),
lockAcquire: joi
.boolean()
.optional()
.default(true)
.description("Enable/disable concurrent Maven and Maven Daemon builds"),
extraFlags: joi
.sparseArray()
.items(joi.string())
Expand Down Expand Up @@ -173,7 +178,7 @@ export const gardenPlugin = () =>
build: async (params) => {
const { ctx, log, action } = params
const spec = action.getSpec() as JibBuildActionSpec
const { jdkVersion, jdkPath, mavenPhases, mavenPath, mavendPath, gradlePath } = spec
const { jdkVersion, jdkPath, mavenPhases, mavenPath, mavendPath, mavenLock, gradlePath } = spec

let openJdkPath: string
if (!!jdkPath) {
Expand Down Expand Up @@ -224,6 +229,7 @@ export const gardenPlugin = () =>
args: [...mavenPhases, ...args],
openJdkPath,
mavenPath,
mavenLock,
outputStream,
})
} else if (projectType === "mavend") {
Expand All @@ -234,6 +240,7 @@ export const gardenPlugin = () =>
args: [...mavenPhases, ...args],
openJdkPath,
mavendPath,
mavenLock,
outputStream,
})
} else {
Expand Down
27 changes: 22 additions & 5 deletions plugins/jib/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export async function mvn({
openJdkPath,
mavenPath,
outputStream,
mavenLock,
}: {
ctx: PluginContext
args: string[]
Expand All @@ -131,6 +132,7 @@ export async function mvn({
openJdkPath: string
mavenPath?: string
outputStream?: Writable
mavenLock?: boolean
}) {
let mvnPath: string
if (!!mavenPath) {
Expand All @@ -143,11 +145,26 @@ export async function mvn({
mvnPath = await tool.ensurePath(log)
}

// Maven has issues when running concurrent processes, so we're working around that with a lock.
// TODO: http://takari.io/book/30-team-maven.html would be a more robust solution.
return buildLock.acquire("mvn", async () => {
log.debug(`Execing ${mvnPath} ${args.join(" ")}`)
log.debug(`Execing ${mvnPath} ${args.join(" ")}`)
if (mavenLock) {
// Maven has issues when running concurrent processes, so we're working around that with a lock.
// TODO: http://takari.io/book/30-team-maven.html would be a more robust solution.
return buildLock.acquire("mvn", async () => {
const res = execa(mvnPath, args, {
cwd,
env: {
JAVA_HOME: openJdkPath,
},
})

if (outputStream) {
res.stdout?.pipe(outputStream)
res.stderr?.pipe(outputStream)
}

return res
})
} else {
const res = execa(mvnPath, args, {
cwd,
env: {
Expand All @@ -161,5 +178,5 @@ export async function mvn({
}

return res
})
}
}
28 changes: 24 additions & 4 deletions plugins/jib/mavend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export async function mvnd({
openJdkPath,
mavendPath,
outputStream,
mavenLock,
}: {
ctx: PluginContext
args: string[]
Expand All @@ -161,20 +162,39 @@ export async function mvnd({
openJdkPath: string
mavendPath?: string
outputStream?: Writable
mavenLock?: boolean
}) {
let mvndPath: string
if (!!mavendPath) {
log.verbose(`Using explicitly specified Maven Daemon binary from ${mavendPath}`)
mvndPath = mavendPath
await verifyMavendPath(mvndPath)
} else {
log.verbose(`The Maven Daemon binary hasn't been specified explicitly. Maven ${mvndVersion} will be used by default.`)
log.verbose(
`The Maven Daemon binary hasn't been specified explicitly. Maven ${mvndVersion} will be used by default.`
)
const tool = getMvndTool(ctx)
mvndPath = await tool.ensurePath(log)
}
return buildLock.acquire("mvnd", async () => {
log.debug(`Execing ${mvndPath} ${args.join(" ")}`)

log.debug(`Execing ${mvndPath} ${args.join(" ")}`)
if (mavenLock) {
return buildLock.acquire("mvnd", async () => {
const res = execa(mvndPath, args, {
cwd,
env: {
JAVA_HOME: openJdkPath,
},
})

if (outputStream) {
res.stdout?.pipe(outputStream)
res.stderr?.pipe(outputStream)
}

return res
})
} else {
const res = execa(mvndPath, args, {
cwd,
env: {
Expand All @@ -188,5 +208,5 @@ export async function mvnd({
}

return res
})
}
}
1 change: 1 addition & 0 deletions plugins/jib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface JibBuildSpec {
mavenPath?: string
mavendPath?: string
mavenPhases: string[]
mavenLock?: boolean
gradlePath?: string
}

Expand Down

0 comments on commit bdb9505

Please sign in to comment.