Skip to content

Commit f8b11ad

Browse files
nigredo-torimuuki88
authored andcommitted
Assorted JlinkPlugin improvements (#1242)
* Use sys.props("java.home") if javaHome is not defined (JlinkPlugin) * Add Ignore.byPackagePrefix (JlinkPlugin) * Add jlinkModulePath (JlinkPlugin)
1 parent a281550 commit f8b11ad

File tree

4 files changed

+65
-12
lines changed

4 files changed

+65
-12
lines changed

src/main/scala/com/typesafe/sbt/packager/archetypes/jlink/JlinkKeys.scala

+3
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ private[packager] trait JlinkKeys {
2929

3030
val jlinkBuildImage =
3131
TaskKey[File]("jlinkBuildImage", "Runs jlink. Yields the directory with the runtime image")
32+
33+
val jlinkModulePath =
34+
TaskKey[Seq[File]]("jlinkModulePath", "Module path to supply to jlink")
3235
}

src/main/scala/com/typesafe/sbt/packager/archetypes/jlink/JlinkPlugin.scala

+35-7
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ object JlinkPlugin extends AutoPlugin {
8989
module
9090
}.distinct
9191
},
92+
jlinkModulePath := (jlinkModulePath ?? Nil).value,
93+
jlinkModulePath ++= {
94+
fullClasspath.in(jlinkBuildImage).value.map(_.data)
95+
},
9296
jlinkOptions := (jlinkOptions ?? Nil).value,
9397
jlinkOptions ++= {
9498
val modules = jlinkModules.value
@@ -97,7 +101,11 @@ object JlinkPlugin extends AutoPlugin {
97101
sys.error("jlinkModules is empty")
98102
}
99103

100-
JlinkOptions(addModules = modules, output = Some(target.in(jlinkBuildImage).value))
104+
JlinkOptions(
105+
addModules = modules,
106+
output = Some(target.in(jlinkBuildImage).value),
107+
modulePath = jlinkModulePath.value
108+
)
101109
},
102110
jlinkBuildImage := {
103111
val log = streams.value.log
@@ -129,25 +137,28 @@ object JlinkPlugin extends AutoPlugin {
129137
.pair(file => IO.relativize(dir, file))
130138

131139
private def runJavaTool(jvm: Option[File], log: Logger)(exeName: String, args: Seq[String]): ProcessBuilder = {
132-
val exe = jvm match {
133-
case None => exeName
134-
case Some(jh) => (jh / "bin" / exeName).getAbsolutePath
135-
}
140+
val jh = jvm.getOrElse(file(sys.props.getOrElse("java.home", sys.error("no java.home"))))
141+
val exe = (jh / "bin" / exeName).getAbsolutePath
136142

137143
log.info("Running: " + (exe +: args).mkString(" "))
138144

139145
Process(exe, args)
140146
}
141147

142148
private object JlinkOptions {
149+
@deprecated("1.3.24", "")
143150
def apply(addModules: Seq[String] = Nil, output: Option[File] = None): Seq[String] =
151+
apply(addModules = addModules, output = output, modulePath = Nil)
152+
153+
def apply(addModules: Seq[String], output: Option[File], modulePath: Seq[File]): Seq[String] =
144154
option("--output", output) ++
145-
list("--add-modules", addModules)
155+
list("--add-modules", addModules) ++
156+
list("--module-path", modulePath)
146157

147158
private def option[A](arg: String, value: Option[A]): Seq[String] =
148159
value.toSeq.flatMap(a => Seq(arg, a.toString))
149160

150-
private def list(arg: String, values: Seq[String]): Seq[String] =
161+
private def list[A](arg: String, values: Seq[A]): Seq[String] =
151162
if (values.nonEmpty) Seq(arg, values.mkString(",")) else Nil
152163
}
153164

@@ -204,5 +215,22 @@ object JlinkPlugin extends AutoPlugin {
204215
val nothing: ((String, String)) => Boolean = Function.const(false)
205216
val everything: ((String, String)) => Boolean = Function.const(true)
206217
def only(dependencies: (String, String)*): ((String, String)) => Boolean = dependencies.toSet.contains
218+
219+
/** This matches pairs by their respective ''package'' prefixes. This means that `"foo.bar"`
220+
* matches `"foo.bar"`, `"foo.bar.baz"`, but not `"foo.barqux"`. Empty
221+
* string matches anything.
222+
*/
223+
def byPackagePrefix(prefixPairs: (String, String)*): ((String, String)) => Boolean = {
224+
case (a, b) =>
225+
prefixPairs.exists {
226+
case (prefixA, prefixB) =>
227+
packagePrefixMatches(prefixA, a) && packagePrefixMatches(prefixB, b)
228+
}
229+
}
230+
231+
private def packagePrefixMatches(prefix: String, s: String): Boolean =
232+
prefix.isEmpty ||
233+
s == prefix ||
234+
s.startsWith(prefix + ".")
207235
}
208236
}

src/sphinx/archetypes/misc_archetypes.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,13 @@ The plugin analyzes the dependencies between packages using `jdeps`, and raises
5050
"foo.bar" -> "bar.qux"
5151
)
5252
53-
For large projects with a lot of dependencies this can get unwieldy. You can implement a more flexible ignore strategy:
53+
For large projects with a lot of dependencies this can get unwieldy. You can use a more flexible ignore strategy:
5454

5555
.. code-block:: scala
5656
57-
jlinkIgnoreMissingDependency := {
58-
case ("foo.bar", dependee) if dependee.startsWith("bar") => true
59-
case _ => false
60-
}
57+
jlinkIgnoreMissingDependency := JlinkIgnore.byPackagePrefix(
58+
"foo.bar" -> "bar"
59+
)
6160
6261
Otherwise you may opt out of the check altogether (which is not recommended):
6362

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.typesafe.sbt.packager.archetypes.jlink
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
import JlinkPlugin.Ignore.byPackagePrefix
5+
6+
class JlinkSpec extends FlatSpec with Matchers {
7+
"Ignore.byPackagePrefix()" should "match as expected for sample examples" in {
8+
byPackagePrefix("" -> "")("foo" -> "bar") should be(true)
9+
10+
byPackagePrefix("foo" -> "bar")("foo" -> "bar") should be(true)
11+
byPackagePrefix("foo" -> "bar")("bar" -> "foo") should be(false)
12+
byPackagePrefix("foo" -> "bar")("baz" -> "bar") should be(false)
13+
byPackagePrefix("foo" -> "bar")("foo" -> "baz") should be(false)
14+
15+
byPackagePrefix("foo" -> "bar")("foobaz" -> "barqux") should be(false)
16+
byPackagePrefix("foo" -> "bar")("foo.baz" -> "bar.qux") should be(true)
17+
byPackagePrefix("foo.baz" -> "bar.qux")("foo" -> "bar") should be(false)
18+
19+
byPackagePrefix("" -> "", "foo" -> "bar")("baz" -> "qux") should be(true)
20+
byPackagePrefix("foo" -> "bar", "" -> "")("baz" -> "qux") should be(true)
21+
byPackagePrefix("foo" -> "", "" -> "bar")("baz" -> "qux") should be(false)
22+
}
23+
}

0 commit comments

Comments
 (0)