@@ -21,7 +21,10 @@ 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 .readLines().filter { it.isNotBlank() }.associateBy({ it.split(" " )[0 ] }, { it.split(" " )[1 ] })
24+ val cache = DEP_LOOKUP_CACHE_FILE
25+ .readLines()
26+ .filter { it.isNotBlank() }
27+ .associateBy({ it.split(" " )[0 ] }, { it.split(" " )[1 ] })
2528
2629 if (cache.containsKey(depsHash)) {
2730 return cache.get(depsHash)
@@ -104,10 +107,70 @@ fun resolveDependencies(depIds: List<String>, customRepos: List<MavenRepo> = emp
104107}
105108
106109
110+ internal fun buildPom (depIds : List <String >, customRepos : List <MavenRepo >): String {
111+ val depTags = depIds.map {
112+ val regex = Regex (" ^([^:]*):([^:]*):([^:@]*)(:(.*))?(@(.*))?\$ " )
113+ val matchResult = regex.find(it)
114+
115+ if (matchResult == null ) {
116+ System .err.println (" [ERROR] Invalid dependency locator: '${it} '. Expected format is groupId:artifactId:version[:classifier][@type]" )
117+ quit(1 )
118+ }
119+
120+ """
121+ <dependency>
122+ <groupId>${matchResult.groupValues[1 ]} </groupId>
123+ <artifactId>${matchResult.groupValues[2 ]} </artifactId>
124+ <version>${matchResult.groupValues[3 ]} </version>
125+ ${matchResult.groups[5 ]?.let { " <classifier>" + it.value + " </classifier>" } ? : " " }
126+ ${matchResult.groups[7 ]?.let { " <type>" + it.value + " </type>" } ? : " " }
127+ </dependency>
128+ """
129+ }
130+
131+ // see https://github.com/holgerbrandl/kscript/issues/22
132+ val repoTags = customRepos.map {
133+ """
134+ <repository>
135+ <id>${it.id} </id>
136+ <url>${it.url} </url>
137+ </repository>
138+ """
139+
140+ }
141+
142+ return """
143+ <?xml version="1.0" encoding="UTF-8"?>
144+ <project xmlns="http://maven.apache.org/POM/4.0.0"
145+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
146+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
147+
148+ <modelVersion>4.0.0</modelVersion>
149+
150+ <groupId>kscript</groupId>
151+ <artifactId>maven_template</artifactId>
152+ <version>1.0</version>
153+
154+ <repositories>
155+ <repository>
156+ <id>jcenter</id>
157+ <url>http://jcenter.bintray.com/</url>
158+ </repository>
159+ ${repoTags.joinToString(" \n " )}
160+ </repositories>
161+
162+ <dependencies>
163+ ${depTags.joinToString(" \n " )}
164+ </dependencies>
165+ </project>
166+ """
167+ }
168+
169+
107170// called by unit tests
108171object DependencyUtil {
109172 @JvmStatic
110173 fun main (args : Array <String >) {
111174 System .err.println (resolveDependencies(args.toList(), loggingEnabled = false ))
112175 }
113- }
176+ }
0 commit comments