Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot how to make project run on server in eclipse

I am new to Spring Boot, I have sample spring boot code in my Eclipse IDE.

Now to run the project. In project there is class "Application", I run that using Run As Java Application and then Its running on given port.

But I want it to run using Run on Server option of Eclipse, so whenever I try to run that on server it gives me 404.

Please give me any idea to resolve this issue.

Code:

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

@Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        System.out.println(Arrays.toString(beanNames));
    }
}

application.properties

server.address=localhost
server.port=8080
server.contextPath=/spring-security-cas
app.service.security=http://localhost:8080/j_spring_cas_security_check
app.service.home=http://localhost:8080/

pom.xml

<?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>org.esco.demo</groupId>
    <artifactId>spring-security-cas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo-spring-security-cas</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>1.2.1.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <!-- Usefull for accessing to jsp -->
    <dependency>
        <groupId>org.springframework.boot</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>
    <!-- END Usefull for accessing to jsp -->
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.8</version>
    </dependency>
</dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>org.esco.demo.ssc.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

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

</project>

Note: Project running when run Application as Run as Java Application, problem is I want it to run using Run on Server option.

like image 865
Kamini Avatar asked Jan 06 '23 05:01

Kamini


1 Answers

If you want to deploy it to a container, instead of using the embedded container follow this section in the reference guide.

In short the steps are

  1. Use war packaging instead of jar packaging
  2. Let your Application class extends SpringBootServletInitializer and implement the configure method.
  3. Mark the container dependencies (tomcat I guess) as provided

So in a nutshell

  1. Change <packaging>jar</packaging> to <packaging>war</packaging>
  2. Change your Application

    public class Application extends SpringBootServletInitializer {
        ...
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Application.class);
        }
    }
    
  3. Add tomcat dependency as provided.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifcatId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

For more elaborate information check the reference guide.

like image 74
M. Deinum Avatar answered Jan 07 '23 17:01

M. Deinum