String Templates - How to escape them?


#1

Hi,

I’m writing a Maven Pluging with Kotlin. And it happend that maven way of handling string templates are exactly same as Kotlin one.
Therefore following code having compile error, because kotlin is trying to handle “${project.testOutputDirectory}” in the Parameter annotation.

Mojo(name = "mytest", defaultPhase = LifecyclePhase.PROCESS_SOURCES) public open class FooMojo : AbstractMojo() {

  Parameter(defaultValue = “{project.testOutputDirectory}", property = "outputDirectory") &nbsp;&nbsp;private var outputDirectory : String = ""</p><div> </div></code></pre><p> <span > </span> Is there any way to tell Kotlin to ignore this "<span>{project.testOutputDirectory}” and leave it for maven to handle it ?


#2

Just escape the '$' sign:

``

    Parameter(defaultValue = “${project.testOutputDirectory}”, property = “outputDirectory”)


#3

I've tried this before. by using "${foo}", both kotlin and maven will treat it as a normal string(no more injection from maven).


#4

This code works for me:

``

annotation class Foo(val s: String)

Foo("{}") // "{}" in the byte code
fun main(args: Array<String>) {
  println("{foo}") // prints {foo}
}

I think you should check whether this “$” in the annotation works in Maven at all, even from Java


#5

Sorry, my bad.

“${foo}”  is actually working and I managed to convert the plugin code (originally written in Java) to kotlin code.

Thanks for your help