Spring Boot with No Parent – Example

Normally, you need to use Spring Boot as parent. If you do not want to use its as parent, this page is for you.

This example is a part of  Spring Boot with all examples

Full Code with Spring Boot 1.5.9.RELEASE:

9_spring-boot-no-parent.zip

What you need for run this:

  • Maven 3
  • Java 8

What the code will do:

  • Show how to config Spring Boot as not parent
  • Also support JSP (most examples in internet do not have this)
  • Also support maven resource filter (again, most examples in internet do not care about this)

Explain:

This is the file structure:

Most of files are the same as Spring Boot with JSP and Spring Boot with Maven Resource Filter. If you want to see explanation, click in those links.

Only pom.xml is different, see the yellow lines here.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.surasint.example</groupId>
    <artifactId>spring-boot-09</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <springframework.boot.version>1.5.9.RELEASE</springframework.boot.version>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- JSP -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- jstl for jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${springframework.boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <database.url>jdbc:mysql://localhost/testdb</database.url>
            </properties>
        </profile>
        <profile>
            <id>production</id>
            <properties>
                <database.url>jdbc:mysql://production/realdb</database.url>
            </properties>
        </profile>
    </profiles>
</project>

The keys are these lines:

  • Line 14
<failOnMissingWebXml>false</failOnMissingWebXml>

  If you do not have this line, you will get this error message when you do ‘mvn clean package’

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring-boot-09: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executi
ng in update mode) -> [Help 1]
  • Line 16-27
 <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springframework.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

This is the main replacement as using Spring Boot as Parent.

  • Line 50
<version>${springframework.boot.version}</version>

Since this is not Spring Boot as Parent,  you have to explicit define the version for spring-boot-maven-plugin.

  • List 46-51
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

Since this is not Spring Boot as Parent,  you have to explicit define where the resource folder is, more detail here:

  • Line 56-63
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>

Since this is not Spring Boot as Parent,  you have to explicit define which goal to be hooked for spring-boot-maven-plugin. If you miss this, when you run this:

mvn clean package

java -jar target/spring-boot-09-1.0-SNAPSHOT.war

You will get this error message:

no main manifest attribute, in spring-boot-09-1.0-SNAPSHOT.war

That is because spring-boot-maven-plugin does not manifest file to point to the main class (when ‘mvn clean package’), so the war file will not be executable file.

  • Line 65-75:
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>

Since this is not Spring Boot as Parent,  you have to explicit define, what delimiter is. Normally maven uses “${}”, but in Spring Boot you should use “@”, more detail here:

Try:

run this in console:

mvn clean package

Then run this to start:

mvn clean spring-boot:run

You should see something like this:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.8.RELEASE)

.....
.....
2017-12-12 22:03:04.243  INFO 260 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.Annotation
ConfigEmbeddedWebApplicationContext@7595f8d7: startup date [Tue Dec 12 22:03:01 CET 2017]; root of context hierarchy

Open a browser and try to go to this url:

http://localhost:8080/testjsp

Then you should see this:

And, go to this url:

http://localhost:8080/api/show

You will get this:

Then stop the console by Ctrl + C (on Windows).

And start with this:

mvn clean spring-boot:run -Pproduction

Open a browswer and try to go to this url:

http://localhost:8080/api/show

You will get this: