Springboot+gradle building Multi-module projects

Source: Internet
Author: User
Tags mkdir jcenter

Springboot for the convenience of Web project development, this article will not repeat, mainly to the work based on Springboot and Gradle multi-module project construction experience to summarize and generalize. 1. Create a project

First installs Java and gradle, this article chooses Java version is 1.8.0_40, gradle version is 2.10, installs the process this article not to repeat.

Next, create the project with the name SPRINGBOOT-MM:
mkdir springboot-mm
CD springboot-mm
Gradle Init

The project structure at this point is as follows:

Next, create multiple modules, where the web and modules, for example, first create the relevant directory,
Linux:

Mkdir-p web/src/main/{java,resources} web/src/test/{java,resource}
mkdir-p modules/src/main/{java,resources} Modules/src/test/{java,resource}

Windows:

mkdir web\src\main\java,web\src\main\resource,web\src\test\java,web\src\test\resource
mkdir modules\src\main \java,modules\src\main\resource,modules\src\test\java,modules\src\test\resource

The directory structure at this time is as follows:
2. Modify the configuration

First, modify the settings.gradle file in the root directory to introduce the submodule:

Next, modify the Build.gradlein the root directory:

Generic configuration for all sub-projects subprojects {Apply plugin: ' java ' Apply plugin: ' idea ' Version = ' 1.0.0 '//JVM release number requirements sourcecompatibility = 1.8 Targetcompatibility = 1.8//Java compilation will fail by default due to Chinese characters [compilejava,compiletest java,javadoc]*.options*.encoding = ' UTF-8 ' repositories {mavenlocal () Maven {URL "http://maven.aliy Un.com/nexus/content/groups/public "} mavencentral () Jcenter () Maven {URL" http://repo.spring.io/ Snapshot "} maven {URL" Http://repo.spring.io/milestone "} maven {URL ' http://maven.springframework.org/
            Release '} maven {URL ' http://maven.springframework.org/milestone '}} jar {manifest {
    Attributes ("Implementation-title": "Gradle")}//Displays all jars for compile under the current project. Task Listjars (Description: ' Display all compile jars. ') << {configurations.compile.each {file file--p Rintln File.name}} Gradle.projectsevaluated {Tasks.withtype (javacompile) {Options.compilerargs <<-xlint:unchecked << "-xlint:deprecation"}}}

Next, import the project using IntelliJ (using version 2016.3) , and after the import is successful, it should be visible as shown below:

Add the web/build.gradle file and modify it to define the dependencies of the Web module:

Buildscript {
    repositories {
        mavenlocal ()
        maven {URL "http://maven.aliyun.com/nexus/content/groups/ Public "}
        maven {URL" http://repo.spring.io/snapshot "}
        maven {URL" http://repo.spring.io/milestone "}
        Jcenter ()
    }

    dependencies {
        classpath ("org.springframework.boot:spring-boot-gradle-plugin:1.4.5. RELEASE ")
    }
}

Apply plugin: ' org.springframework.boot '

archivesbasename = ' web '

ext {
    Springbootvar = ' 1.4.5.RELEASE '
}

dependencies {
    Compile project (': Modules ')

    //Spring boot
    compile "org.springframework.boot: Spring-boot-starter-web: $springBootVar "

    testcompile" org.springframework.boot:spring-boot-starter-test:$ Springbootvar "
}

Add the modules/build.gradle file, in this case modules is a Web-dependent module, usually can be similar ORM, payment, authentication and other functions into different modules, which can reduce the coupling of the program, easy to expand the business. Readers can modify the Modules/build.gradle according to their own project requirements, but need to add the following configuration to the Modules/build.gradle:

bootrepackage {
    enabled = False
}

For specific reasons, please refer here. 3. Add test Code

To add Web\src\main\java\com\formularoom\application.java, the sample code is as follows

@ComponentScan (basepackages = {"Com.formularoom"})
@EnableAutoConfiguration public
class application Extends Springbootservletinitializer implements Embeddedservletcontainercustomizer {public
    static void main ( String args[]) {
        springapplication.run (application.class, args);
    }

    @Override public
    void Customize (Configurableembeddedservletcontainer container) {
        Container.setport (8081 );
    }
}

Add Web\src\main\java\com\formularoom\controller\test1.java,

@Controller public
class Test1 {

    @RequestMapping (value = "/test1", method = Requestmethod.get)
    @ Responsebody public
    String test1 () {
        return "Spring Boot multiple modules test";
    }
}

Using a browser to access Http://127.0.0.1:8081/test1, the following is displayed:

Complete sample code, which can be obtained from here.

Exchange group number, 255489119.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.