Skip to content

Commit c98b1b6

Browse files
committed
feat(maven): add project selector to batch runner invocation
Enable Maven to locate specific projects by adding project selector to invocation request. Set isRecursive to true to allow Maven to discover all modules in the reactor.
1 parent c926ffb commit c98b1b6

File tree

14 files changed

+207
-56
lines changed

14 files changed

+207
-56
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>dev.nx.maven</groupId>
5+
<artifactId>batch-runner</artifactId>
6+
<name>Nx Maven Batch Runner</name>
7+
<version>0.0.8-SNAPSHOT</version>
8+
<description>Batch runner for Nx Maven plugin using Maven Invoker API</description>
9+
<url>https://github.com/nrwl/nx/packages/nx-maven-parent/batch-runner</url>
10+
<developers>
11+
<developer>
12+
<name>Nx Team</name>
13+
<email>[email protected]</email>
14+
<organization>Nrwl</organization>
15+
<organizationUrl>https://nrwl.io</organizationUrl>
16+
</developer>
17+
</developers>
18+
<licenses>
19+
<license>
20+
<name>MIT License</name>
21+
<url>https://opensource.org/licenses/MIT</url>
22+
<distribution>repo</distribution>
23+
</license>
24+
</licenses>
25+
<scm>
26+
<connection>scm:git:git://github.com/nrwl/nx.git/packages/nx-maven-parent/batch-runner</connection>
27+
<developerConnection>scm:git:ssh://github.com:nrwl/nx.git/packages/nx-maven-parent/batch-runner</developerConnection>
28+
<url>https://github.com/nrwl/nx/tree/master/packages/nx-maven-parent/batch-runner</url>
29+
</scm>
30+
</project>

packages/maven/batch-runner/pom.xml

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,48 @@
55
http://maven.apache.org/xsd/maven-4.0.0.xsd">
66
<modelVersion>4.0.0</modelVersion>
77

8-
<groupId>dev.nx.maven</groupId>
8+
<parent>
9+
<groupId>dev.nx.maven</groupId>
10+
<artifactId>nx-maven-parent</artifactId>
11+
<version>0.0.8-SNAPSHOT</version>
12+
<relativePath>../pom.xml</relativePath>
13+
</parent>
14+
915
<artifactId>batch-runner</artifactId>
10-
<version>0.0.7-SNAPSHOT</version>
1116
<packaging>jar</packaging>
1217

1318
<name>Nx Maven Batch Runner</name>
1419
<description>Batch runner for Nx Maven plugin using Maven Invoker API</description>
1520

16-
<properties>
17-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18-
<maven.compiler.source>11</maven.compiler.source>
19-
<maven.compiler.target>11</maven.compiler.target>
20-
<kotlin.version>1.9.20</kotlin.version>
21-
</properties>
22-
2321
<dependencies>
2422
<!-- Maven Invoker API for programmatic Maven execution -->
2523
<dependency>
2624
<groupId>org.apache.maven.shared</groupId>
2725
<artifactId>maven-invoker</artifactId>
28-
<version>3.1.0</version>
2926
</dependency>
3027

3128
<!-- Logging -->
3229
<dependency>
3330
<groupId>org.slf4j</groupId>
3431
<artifactId>slf4j-api</artifactId>
35-
<version>2.0.9</version>
32+
<scope>compile</scope>
3633
</dependency>
3734
<dependency>
3835
<groupId>org.slf4j</groupId>
3936
<artifactId>slf4j-simple</artifactId>
40-
<version>2.0.9</version>
37+
<scope>compile</scope>
4138
</dependency>
4239

4340
<!-- JSON parsing -->
4441
<dependency>
4542
<groupId>com.google.code.gson</groupId>
4643
<artifactId>gson</artifactId>
47-
<version>2.10.1</version>
4844
</dependency>
4945

5046
<!-- Kotlin -->
5147
<dependency>
5248
<groupId>org.jetbrains.kotlin</groupId>
5349
<artifactId>kotlin-stdlib</artifactId>
54-
<version>${kotlin.version}</version>
5550
</dependency>
5651
</dependencies>
5752

packages/maven/batch-runner/src/main/kotlin/dev/nx/maven/NxMavenBatchRunner.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dev.nx.maven
22

33
import com.google.gson.Gson
44
import dev.nx.maven.cli.ArgParser
5-
import dev.nx.maven.data.TaskResult
65
import dev.nx.maven.runner.MavenInvokerRunner
76
import org.slf4j.LoggerFactory
87
import kotlin.system.exitProcess

packages/maven/batch-runner/src/main/kotlin/dev/nx/maven/cli/ArgParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ object ArgParser {
6969
phase = (taskData["phase"] as? String),
7070
goals = (taskData["goals"] as? List<*>)?.mapNotNull { it as? String } ?: emptyList(),
7171
args = (taskData["args"] as? List<*>)?.mapNotNull { it as? String } ?: emptyList(),
72-
project = (taskData["project"] as? String)
72+
project = (taskData["project"] as String)
7373
)
7474
}
7575
} catch (e: Exception) {

packages/maven/batch-runner/src/main/kotlin/dev/nx/maven/data/MavenBatchOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ data class MavenBatchTask(
1616
val phase: String? = null,
1717
val goals: List<String> = emptyList(),
1818
val args: List<String> = emptyList(),
19-
val project: String? = null
19+
val project: String
2020
)
2121

2222
data class TaskResult(

packages/maven/batch-runner/src/main/kotlin/dev/nx/maven/runner/MavenInvokerRunner.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
2727
log.info("Task execution order from graph: ${taskIds.joinToString(", ")}")
2828

2929
// Group tasks by project
30-
val tasksByProject = groupTasksByProject(taskIds)
30+
val tasksByProject = groupTasksByProject()
3131
log.info("Grouped tasks into ${tasksByProject.size} projects")
3232
for ((project, tasks) in tasksByProject) {
3333
log.info(" Project '$project': ${tasks.joinToString(", ")}")
@@ -56,7 +56,7 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
5656
}
5757

5858
private fun executeProjectTasks(
59-
project: String?,
59+
project: String,
6060
projectTasks: List<String>,
6161
results: ConcurrentHashMap<String, TaskResult>
6262
) {
@@ -69,7 +69,7 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
6969
val projectOutput = StringBuilder()
7070

7171
try {
72-
val request = createInvocationRequest(projectTasks)
72+
val request = createInvocationRequest(project, projectTasks)
7373

7474
// Set output handler to capture Maven output
7575
invoker.setOutputHandler { line ->
@@ -146,17 +146,15 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
146146
}
147147
}
148148

149-
private fun groupTasksByProject(taskIds: List<String>): LinkedHashMap<String?, List<String>> {
150-
val grouped = LinkedHashMap<String?, MutableList<String>>()
149+
private fun groupTasksByProject(): LinkedHashMap<String, MutableList<String>> {
150+
val grouped = LinkedHashMap<String, MutableList<String>>()
151151

152-
for (taskId in taskIds) {
153-
val task = options.tasks[taskId]
154-
val project = task?.project
155-
grouped.computeIfAbsent(project) { mutableListOf() }.add(taskId)
152+
for ((taskId, task) in options.tasks) {
153+
val project = task.project
154+
grouped.computeIfAbsent(project) { mutableListOf() }.add(taskId)
156155
}
157156

158-
@Suppress("UNCHECKED_CAST")
159-
return grouped as LinkedHashMap<String?, List<String>>
157+
return grouped
160158
}
161159

162160
private fun buildTargets(task: MavenBatchTask): List<String> {
@@ -168,18 +166,23 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
168166
return targets
169167
}
170168

171-
private fun createInvocationRequest(taskIds: List<String>): InvocationRequest {
169+
private fun createInvocationRequest(project: String, taskIds: List<String>): InvocationRequest {
172170
val request = DefaultInvocationRequest()
173171
request.baseDirectory = File(options.workspaceRoot)
174172

173+
// Don't filter by project selector (-pl flag)
174+
// Maven will discover and build all modules in the reactor
175+
// The Nx goals will handle recording execution for the specific project
176+
177+
request.projects = mutableListOf(project)
178+
175179
request.goals = mutableListOf()
176180
// Collect all goals from tasks (with Nx wrapper goals)
177181

178182
// Add Nx Maven record goal after user goals
179183
request.goals.add("dev.nx.maven:nx-maven-plugin:apply")
180184
for (taskId in taskIds) {
181185
val task = options.tasks[taskId] ?: continue
182-
request.projects = listOf(task.project)
183186
val targets = buildTargets(task)
184187
request.goals.addAll(targets)
185188
log.debug("Added targets from task $taskId: ${targets.joinToString(", ")}")
@@ -188,9 +191,9 @@ class MavenInvokerRunner(private val options: MavenBatchOptions) {
188191
// Add Nx Maven record goal after user goals
189192
request.goals.add("dev.nx.maven:nx-maven-plugin:record")
190193

191-
request.isRecursive = false
194+
request.isRecursive = true
192195

193-
log.info("Executing ${request.goals.joinToString(", ")} goals for ${request.projects.joinToString(", ")}")
196+
log.info("Executing ${request.goals.joinToString(", ")} goals for project: $project")
194197

195198
// Add additional arguments
196199
val allArgs = mutableListOf<String>()

packages/maven/maven-plugin/pom.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<parent>
6-
<groupId>dev.nx</groupId>
7-
<artifactId>nx-parent</artifactId>
6+
<groupId>dev.nx.maven</groupId>
7+
<artifactId>nx-maven-parent</artifactId>
88
<version>0.0.8-SNAPSHOT</version>
9-
<relativePath>../../../pom.xml</relativePath>
9+
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

12-
<groupId>dev.nx.maven</groupId>
1312
<artifactId>nx-maven-plugin</artifactId>
14-
<version>${project.parent.version}</version>
1513
<packaging>maven-plugin</packaging>
1614

1715
<name>Nx Maven Plugin</name>

packages/maven/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"author": "Victor Savkin",
4646
"license": "MIT",
4747
"dependencies": {
48-
"@nx/devkit": "workspace:*",
48+
"@nx/devkit": "22.0.1",
4949
"@xmldom/xmldom": "^0.8.10",
5050
"tslib": "^2.3.0"
5151
},

packages/maven/pom.xml

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>dev.nx</groupId>
8+
<artifactId>nx-parent</artifactId>
9+
<version>0.0.8-SNAPSHOT</version>
10+
<relativePath>../../pom.xml</relativePath>
11+
</parent>
12+
13+
<groupId>dev.nx.maven</groupId>
14+
<artifactId>nx-maven-parent</artifactId>
15+
<packaging>pom</packaging>
16+
17+
<name>Nx Maven Parent</name>
18+
<description>Parent POM for Nx Maven projects (batch-runner and nx-maven-plugin)</description>
19+
20+
<modules>
21+
<module>batch-runner</module>
22+
<module>maven-plugin</module>
23+
</modules>
24+
25+
<properties>
26+
<!-- Skip deployment for this parent POM -->
27+
<maven.install.skip>true</maven.install.skip>
28+
<maven.deploy.skip>true</maven.deploy.skip>
29+
<maven.gpg.skip>true</maven.gpg.skip>
30+
</properties>
31+
32+
<dependencyManagement>
33+
<dependencies>
34+
<!-- Maven Invoker API for batch-runner -->
35+
<dependency>
36+
<groupId>org.apache.maven.shared</groupId>
37+
<artifactId>maven-invoker</artifactId>
38+
<version>3.1.0</version>
39+
</dependency>
40+
41+
<!-- Gson for JSON parsing -->
42+
<dependency>
43+
<groupId>com.google.code.gson</groupId>
44+
<artifactId>gson</artifactId>
45+
<version>2.10.1</version>
46+
</dependency>
47+
48+
<!-- SLF4J Simple -->
49+
<dependency>
50+
<groupId>org.slf4j</groupId>
51+
<artifactId>slf4j-simple</artifactId>
52+
<version>${slf4j.version}</version>
53+
</dependency>
54+
55+
<!-- JGit for gitignore parsing -->
56+
<dependency>
57+
<groupId>org.eclipse.jgit</groupId>
58+
<artifactId>org.eclipse.jgit</artifactId>
59+
<version>6.8.0.202311291450-r</version>
60+
</dependency>
61+
62+
<!-- Plexus Utils for XML parsing -->
63+
<dependency>
64+
<groupId>org.codehaus.plexus</groupId>
65+
<artifactId>plexus-utils</artifactId>
66+
<version>3.5.1</version>
67+
</dependency>
68+
69+
<!-- Maven Plugin Testing Harness -->
70+
<dependency>
71+
<groupId>org.apache.maven.plugin-testing</groupId>
72+
<artifactId>maven-plugin-testing-harness</artifactId>
73+
<version>4.0.0-beta-4</version>
74+
<scope>test</scope>
75+
</dependency>
76+
77+
<!-- Snapshot Testing -->
78+
<dependency>
79+
<groupId>de.skuzzle.test</groupId>
80+
<artifactId>snapshot-tests-junit5</artifactId>
81+
<version>1.11.0</version>
82+
<scope>test</scope>
83+
</dependency>
84+
85+
<!-- JSON Serialization for Snapshots -->
86+
<dependency>
87+
<groupId>de.skuzzle.test</groupId>
88+
<artifactId>snapshot-tests-json</artifactId>
89+
<version>1.11.0</version>
90+
<scope>test</scope>
91+
</dependency>
92+
93+
<!-- JUnit 4 for Maven Plugin Testing Harness -->
94+
<dependency>
95+
<groupId>junit</groupId>
96+
<artifactId>junit</artifactId>
97+
<version>4.13.2</version>
98+
<scope>test</scope>
99+
</dependency>
100+
</dependencies>
101+
</dependencyManagement>
102+
103+
<build>
104+
<pluginManagement>
105+
<plugins>
106+
<!-- Maven Shade Plugin for uber JAR in batch-runner -->
107+
<plugin>
108+
<groupId>org.apache.maven.plugins</groupId>
109+
<artifactId>maven-shade-plugin</artifactId>
110+
<version>3.5.0</version>
111+
</plugin>
112+
113+
<!-- Maven Flatten Plugin for publishing -->
114+
<plugin>
115+
<groupId>org.codehaus.mojo</groupId>
116+
<artifactId>flatten-maven-plugin</artifactId>
117+
<version>1.6.0</version>
118+
</plugin>
119+
</plugins>
120+
</pluginManagement>
121+
</build>
122+
123+
</project>

packages/maven/project.json

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,12 @@
1818
"{workspaceRoot}/scripts/copy-readme.js",
1919
"{workspaceRoot}/scripts/readme-fragments"
2020
],
21-
"outputs": ["{projectRoot}/README.md"],
22-
"dependsOn": [
23-
"^build",
24-
"typecheck",
25-
"legacy-post-build",
26-
"build-base",
27-
"build-native",
28-
"maven-batch-runner:package"
29-
]
21+
"outputs": ["{projectRoot}/README.md"]
3022
},
3123
"legacy-post-build": {
3224
"executor": "@nx/workspace-plugin:legacy-post-build",
3325
"outputs": ["{options.outputPath}"],
26+
"dependsOn": ["maven-batch-runner:package"],
3427
"options": {
3528
"tsConfig": "./tsconfig.lib.json",
3629
"outputPath": "packages/maven/dist",

0 commit comments

Comments
 (0)