Integrate Spring Boot in an EAR project

前端 未结 3 1281
傲寒
傲寒 2021-01-02 19:55

I have an existing war project created using spring boot. How to package it within an EAR which has an EJB module?

Is there any way to move the model and dao package

相关标签:
3条回答
  • 2021-01-02 20:08

    I have created a multi module gradle project including a spring RESTFul web services - EAR application name is - bluestone bluestone/settings.gradle -

    rootProject.name = 'bluestone'
    include ':bluestone-web'
    include ':bluestone-core'
    include ':bluestone-rest'
    
    project (':bluestone-web').projectDir = new File('bluestone-web')
    project (':bluestone-core').projectDir = new File('bluestone-core')
    project (':bluestone-rest').projectDir = new File('bluestone-rest')

    bluestone-rest project structure is -

    bluestone-rest/build.gradle

    plugins {
        id 'war'
    }
    
    group 'com.bluestone.smart.rest'
    version '1.0-SNAPSHOT'
    
    
    
    dependencies {
        compile library.spring_context
        compile library.spring_web
        compile library.spring_beans
        compile library.spring_mvc
        providedCompile library.servlet_api
        testCompile library.junit
    
    }

    all the dependencies are imported from common libraries.gradle. common libraries.gradle is user ear bluestone/libraries.gradle

    /* ============================================================================
       Library definitions for project 'Bluestone'
       ============================================================================
       Define here library dependencies and use them inside sub-modules build.gradle.
    
       Included from: "${rootProject.projectDir}/build.gradle"
       ============================================================================
    
     */
    ext {
    
        library = [
                /* testing */
                junit: "junit:junit:4.12",
                log4j: "log4j:log4j:1.2.17",
    
                /* Spring libraries*/
                spring_context:					"org.springframework:spring-context:${spring_lib_version}",
                spring_aop:						"org.springframework:spring-aop:${spring_lib_version}",
                spring_beans:					"org.springframework:spring-beans:${spring_lib_version}",
                spring_orm:						"org.springframework:spring-orm:${spring_lib_version}",
                spring_web:						"org.springframework:spring-web:${spring_lib_version}",
                spring_mvc:                     "org.springframework:spring-webmvc:${spring_lib_version}",
                servlet_api:                     "javax.servlet:javax.servlet-api:4.0.1"
    
        ]
    }

    Within bluestone-rest, i have created three basic file to test a sample rest message -

    1. spring Configuration named - BlueRestConfiguration.java

    package com.bluestone.smart.rest.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScans;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = {"com.bluestone.smart.rest.resources", "com.bluestone.smart.rest.controller"})
    public class BlueRestConfiguration {
    }

    1. Initialization file - named is - RestInit.java

    package com.bluestone.smart.rest.config;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    import javax.servlet.ServletContext;
    
    public class RestInit extends AbstractAnnotationConfigDispatcherServletInitializer {
        /**
         * Specify {@code @Configuration} and/or {@code @Component} classes for the
         * {@linkplain #createRootApplicationContext() root application context}.
         *
         * @return the configuration for the root application context, or {@code null}
         * if creation and registration of a root context is not desired
         */
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return new Class[] {BlueRestConfiguration.class};
        }
    
        /**
         * Specify {@code @Configuration} and/or {@code @Component} classes for the
         * {@linkplain #createServletApplicationContext() Servlet application context}.
         *
         * @return the configuration for the Servlet application context, or
         * {@code null} if all configuration is specified through root config classes.
         */
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return null;
        }
    
        /**
         * Specify the servlet mapping(s) for the {@code DispatcherServlet} &mdash;
         * for example {@code "/"}, {@code "/app"}, etc.
         *
         * @see #registerDispatcherServlet(ServletContext)
         */
        @Override
        protected String[] getServletMappings() {
            return new String[] {"/"};
        }
    }

    1. A rest service API - named - GreetingsController.java

    package com.bluestone.smart.rest.resources;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class GreetingsController {
        @RequestMapping(path = "/greeting", method = RequestMethod.GET)
        public String greetings(){
            return "Welcome Spring Rest!";
        }
    }

    finally build this EAR application using -

    gradlew clean build 

    and deploy on WildFly application and then calling this service using postman -

    Please let me know if any more details required. I shall push this code on git and will share the git link here.

    0 讨论(0)
  • 2021-01-02 20:21

    You have to use the dependency management system.

    It allows you to set the parent of Spring Boot WAR module project to the different from the spring-boot-starter-parent. Then it's would be possible to include the WAR project into the EAR one in the same way like any other.

    <dependencyManagement>
         <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    ... now you can use all the Spring Boot starter dependencies in the usual way:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>
    

    The starter dependencies you've to specify at the module projects level, while the dependency management configuration may be specified at the both ones - around the whole EAR projects or individually on each of them, depending on the app requirements.

    Using Spring Boot without the parent POM

    0 讨论(0)
  • 2021-01-02 20:21

    You need a parent project that includes a war project, which will be your spring boot project, and an ear project just for making your ear.

    Parent will need to have the spring boot as its parent :

    <?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>
    
     <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
      </parent>
    
      <groupId>com.greg</groupId>
      <artifactId>ear-example</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>pom</packaging>
    
      <properties>
            <myproject.version>1.0-SNAPSHOT</myproject.version>
      </properties>
    
      <name>ear-example</name>
      <modules>
        <module>example-ear</module>
        <module>example-war</module>
      </modules>
    
    </project>
    

    Your ear project is:

    <?xml version="1.0"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>com.greg</groupId>
        <artifactId>ear-example</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>example-ear</artifactId>
      <packaging>ear</packaging>
    
      <dependencies>
        <dependency>
          <groupId>com.greg</groupId>
          <artifactId>example-war</artifactId>
          <version>${project.version}</version>
          <type>war</type>
        </dependency>
      </dependencies>
    
      <build>
       <plugins>
         <plugin>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.10.1</version>
            <configuration>
                    <modules>
                            <webModule>
                                    <groupId>com.greg</groupId>
                                    <artifactId>example-war</artifactId>
                                    <contextRoot>/appname</contextRoot>
                            </webModule>
                    </modules>
            </configuration>
          </plugin>
        </plugins>
      </build>
    
    </project>
    
    0 讨论(0)
提交回复
热议问题