How to include CSS files of javafx project in grad

2019-08-07 05:16发布

问题:

I have a javafx project including css files. I want them directly in the main java src:

src/main/java/Foo.java
src/main/java/Foo.css

When I now use my gradle script to build a fat jar, it does not include the css files and running the jar ends up in a npe.

My gradle script looks like this:

apply plugin: 'java'
apply plugin: 'eclipse'

jar {
  manifest { 
    attributes "Main-Class": "foo.Main"
  }
  from {
    configurations.compile.collect {
      it.isDirectory() ? it : zipTree(it)
    }
    configurations.runtime.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
}

repositories {
  mavenCentral()
}

dependencies {
  compile 'com.github.sarxos:webcam-capture:0.3.10'
}

What can I do to get a runnable jar file from gradle? I tried already to explicitly include the css files - it did not work / I did it wrong ...

回答1:

If you can't change your resource files to a resource directory, then you can use:

jar {
  manifest { 
    attributes "Main-Class": "foo.Main"
  }
  from {
    configurations.compile.collect {
      it.isDirectory() ? it : zipTree(it)
    }
    configurations.runtime.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }

  from('src/main/java') {
     include '**/*.css'
  }

}