Skip to content

Commit 8559317

Browse files
andreas-mauschholgerbrandl
authored andcommitted
Support dependencies with different types (pom instead of jar) (#158)
* extracted PomBuilder to make it testable * make Pom understand maven <type>
1 parent 6187591 commit 8559317

File tree

5 files changed

+106
-63
lines changed

5 files changed

+106
-63
lines changed

src/main/kotlin/kscript/app/DependencyUtil.kt

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
2121

2222
// Use cached classpath from previous run if present
2323
if (DEP_LOOKUP_CACHE_FILE.isFile()) {
24-
val cache = DEP_LOOKUP_CACHE_FILE.
25-
readLines().filter { it.isNotBlank() }.
26-
associateBy({ it.split(" ")[0] }, { it.split(" ")[1] })
24+
val cache = DEP_LOOKUP_CACHE_FILE.readLines().filter { it.isNotBlank() }.associateBy({ it.split(" ")[0] }, { it.split(" ")[1] })
2725

2826
if (cache.containsKey(depsHash)) {
2927
return cache.get(depsHash)
@@ -34,62 +32,6 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
3432
if (loggingEnabled) System.err.print("[kscript] Resolving dependencies...")
3533
var hasLoggedDownload = false
3634

37-
val depTags = depIds.map {
38-
val splitDepId = it.split(":")
39-
40-
if (!listOf(3, 4).contains(splitDepId.size)) {
41-
System.err.println("[ERROR] Invalid dependency locator: '${it}'. Expected format is groupId:artifactId:version[:classifier]")
42-
quit(1)
43-
}
44-
45-
"""
46-
<dependency>
47-
<groupId>${splitDepId[0]}</groupId>
48-
<artifactId>${splitDepId[1]}</artifactId>
49-
<version>${splitDepId[2]}</version>
50-
${if (splitDepId.size == 4) "<classifier>" + splitDepId[3] + "<classifier>" else ""}
51-
</dependency>
52-
"""
53-
}
54-
55-
// see https://github.com/holgerbrandl/kscript/issues/22
56-
val repoTags = customRepos.map {
57-
"""
58-
<repository>
59-
<id>${it.id}</id>
60-
<url>${it.url}</url>
61-
</repository>
62-
"""
63-
64-
}
65-
66-
val pom = """
67-
<?xml version="1.0" encoding="UTF-8"?>
68-
<project xmlns="http://maven.apache.org/POM/4.0.0"
69-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
70-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
71-
72-
<modelVersion>4.0.0</modelVersion>
73-
74-
<groupId>kscript</groupId>
75-
<artifactId>maven_template</artifactId>
76-
<version>1.0</version>
77-
78-
<repositories>
79-
<repository>
80-
<id>jcenter</id>
81-
<url>http://jcenter.bintray.com/</url>
82-
</repository>
83-
${repoTags.joinToString("\n")}
84-
</repositories>
85-
86-
<dependencies>
87-
${depTags.joinToString("\n")}
88-
</dependencies>
89-
</project>
90-
"""
91-
92-
9335
fun runMaven(pom: String, goal: String): Iterable<String> {
9436
val temp = File.createTempFile("__resdeps__temp__", "_pom.xml")
9537
temp.writeText(pom)
@@ -119,6 +61,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
11961
}).stdout.lines()
12062
}
12163

64+
val pom = buildPom(depIds, customRepos)
12265
val mavenResult = runMaven(pom, "dependency:build-classpath")
12366

12467

@@ -143,8 +86,8 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
14386

14487
System.err.println("[kscript] Generated pom file was:")
14588
pom.lines()
146-
// .map{it.prependIndent("[kscript] [pom] ")}
147-
.forEach { System.err.println(it) }
89+
// .map{it.prependIndent("[kscript] [pom] ")}
90+
.forEach { System.err.println(it) }
14891
quit(1)
14992
}
15093

@@ -167,4 +110,4 @@ object DependencyUtil {
167110
fun main(args: Array<String>) {
168111
System.err.println(resolveDependencies(args.toList(), loggingEnabled = false))
169112
}
170-
}
113+
}

src/main/kotlin/kscript/app/Pom.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package kscript.app
2+
3+
fun buildPom(depIds: List<String>, customRepos: List<MavenRepo>): String {
4+
val depTags = depIds.map {
5+
val regex = Regex("^([^:]*):([^:]*):([^:@]*)(:(.*))?(@(.*))?\$")
6+
val matchResult = regex.find(it)
7+
8+
if (matchResult == null) {
9+
System.err.println("[ERROR] Invalid dependency locator: '${it}'. Expected format is groupId:artifactId:version[:classifier][@type]")
10+
quit(1)
11+
}
12+
13+
"""
14+
<dependency>
15+
<groupId>${matchResult.groupValues[1]}</groupId>
16+
<artifactId>${matchResult.groupValues[2]}</artifactId>
17+
<version>${matchResult.groupValues[3]}</version>
18+
${matchResult.groups[5]?.let { "<classifier>" + it.value + "</classifier>"} ?: ""}
19+
${matchResult.groups[7]?.let { "<type>" + it.value + "</type>"} ?: ""}
20+
</dependency>
21+
"""
22+
}
23+
24+
// see https://github.com/holgerbrandl/kscript/issues/22
25+
val repoTags = customRepos.map {
26+
"""
27+
<repository>
28+
<id>${it.id}</id>
29+
<url>${it.url}</url>
30+
</repository>
31+
"""
32+
33+
}
34+
35+
return """
36+
<?xml version="1.0" encoding="UTF-8"?>
37+
<project xmlns="http://maven.apache.org/POM/4.0.0"
38+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
39+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
40+
41+
<modelVersion>4.0.0</modelVersion>
42+
43+
<groupId>kscript</groupId>
44+
<artifactId>maven_template</artifactId>
45+
<version>1.0</version>
46+
47+
<repositories>
48+
<repository>
49+
<id>jcenter</id>
50+
<url>http://jcenter.bintray.com/</url>
51+
</repository>
52+
${repoTags.joinToString("\n")}
53+
</repositories>
54+
55+
<dependencies>
56+
${depTags.joinToString("\n")}
57+
</dependencies>
58+
</project>
59+
"""
60+
}

src/test/kotlin/Tests.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,38 @@ class Tests {
4747
// }
4848
}
4949

50+
@Test
51+
fun parseAnnotWithTypeAndClassifier() {
52+
val pom = buildPom(listOf("org.javamoney:moneta:1.3", "org.javamoney:moneta:1.3:pom", "org.javamoney:moneta:1.3@pom"), emptyList())
53+
54+
val expected = "" +
55+
" <dependency>\n" +
56+
" <groupId>org.javamoney</groupId>\n" +
57+
" <artifactId>moneta</artifactId>\n" +
58+
" <version>1.3</version>\n" +
59+
" \n" +
60+
" \n" +
61+
" </dependency>\n" +
62+
" \n" +
63+
"\n" +
64+
" <dependency>\n" +
65+
" <groupId>org.javamoney</groupId>\n" +
66+
" <artifactId>moneta</artifactId>\n" +
67+
" <version>1.3</version>\n" +
68+
" <classifier>pom</classifier>\n" +
69+
" \n" +
70+
" </dependency>\n" +
71+
" \n" +
72+
"\n" +
73+
" <dependency>\n" +
74+
" <groupId>org.javamoney</groupId>\n" +
75+
" <artifactId>moneta</artifactId>\n" +
76+
" <version>1.3</version>\n" +
77+
" \n" +
78+
" <type>pom</type>\n" +
79+
" </dependency>"
80+
assertTrue(pom.contains(expected))
81+
}
5082

5183
@Test
5284
fun mixedDependencyCollect() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@file:DependsOn("org.javamoney:moneta:1.3@pom")
2+
3+
import org.javamoney.moneta.spi.MoneyUtils
4+
5+
println("getBigDecimal(1L): " + MoneyUtils.getBigDecimal(1L))

test/test_suite.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ assert "kscript i_do_not_exist.kts 2>&1" "[kscript] [ERROR] Could not read scrip
7878
## make sure that it runs with remote URLs
7979
assert "kscript https://raw.githubusercontent.com/holgerbrandl/kscript/master/test/resources/url_test.kts" "I came from the internet"
8080

81+
## there are some dependencies which are not jar, but maybe pom, aar, ..
82+
## make sure they work, too
83+
assert "kscript ${KSCRIPT_HOME}/test/resources/depends_on_with_type.kts" "getBigDecimal(1L): 1"
8184

8285
# repeated compilation of buggy same script should end up in error again
8386
assert_raises "kscript '1-'; kscript '1-'" 1
@@ -130,7 +133,7 @@ assert "resolve_deps log4j:log4j:9.8.76" "false"
130133
## wrong format should exit with 1
131134
assert "resolve_deps log4j:1.0" "false"
132135

133-
assert_stderr "resolve_deps log4j:1.0" "[ERROR] Invalid dependency locator: 'log4j:1.0'. Expected format is groupId:artifactId:version[:classifier]"
136+
assert_stderr "resolve_deps log4j:1.0" "[ERROR] Invalid dependency locator: 'log4j:1.0'. Expected format is groupId:artifactId:version[:classifier][@type]"
134137

135138
## other version of wrong format should die with useful error.
136139
assert_raises "resolve_deps log4j:::1.0" 1

0 commit comments

Comments
 (0)