5

I am bundling a Vue UI with a Spark Java backend.
Both modules are built independently, which works fine with the following structure:

  project
  +-- backend
  |   +-- src
  |   |   +-- main
  |   |       +-- resources
  |   |           +-- public <= Where the jar is picking the static files
  |   +-- build
  |       +-- libs <= Gradle Jar output
  +-- ui
      +-- dist <= Vue build output

On the backend, Gradle is bundling backend/src/main/resources/public into the Jar /public. Hence I copied from ui/dist into backend/src/main/resources/public as a jar task dependency.

  task copyUI(type: Copy) {
      from( '../ui/dist')
      into( 'src/main/resources/public')
  }

  jar.dependsOn( copyUI)

Gradle is copying the files but after creating the jar.
In other words, I have to create the jar twice to get it right.

How can I instruct Gradle to wait the copy completion before packaging /public

My build.gradle jar section looks like this

jar {
    manifest {
        attributes(
                'Main-Class': 'tld.domain.MainClass'
        )
    }
    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    }
}
MonoThreaded
  • 11,429
  • 12
  • 71
  • 102
  • One thing I can think of is to add cp task to copy all the files in the `ui/dist` folder into `backend/src/main/resources` folder, do you have any sample project to play with it? – chenrui Apr 05 '18 at 17:13
  • @chenrui I did that but the result was somewhat random as if the jar was triggering the copy but not waiting for its completion – MonoThreaded Apr 05 '18 at 20:07
  • you can have task dependency to ensuring the ordering. – chenrui Apr 05 '18 at 20:12
  • @chenrui This is precisely what I tried. Can you please provide a response with an example? – MonoThreaded Apr 05 '18 at 20:27

1 Answers1

4

While what you are doing seems logical (and bug-free) to me, I don't see a reason for gradle to not wait until copying is done. May I suggest doing it little differently though.

You can directly instruct jar task to load files from ../ui/dist in the from block. This way, you won't have to actually copy anything to public dir.

jar {
    // ...
    from( '../ui/dist')
}

This is better as public can stay clean of generated code (via build of ui project) and you save time of copying (and potentially issue arising because of it).

and finally make jar task dependsOn you UI project's build task, so that latest dist is available in ../ui/dist

kdabir
  • 9,623
  • 3
  • 43
  • 45
  • Almost. All resources end up in the jar root directory. Can I specify a 'to' for this specific file set? – MonoThreaded Apr 16 '18 at 17:56
  • Did you try [`into`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:into(java.lang.Object)) – kdabir Apr 17 '18 at 04:25
  • Something like `jar { into('...') { from '....' }}`, notice the `from` inside the `into`'s block. may be see [this](https://stackoverflow.com/a/9359588/469414) answer – kdabir Apr 17 '18 at 13:36