perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.64k stars 1.56k forks source link

Creating spark application jar #1040

Closed vlio20 closed 5 years ago

vlio20 commented 5 years ago

Hi, I am trying to create a runnable jar for the application, here is how I set my maven file (only relevant part):

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>oogaday.ApiServerKt</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>

I am building with mvn package and then I am running java -jar target/org.oogaday.api-1.0-SNAPSHOT-jar-with-dependencies.jar in order to run the app.

All endpoints are served as expected, the only problem is with the static files: when trying to access the static files the following stack trace is thrown:

java.lang.NullPointerException
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at java.io.FilterInputStream.read(FilterInputStream.java:107)
        at spark.utils.IOUtils.copyLarge(IOUtils.java:186)
        at spark.utils.IOUtils.copy(IOUtils.java:159)
        at spark.staticfiles.StaticFilesConfiguration.consumeWithFileResourceHandlers(StaticFilesConfiguration.java:102)
        at spark.staticfiles.StaticFilesConfiguration.consume(StaticFilesConfiguration.java:70)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:94)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1568)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
        at org.eclipse.jetty.server.Server.handle(Server.java:564)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
        at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
        at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
        at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
        at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
        at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
        at java.lang.Thread.run(Thread.java:745)
        Suppressed: java.lang.NullPointerException
                at java.io.FilterInputStream.close(FilterInputStream.java:181)
                at sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream.close(JarURLConnection.java:108)
                at spark.staticfiles.StaticFilesConfiguration.consumeWithFileResourceHandlers(StaticFilesConfiguration.java:103)
                ... 19 more

Any idea why this can happen? (I have unzipped the jar and I see the static files in the resources folder as expected)

vladp commented 5 years ago

did you set static files location as per http://sparkjava.com/documentation#static-files

vlio20 commented 5 years ago

Sure, it works as expected when running via intellij

vladp commented 5 years ago

I cannot help with maven, but I have no problems creating fatjars using gradle shadow plugin. And then using files from the resource folder.

There are number of reported bugs with sparkjava, where multiple jetty servers cannot be spawned with their own static folders (due to spark using global/static variables for directory config, and then not isolating them per thread). However I doubt that you are running into that issue yet.

perwendel commented 5 years ago

@vlio20 The problem doesn't seem to be due to multiple jetty servers sharing the same static folder (which has been fixed and will be available in next release).

It's hard to say anything without getting more information on the code configuring the static files location and the structure of that folder etc.

Orfeous commented 5 years ago

@vlio20

I use Jolira one jar for that. Works like a charm for me.

<build>
    <plugins>
        <plugin>
            <groupId>com.jolira</groupId>
            <artifactId>onejar-maven-plugin</artifactId>
            <version>1.4.4</version>
            <executions>
                <execution>
                    <configuration>
                        <mainClass>hu.orfeous.demo.Application</mainClass>
                    </configuration>
                    <goals>
                        <goal>one-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>