10

I want to package a spring-boot application as jar, and I do so with mvn package.

This produces a jar which does not contain any /WEB-INF/jsp nor /src/main/webapp/resources.

How can I make sure my jar contains everything needed ?

Here my current pom.xml:

<modelVersion>4.0.0</modelVersion>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-samples</artifactId>
    <version>1.0.0.RC3</version>
</parent>

<packaging>jar</packaging>

<properties>
    <main.basedir>${basedir}/../..</main.basedir>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>

<dependencies>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1101-jdbc41</version>
    </dependency>

</dependencies>

<!-- Package as an executable JAR -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
        </plugin>
    </plugins>
</build>

<!-- Allow access to Spring milestones and snapshots -->
<!-- (you don't need this if you are using anything after 1.0.0.RELEASE) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>http://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>
<artifactId>com.example.app</artifactId>

5 Answers 5

16

The following example works with Spring Boot 1.3.3.RELEASE: https://github.com/ghillert/spring-boot-jsp-demo

The key is to put the static jsp content in:

/src/main/resources/META-INF/resources/WEB-INF/jsp

and ensure you define the view prefix/suffix in your application.properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
1
  • why does no tutorial mention this?
    – wutzebaer
    Commented Dec 13, 2019 at 15:05
6

Is there any reason why you can't use the war packaging type? https://maven.apache.org/plugins/maven-war-plugin/usage.html I would recommend to use the war packaging type and use default maven web-application structure.

If you really want to use the jar plugin for your webapp, you need to configure it for your project. Due to your posting, I don't understand your structure and can't give you an example. Check out the usage of jar plugin here:https://maven.apache.org/plugins/maven-war-plugin/usage.html

2
  • 3
    You need to use war packaging, but otherwise it all looks sane. See docs in JSP limitations.
    – Dave Syer
    Commented Mar 2, 2014 at 8:25
  • 4
    I have tried with war packaging, and it works. java -jar myfile.war looked a bit strange, but it works. Thanks !
    – yglodt
    Commented Mar 2, 2014 at 10:51
3

Change your build tag to

 <build>
<resources>
            <resource>
                <directory>${basedir}/src/main/webapp</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
3

I had the same problem you had. I tried the answer above that you marked correct but it didn't work for me.

This worked ... change the pom.xml to ...

<packaging>war</packaging>

... this will causes maven to create a WAR file that is executable like so ...

java -jar yourwarfile.war

I found this solution with this similar question here ...

WEB-INF not included in WebApp using SpringBoot, Spring-MVC and Maven

0
1

To create a runnable JAR with Spring Boot, use the spring-boot-maven-plugin in your pom.xml

<build>
<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
</plugins>
</build>

Maybe you want to have a look at my example application http://info.michael-simons.eu/2014/02/20/developing-a-web-application-with-spring-boot-angularjs-and-java-8/ (Source is on GitHub, App is live and runs from a JAR).

One thing that you should note: JSP from JAR doesn't work due to some embedded Tomcat problems. I'm using Thymeleaf. If you need JSP, stay with the WAR deployment.

Not the answer you're looking for? Browse other questions tagged or ask your own question.