Skip to content

Commit 10243af

Browse files
oheger-boschsschuberth
authored andcommittedFeb 25, 2025
feat(Maven): Add a function to detect Tycho projects
Tycho [1] is an extension for Maven that supports building artifacts specific to OSGi and the Eclipse platform. The current Maven package manager implementation is not able to extract the dependencies of such projects correctly. Upcoming commits will add a package manager implementation that can handle such projects. The new function is going to be used to distinguish between the projects to be handled by the Maven or the Tycho implementation. To simplify reusing functionality in tests between these two package manager implementations, move a helper function to a new test `Extensions.kt` file. [1]: https://github.com/eclipse-tycho/tycho Signed-off-by: Oliver Heger <oliver.heger@bosch.io>
1 parent 08bc01a commit 10243af

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
 

‎plugins/package-managers/maven/src/main/kotlin/utils/MavenSupport.kt

+22
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,28 @@ private fun createContainer(): PlexusContainer {
622622
}
623623
}
624624

625+
/** The namespace for the Maven Tycho build extension. */
626+
private const val TYCHO_NAMESPACE = "org.eclipse.tycho"
627+
628+
/** The ID for the Maven Tycho build extension. */
629+
private const val TYCHO_ID = "tycho-build"
630+
631+
/** The path to the subfolder containing core extensions for Maven. */
632+
internal const val EXTENSIONS_PATH = ".mvn/extensions.xml"
633+
634+
/**
635+
* Return *true* if the given [file] points to a Maven Tycho project. The [file] can either reference the project
636+
* folder directly or a file within the project folder.
637+
*/
638+
internal fun isTychoProject(file: File): Boolean {
639+
val root = file.takeIf { it.isDirectory } ?: file.parentFile
640+
641+
return root?.resolve(EXTENSIONS_PATH)?.takeIf { it.isFile }?.let { extFile ->
642+
val content = extFile.readText()
643+
TYCHO_NAMESPACE in content && TYCHO_ID in content
644+
} == true
645+
}
646+
625647
/**
626648
* Convert this [RemoteRepository] to a repository in the format used by the Maven Repository System.
627649
* Make sure that all relevant properties are set, especially the proxy and authentication.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.packagemanagers.maven
21+
22+
import io.kotest.matchers.shouldBe
23+
24+
import java.io.File
25+
26+
import org.ossreviewtoolkit.plugins.packagemanagers.maven.utils.EXTENSIONS_PATH
27+
28+
/** The content of a Tycho extension file. */
29+
private const val TYCHO_EXTENSION_XML = """
30+
<extensions>
31+
<extension>
32+
<groupId>org.eclipse.tycho</groupId>
33+
<artifactId>tycho-build</artifactId>
34+
<version>4.0.0</version>
35+
</extension>
36+
</extensions>
37+
"""
38+
39+
/**
40+
* Add the required structures to this [File] to make it a Tycho project. Write the given [content] in the extensions
41+
* file.
42+
*/
43+
internal fun File.addTychoExtension(content: String = TYCHO_EXTENSION_XML) =
44+
with(resolve(EXTENSIONS_PATH)) {
45+
parentFile.mkdirs() shouldBe true
46+
writeText(content)
47+
}

‎plugins/package-managers/maven/src/test/kotlin/utils/MavenSupportTest.kt

+52
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.ossreviewtoolkit.plugins.packagemanagers.maven.utils
2121

2222
import io.kotest.core.spec.style.WordSpec
23+
import io.kotest.engine.spec.tempdir
2324
import io.kotest.matchers.shouldBe
2425

2526
import io.mockk.every
@@ -38,6 +39,8 @@ import org.eclipse.aether.repository.Proxy
3839
import org.eclipse.aether.repository.RemoteRepository
3940
import org.eclipse.aether.util.repository.AuthenticationBuilder
4041

42+
import org.ossreviewtoolkit.plugins.packagemanagers.maven.addTychoExtension
43+
4144
class MavenSupportTest : WordSpec({
4245
@Suppress("DEPRECATION") // For deprecated ArtifactRepository interface.
4346
"toArtifactRepository()" should {
@@ -156,4 +159,53 @@ class MavenSupportTest : WordSpec({
156159
}
157160
}
158161
}
162+
163+
"isTychoProject()" should {
164+
"return true if the Tycho extension is found" {
165+
val projectDir = tempdir()
166+
projectDir.addTychoExtension()
167+
168+
isTychoProject(projectDir) shouldBe true
169+
}
170+
171+
"return false if the extension file does not contain the Tycho extension" {
172+
val projectDir = tempdir()
173+
projectDir.addTychoExtension(
174+
"""
175+
<extensions>
176+
<extension>
177+
<groupId>org.eclipse.tycho</groupId>
178+
<artifactId>tycho-foo</artifactId>
179+
<version>4.0.0</version>
180+
</extension>
181+
</extensions>
182+
""".trimIndent()
183+
)
184+
185+
isTychoProject(projectDir) shouldBe false
186+
}
187+
188+
"return false if there is no extension file" {
189+
val projectDir = tempdir()
190+
val mvnDir = projectDir.resolve(".mvn")
191+
mvnDir.mkdirs()
192+
193+
isTychoProject(projectDir) shouldBe false
194+
}
195+
196+
"return false if there is no .mvn directory" {
197+
val projectDir = tempdir()
198+
199+
isTychoProject(projectDir) shouldBe false
200+
}
201+
202+
"return true for a pom file in a folder that has the Tycho extension" {
203+
val projectDir = tempdir()
204+
projectDir.addTychoExtension()
205+
206+
val pomFile = projectDir.resolve("pom.xml")
207+
208+
isTychoProject(pomFile) shouldBe true
209+
}
210+
}
159211
})

0 commit comments

Comments
 (0)
Please sign in to comment.