Skip to content

Commit af05b47

Browse files
Make Project.dependencyManagement nullable (#93)
This will make the structure of the object more generalizable to publishing structures outside our internal repos. I also added a function to `Project` to expose just the artifact swap dependencies from the `dependencyManagement` field, which I think improves clarity at call sites.
1 parent 45877a3 commit af05b47

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

cli/src/main/kotlin/xyz/block/artifactswap/cli/di/ArtifactRemoverModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ fun artifactRemoverModules(application: KoinApplication, config: ArtifactRemover
3030
mavenDirectory = config.mavenLocalPath,
3131
xmlMapper = get(),
3232
ioContext = get<CoroutineDispatcher>(named("IO")),
33+
config = get(),
3334
)
3435
}
3536

cli/src/test/kotlin/xyz/block/artifactswap/cli/commands/BomPublishingCommandTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,6 @@ class BomPublishingCommandTest {
190190
val pushedPom = fakeArtifactoryEndpoints.pushedPoms.first()
191191
assertEquals("bom", pushedPom.artifactId)
192192
assertEquals("2.0.0", pushedPom.version)
193-
assertEquals(2, pushedPom.dependencyManagement.dependencies.dependency.size)
193+
assertEquals(2, pushedPom.dependencyManagement?.dependencies?.dependency?.size)
194194
}
195195
}

core/src/main/kotlin/xyz/block/artifactswap/core/download/services/ArtifactRepository.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ class RealArtifactRepository(
150150
objectMapper.writeValue(bomFile.toFile(), project)
151151
}
152152
}
153-
project.dependencyManagement.dependencies.dependency.map { dependency ->
153+
requireNotNull(project.dependencyManagement) {
154+
"BOM should declare all artifacts in <dependencyManagement> section, but none was found " +
155+
"for BOM version $bomVersion."
156+
}
157+
project.artifactDependencies(config.primaryArtifactsMavenGroup).map { dependency ->
154158
Artifact(
155159
groupId = dependency.groupId,
156160
artifactId = dependency.artifactId,

core/src/main/kotlin/xyz/block/artifactswap/core/maven/Project.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ data class Project(
1010
val artifactId: String,
1111
val version: String,
1212
val name: String,
13-
val dependencyManagement: DependencyManagement,
13+
val dependencyManagement: DependencyManagement? = null,
1414
@JacksonXmlProperty(isAttribute = true) val xmlns: String = "http://maven.apache.org/POM/4.0.0",
1515
@JacksonXmlProperty(isAttribute = true, localName = "xmlns:xsi")
1616
val xsi: String = "http://www.w3.org/2001/XMLSchema-instance",
@@ -19,10 +19,17 @@ data class Project(
1919
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd",
2020
val packaging: String = "pom",
2121
val modelVersion: String = "4.0.0",
22-
)
22+
) {
23+
fun artifactDependencies(artifactDepGroup: String): List<Dependency> {
24+
val depManagementDependencies = dependencyManagement?.dependencies?.dependency ?: emptyList()
25+
return depManagementDependencies.filter { it.groupId == artifactDepGroup }
26+
}
27+
}
2328

2429
data class DependencyManagement(val dependencies: Dependencies)
2530

2631
data class Dependencies(val dependency: List<Dependency>)
2732

33+
// Note, technically versions can be `null` in general POMs, but we are using this to check
34+
// actual versions of artifacts in a BOM, so they should be present
2835
data class Dependency(val groupId: String, val artifactId: String, val version: String)

core/src/main/kotlin/xyz/block/artifactswap/core/repository/LocalArtifactRepository.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import kotlinx.coroutines.launch
3131
import kotlinx.coroutines.withContext
3232
import org.apache.logging.log4j.kotlin.logger
3333
import org.slf4j.Logger
34+
import xyz.block.artifactswap.core.config.ArtifactSwapConfig
35+
import xyz.block.artifactswap.core.maven.Dependency
3436
import xyz.block.artifactswap.core.maven.Project
3537

3638
/** Information about an artifact installed locally with its project path and versions. */
@@ -116,6 +118,7 @@ class RealLocalArtifactRepository(
116118
private val ioContext: CoroutineContext,
117119
private val mavenDirectory: Path = DEFAULT_LOCAL_MAVEN_DIRECTORY,
118120
private val slf4jLogger: Logger? = null,
121+
private val config: ArtifactSwapConfig,
119122
) : LocalArtifactRepository {
120123

121124
companion object {
@@ -139,7 +142,8 @@ class RealLocalArtifactRepository(
139142
kotlinx.coroutines.coroutineScope {
140143
val (result, duration) =
141144
measureTimedValue {
142-
bom.dependencyManagement.dependencies.dependency
145+
bom
146+
.artifactDependencies(config.primaryArtifactsMavenGroup)
143147
.map { dependency ->
144148
async(ioContext) {
145149
// no version present means we won't be able to find an artifact
@@ -338,7 +342,8 @@ class RealLocalArtifactRepository(
338342
bomFile
339343
.inputStream()
340344
.use { xmlMapper.readValue<Project>(it) }
341-
.toInstalledProjects(baseGroupDir),
345+
.artifactDependencies(config.primaryArtifactsMavenGroup)
346+
.map { it.toInstalledProject(baseGroupDir) },
342347
)
343348
}
344349
} else {
@@ -415,15 +420,13 @@ class RealLocalArtifactRepository(
415420
}
416421
}
417422

418-
private fun Project.toInstalledProjects(baseGroupDir: Path): List<InstalledProject> {
419-
return dependencyManagement.dependencies.dependency.map {
420-
val projectRepositoryPath = baseGroupDir.resolve(artifactId)
421-
InstalledProject(
422-
projectPath = it.artifactId.artifactIdToProjectPath(),
423-
repositoryPath = projectRepositoryPath,
424-
versions = setOf(it.version),
425-
)
426-
}
423+
private fun Dependency.toInstalledProject(baseGroupDir: Path): InstalledProject {
424+
val projectRepositoryPath = baseGroupDir.resolve(artifactId)
425+
return InstalledProject(
426+
projectPath = artifactId.artifactIdToProjectPath(),
427+
repositoryPath = projectRepositoryPath,
428+
versions = setOf(version),
429+
)
427430
}
428431

429432
@OptIn(ExperimentalPathApi::class)

core/src/test/kotlin/xyz/block/artifactswap/core/module_selector/LocalArtifactRepositoryTest.kt

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import kotlinx.coroutines.test.runTest
1616
import org.junit.jupiter.api.BeforeEach
1717
import org.junit.jupiter.api.Test
1818
import org.junit.jupiter.api.io.TempDir
19+
import xyz.block.artifactswap.core.config.ArtifactSwapConfig
1920
import xyz.block.artifactswap.core.maven.Dependencies
2021
import xyz.block.artifactswap.core.maven.Dependency
2122
import xyz.block.artifactswap.core.maven.DependencyManagement
@@ -63,6 +64,8 @@ class LocalArtifactRepositoryTest {
6364
"features_open-tickets-v2_home_fake",
6465
"features_open-tickets-v2_home_impl",
6566
)
67+
private val testMavenGroup = "com.squareup.register.sandbags"
68+
private val config = ArtifactSwapConfig(primaryArtifactsMavenGroup = testMavenGroup)
6669

6770
@BeforeEach
6871
fun setUp() {
@@ -79,6 +82,7 @@ class LocalArtifactRepositoryTest {
7982
xmlMapper = xmlMapper,
8083
ioContext = EmptyCoroutineContext,
8184
mavenDirectory = fakeM2Root,
85+
config = config,
8286
)
8387
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(fakeBom)
8488
assertTrue(installedArtifacts.getOrThrow().isEmpty())
@@ -94,12 +98,12 @@ class LocalArtifactRepositoryTest {
9498
dependentProjects =
9599
listOf(
96100
Dependency(
97-
groupId = "com.squareup.register.sandbags",
101+
groupId = testMavenGroup,
98102
artifactId = "features_open-tickets-v2_create-ticket_impl-tablet",
99103
version = "asdf1132",
100104
),
101105
Dependency(
102-
groupId = "com.squareup.register.sandbags",
106+
groupId = testMavenGroup,
103107
artifactId = "features_open-tickets-v2_create-ticket_anvil-wiring",
104108
version = "a23kh1",
105109
),
@@ -111,6 +115,7 @@ class LocalArtifactRepositoryTest {
111115
xmlMapper = xmlMapper,
112116
ioContext = EmptyCoroutineContext,
113117
mavenDirectory = fakeM2Root,
118+
config = config,
114119
)
115120
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
116121
assertTrue(installedArtifacts.getOrThrow().isEmpty())
@@ -126,12 +131,12 @@ class LocalArtifactRepositoryTest {
126131
dependentProjects =
127132
listOf(
128133
Dependency(
129-
groupId = "com.squareup.register.sandbags",
134+
groupId = testMavenGroup,
130135
artifactId = fakeArtifactDirectoryNames[0],
131136
version = "asdf1132",
132137
),
133138
Dependency(
134-
groupId = "com.squareup.register.sandbags",
139+
groupId = testMavenGroup,
135140
artifactId = fakeArtifactDirectoryNames[1],
136141
version = "a23kh1",
137142
),
@@ -158,6 +163,7 @@ class LocalArtifactRepositoryTest {
158163
xmlMapper = xmlMapper,
159164
ioContext = EmptyCoroutineContext,
160165
mavenDirectory = fakeM2Root,
166+
config = config,
161167
)
162168
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
163169
assertTrue(installedArtifacts.getOrThrow().isEmpty())
@@ -173,12 +179,12 @@ class LocalArtifactRepositoryTest {
173179
dependentProjects =
174180
listOf(
175181
Dependency(
176-
groupId = "com.squareup.register.sandbags",
182+
groupId = testMavenGroup,
177183
artifactId = fakeArtifactDirectoryNames[0],
178184
version = "hash1",
179185
),
180186
Dependency(
181-
groupId = "com.squareup.register.sandbags",
187+
groupId = testMavenGroup,
182188
artifactId = fakeArtifactDirectoryNames[1],
183189
version = "hash2",
184190
),
@@ -194,6 +200,7 @@ class LocalArtifactRepositoryTest {
194200
xmlMapper = xmlMapper,
195201
ioContext = EmptyCoroutineContext,
196202
mavenDirectory = fakeM2Root,
203+
config = config,
197204
)
198205
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
199206
val expected =
@@ -211,17 +218,17 @@ class LocalArtifactRepositoryTest {
211218
dependentProjects =
212219
listOf(
213220
Dependency(
214-
groupId = "com.squareup.register.sandbags",
221+
groupId = testMavenGroup,
215222
artifactId = fakeArtifactDirectoryNames[0],
216223
version = "hash1",
217224
),
218225
Dependency(
219-
groupId = "com.squareup.register.sandbags",
226+
groupId = testMavenGroup,
220227
artifactId = fakeArtifactDirectoryNames[1],
221228
version = "hash2",
222229
),
223230
Dependency(
224-
groupId = "com.squareup.register.sandbags",
231+
groupId = testMavenGroup,
225232
artifactId = fakeArtifactDirectoryNames[2],
226233
version = "hash3",
227234
),
@@ -244,6 +251,7 @@ class LocalArtifactRepositoryTest {
244251
xmlMapper = xmlMapper,
245252
ioContext = EmptyCoroutineContext,
246253
mavenDirectory = fakeM2Root,
254+
config = config,
247255
)
248256
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
249257
val expected =
@@ -264,12 +272,12 @@ class LocalArtifactRepositoryTest {
264272
dependentProjects =
265273
listOf(
266274
Dependency(
267-
groupId = "com.squareup.register.sandbags",
275+
groupId = testMavenGroup,
268276
artifactId = fakeArtifactDirectoryNames[0],
269277
version = "hash1",
270278
),
271279
Dependency(
272-
groupId = "com.squareup.register.sandbags",
280+
groupId = testMavenGroup,
273281
artifactId = fakeArtifactDirectoryNames[1],
274282
version = "hash2",
275283
),
@@ -295,6 +303,7 @@ class LocalArtifactRepositoryTest {
295303
xmlMapper = xmlMapper,
296304
ioContext = EmptyCoroutineContext,
297305
mavenDirectory = fakeM2Root,
306+
config = config,
298307
)
299308
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
300309
val expected =
@@ -312,7 +321,7 @@ class LocalArtifactRepositoryTest {
312321
dependentProjects =
313322
listOf(
314323
Dependency(
315-
groupId = "com.squareup.register.sandbags",
324+
groupId = testMavenGroup,
316325
artifactId = fakeArtifactDirectoryNames[0],
317326
version = "hash1",
318327
)
@@ -333,6 +342,7 @@ class LocalArtifactRepositoryTest {
333342
xmlMapper = xmlMapper,
334343
ioContext = EmptyCoroutineContext,
335344
mavenDirectory = fakeM2Root,
345+
config = config,
336346
)
337347
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
338348
val expected =
@@ -350,12 +360,12 @@ class LocalArtifactRepositoryTest {
350360
dependentProjects =
351361
listOf(
352362
Dependency(
353-
groupId = "com.squareup.register.sandbags",
363+
groupId = testMavenGroup,
354364
artifactId = fakeArtifactDirectoryNames[0],
355365
version = "hash1",
356366
),
357367
Dependency(
358-
groupId = "com.squareup.register.sandbags",
368+
groupId = testMavenGroup,
359369
artifactId = fakeArtifactDirectoryNames[1],
360370
version = "hash2",
361371
),
@@ -371,6 +381,7 @@ class LocalArtifactRepositoryTest {
371381
xmlMapper = xmlMapper,
372382
ioContext = EmptyCoroutineContext,
373383
mavenDirectory = fakeM2Root,
384+
config = config,
374385
)
375386
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
376387
assertTrue(installedArtifacts.getOrThrow().all { it.projectPath.startsWith(":") })
@@ -386,12 +397,12 @@ class LocalArtifactRepositoryTest {
386397
dependentProjects =
387398
listOf(
388399
Dependency(
389-
groupId = "com.squareup.register.sandbags",
400+
groupId = testMavenGroup,
390401
artifactId = fakeArtifactDirectoryNames[0],
391402
version = "hash1",
392403
),
393404
Dependency(
394-
groupId = "com.squareup.register.sandbags",
405+
groupId = testMavenGroup,
395406
artifactId = fakeArtifactDirectoryNames[1],
396407
version = "hash2",
397408
),
@@ -403,12 +414,12 @@ class LocalArtifactRepositoryTest {
403414
dependentProjects =
404415
listOf(
405416
Dependency(
406-
groupId = "com.squareup.register.sandbags",
417+
groupId = testMavenGroup,
407418
artifactId = fakeArtifactDirectoryNames[0],
408419
version = "hash3",
409420
),
410421
Dependency(
411-
groupId = "com.squareup.register.sandbags",
422+
groupId = testMavenGroup,
412423
artifactId = fakeArtifactDirectoryNames[1],
413424
version = "hash4",
414425
),
@@ -439,6 +450,7 @@ class LocalArtifactRepositoryTest {
439450
xmlMapper = xmlMapper,
440451
ioContext = EmptyCoroutineContext,
441452
mavenDirectory = fakeM2Root,
453+
config = config,
442454
)
443455
val installedArtifacts = localArtifactRepository.getInstalledArtifacts(bom.getOrThrow())
444456
val expected =
@@ -457,7 +469,7 @@ class LocalArtifactRepositoryTest {
457469
): Result<Project> = runCatching {
458470
val mavenPom =
459471
Project(
460-
groupId = "com.squareup.register.sandbags",
472+
groupId = testMavenGroup,
461473
artifactId = "features_open-tickets-v2_create-ticket_impl-tablet",
462474
version = bomVersion,
463475
name = "features_open-tickets-v2_create-ticket_impl-tablet",

core/src/test/kotlin/xyz/block/artifactswap/core/publisher/BomPublisherTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ class BomPublisherTest {
146146
val pushedPom = fakeArtifactoryEndpoints.pushedPoms.first()
147147
assertEquals("bom", pushedPom.artifactId)
148148
assertEquals("1.0.0", pushedPom.version)
149-
assertEquals(2, pushedPom.dependencyManagement.dependencies.dependency.size)
149+
val depManagement =
150+
requireNotNull(pushedPom.dependencyManagement) {
151+
"Dependency management should not be null in published BOM, that's where declared versions of artifacts go!"
152+
}
153+
assertEquals(2, depManagement.dependencies.dependency.size)
150154
}
151155

152156
@Test

0 commit comments

Comments
 (0)