Maison  >  Questions et réponses  >  le corps du texte

Comment empaqueter des projets multi-modules Spring Boot ?

J'ai construit un projet simple multi-modules :
Structure :

Le pom.xml extérieur est le suivant

<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.scum</groupId>
<artifactId>demo-package</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>demo-controller</module>
    <module>demo-service</module>
</modules>
<build>
    <plugins>
        <plugin>
            <!-- The plugin rewrites your manifest -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration><!-- 指定该Main Class为全局的唯一入口 -->
            <mainClass>com.example.demo.DemoControllerApplication</mainClass>
            <layout>ZIP</layout>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
                </goals>
                <!--可以生成不含依赖包的不可执行Jar包-->
                <!-- configuration>
                  <classifier>exec</classifier>
                </configuration> -->
            </execution>
        </executions>
    </plugin>
</plugins>

</build>

</projet>

`
fichier web pom`<?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.example</groupId>
<artifactId>demo-controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-controller</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

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

</projet>
`

Peut exécuter le package et signaler qu'IndexService n'existe pas

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class DemoControllerApplication {
    @Autowired
    private IndexService indexService;

    public static void main(String[] args) {
        SpringApplication.run(DemoControllerApplication.class, args);
    }

    @RequestMapping(value = "")
    @ResponseBody
    public String index(){
        return indexService.Index();
    }
}
巴扎黑巴扎黑2702 Il y a quelques jours705

répondre à tous(1)je répondrai

  • 大家讲道理

    大家讲道理2017-05-27 17:43:23

    Ce n'est pas un problème de Spring-boot, c'est un problème de dépendance Maven

    Supposons que j'ai un projet maven violet

    Les crochets angulaires sont le type de projet, précédé du niveau du projet

    iot-cloud<pom>

    iot-http<jar>
    iot-mqtt<jar>
    iot-oauth<jar>
    iot-sql-service<jar>
    iot-restful<war>

    Il y a 5 sous-projets dans le cadre du projet total iot-cloud. Parmi eux, iot-restful est le projet d'entrée, qui est le projet de démarrage de Spring-boot. Il s'appuie sur les quatre autres projets pour fournir des services, vous le présentez donc. les quatre autres projets dans iot-resutful En tant que dépendance, l'installation de maven convient

    .

    Bien entendu, les cas de test ne sont pas packagés et la priorité de l'introduction des fichiers de configuration doit toujours être prise en compte

    
    iot-restful的pom.xml
    
    <dependencies>
            <dependency>
                <groupId>pri.somnus</groupId>
                <artifactId>iot-http</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>pri.somnus</groupId>
                <artifactId>iot-http</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>pri.somnus</groupId>
                <artifactId>iot-mqtt</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>pri.somnus</groupId>
                <artifactId>iot-oauth</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>pri.somnus</groupId>
                <artifactId>iot-sql-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>

    répondre
    0
  • Annulerrépondre