简体   繁体   中英

Maven - Load resources from JAR file?

I have a feeling I'm missing something critical when it comes to bundling resources in JAR files using Maven. My directory structure is the standard Maven one.

When using the IDE I reference the resource files as \main\resources\public\images\path and it works fine, but when Maven css js and images dont work and the site looks bad. If I look in the jar I find all the css, js and images. When I try to open the css link in a browser I get a 404 error. Here is my pom.xml https://shrib.com/#XGISjWGN7QRfa2EIZD4r . In eclipse it works fine. I use Spark.staticFiles.location("public"); to load resources.

I can't comment for Spark, however when you create an artifact (jar, fat or not), it merely doesn't contain main/resources folder (open the jar with tool like winrar and check yourself).

Another difficulty is that if you use something that works with File abstraction - its probably wrong inside the artifact. File describes a place on a filesystem, and you're trying to read something from the artifact.

Usually you can use inside the code that reads the resource:

getClass().getResourceAsStream("/public/impages/path/sample.js") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM