Gradle将多个Spring MVC项目打包成一场战争

时间:2017-04-10 14:03:20

标签: java spring spring-mvc gradle war

我有两个spring mvc项目和struts项目(暂时忽略)。我正在使用servlet 3.1 java config来初始化我的应用程序上下文。 两个项目都通过应用gretty插件单独运行。 即

http://localhost:8080/project-1/

http://localhost:8080/project-2/project2(注意项目-2配置中的servlet映射)

到目前为止我尝试了什么。 Gradle multi project

rootproject
    |
    |-project-2
    |  src
    |  |-main
    |      |-java
    |      |    |-controllers
    |      |    |-config
    |      |
    |      |-webapp
    |--build.gradle
    |
    |-project-2
    |  src
    |  |-main
    |      |-java
    |      |    |-controllers
    |      |    |-config
    |      |
    |      |-webapp
    |--build.gradle
    |
    |-build.gradle
    |-settings.gradle

rootproject build.gradle

  buildscript {
    repositories {
        mavenCentral()

    }

}
apply plugin: 'war'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

allprojects {

    apply plugin: 'eclipse'
    apply plugin: 'eclipse-wtp'
    apply plugin: 'java'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8


    eclipse {
        classpath {
            containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
            containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
        }
    }

}

settings.gradle     包括' project-1',' project-2'

project-1 config

//WebServlet  java config
public class WebServletConfig implements WebApplicationInitializer{

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
      AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
        webCtx.register(SpringConfig.class);
        webCtx.setServletContext(servletContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(webCtx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");

  }

}

project-1 build.gradle

apply plugin: 'java'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

// In this section you declare where to find the dependencies of your project
repositories {
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // .. all required dpendencies
}

项目-2

//WebServlet  java config
public class WebServletConfig implements WebApplicationInitializer{

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
      AnnotationConfigWebApplicationContext webCtx = new AnnotationConfigWebApplicationContext();
        webCtx.register(SpringConfig.class);
        webCtx.setServletContext(servletContext);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(webCtx));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/project2/*");

  }

}

project-2 build.gradle

apply plugin: 'java'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

// In this section you declare where to find the dependencies of your project
repositories {
    jcenter()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // .. all required dpendencies
}

我想要实现的目标:

两个项目都打包成单个war文件。

和url as

http://localhost:8080/rootproject(此处为项目1)

http://localhost:8080/rootproject/project2(项目2在这里)

0 个答案:

没有答案