当前位置: 首页 > 知识库问答 >
问题:

SpringBoot:运行多模块项目

韩明德
2023-03-14

我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行的JAR文件。是一个带有Spring Boot的多模块项目,该项目将有3个模块。这里是父模块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>

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

    <groupId>com.tdkcloud</groupId>
    <artifactId>tdk-cloud</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>   
        <module>tdk-core</module>
        <module>tdk-batch</module>
        <module>tdk-web</module>
    </modules>


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

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>uk.co.jemos.podam</groupId>
            <artifactId>podam</artifactId>
            <version>7.0.5.RELEASE</version>
            <scope>test</scope>
        </dependency>


        <!-- Logging dependencies -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>

        <!-- Email dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

        <!-- Security dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- Spring data -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>



    </dependencies>

</project>

这里是模块核心

<?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.tdkcloud</groupId>
        <artifactId>tdk-cloud</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </parent>


  <groupId>com.tdkcloud.core</groupId>
  <artifactId>tdk-core</artifactId>
   <packaging>jar</packaging>    



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

  <dependencies>


        <!--  Hibernate dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.googlecode.libphonenumber</groupId>
            <artifactId>libphonenumber</artifactId>
            <version>8.4.3</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <!-- <version>1.10</version> -->
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <!-- <version>2.9.0.pr3</version> -->
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <!-- <version>2.9.0.pr3</version> -->
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>       
        </dependency>

        <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.9</version>
</dependency>


    </dependencies>

</project>

这里是模块web:

<?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.tdkcloud</groupId>
        <artifactId>tdk-cloud</artifactId>
        <version>0.0.2-SNAPSHOT</version>
    </parent>

  <groupId>com.tdkcloud.web</groupId>
  <artifactId>tdk-web</artifactId>
  <packaging>jar</packaging>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <start-class>com.tdkcloud.TdkCloudApplication</start-class>
  </properties>

  <dependencies>

        <!-- tdk-core dependencies -->
         <dependency>
            <groupId>com.tdkcloud.core</groupId>
            <artifactId>tdk-core</artifactId>
            <version>0.0.2-SNAPSHOT</version>           
        </dependency> 

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

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


        <!-- Webjars for JQuery and Bootstrap -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.7-1</version>
        </dependency> 
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.2.0</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
            <!-- <version>3.0.2.RELEASE</version> -->
        </dependency>


   </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.tdkcloud.TdkCloudApplication</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>  

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


</project>

从父根目录,我使用以下方法生成所有模块:

mvn clean package

但问题是tdk-web-0.0.2-SNAPSHOT。jar不包含tdk-core-0.0.2-SNAPSHOT。jar,然后在启动时失败

下面是maven的结果:

MacBook-Pro-de-nunito:tdk-cloud calzada$ mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] tdk-cloud
[INFO] tdk-core
[INFO] tdk-batch
[INFO] tdk-web
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building tdk-cloud 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ tdk-cloud ---
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building tdk-core 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.h2database:h2:jar:1.4.194 is missing, no dependency information available
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ tdk-core ---
[INFO] Deleting /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-core/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tdk-core ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tdk-core ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 52 source files to /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-core/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tdk-core ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tdk-core ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ tdk-core ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ tdk-core ---
[INFO] Building jar: /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-core/target/tdk-core-0.0.2-SNAPSHOT.jar
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building tdk-batch 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ tdk-batch ---
[INFO] Deleting /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-batch/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tdk-batch ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-batch/src/main/resources
[INFO] skip non existing resourceDirectory /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-batch/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tdk-batch ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-batch/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tdk-batch ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tdk-batch ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ tdk-batch ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ tdk-batch ---
[INFO] Building jar: /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-batch/target/tdk-batch-0.0.2-SNAPSHOT.jar
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building tdk-web 0.0.2-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ tdk-web ---
[INFO] Deleting /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-web/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ tdk-web ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 339 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ tdk-web ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 25 source files to /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-web/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ tdk-web ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ tdk-web ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ tdk-web ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ tdk-web ---
[INFO] Building jar: /Users/calzada/Development/J2EE/workspace-sts-3.8.4.RELEASE/tdk-cloud/tdk-web/target/tdk-web-0.0.2-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.3.RELEASE:repackage (default) @ tdk-web ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] tdk-cloud ....................................... SUCCESS [  0.105 s]
[INFO] tdk-core ........................................ SUCCESS [  1.634 s]
[INFO] tdk-batch ....................................... SUCCESS [  0.114 s]
[INFO] tdk-web ......................................... SUCCESS [  1.506 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.663 s
[INFO] Finished at: 2017-05-30T05:37:04+02:00
[INFO] Final Memory: 47M/539M
[INFO] ------------------------------------------------------------------------
MacBook-Pro-de-nunito:tdk-cloud calzada$ 

这是我得到的错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field emailService in com.tdkcloud.web.controllers.AppErrorController required a bean of type 'com.tdkcloud.backend.service.EmailService' that could not be found.


Action:

Consider defining a bean of type 'com.tdkcloud.backend.service.EmailService' in your configuration.

我解压缩了jar,没有核心模块的类

将建议的代码添加到模块web:

<plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>install</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

我有下一个错误:错误解析模板/tdk/login/login”,模板可能不存在,或者任何配置的模板资源都无法访问模板


共有3个答案

荀金鹏
2023-03-14

我认为您的@ComponentScan注释丢失了,或者可能是错误的。(例如:@ComponentScan({“com.tdkcloud.web”})在com中找不到服务。TDK云。后端软件包)。或者,您的EmailService中可能缺少@Service annotation。

所以要解决这个问题,您的代码需要如下所示:

应用程序。java(或Webconfig.java)

@Configuration
@EnableWebMvc
@ComponentScan({"com.tdkcloud"})
@SpringBootApplication
public class App {
   ...
}

电子邮件服务。Java语言

public interface EmailService {
   ...
}

EmailServiceImpl。Java语言

@Service
public class EmailServiceImpl {
   ...
}
戚阳
2023-03-14

最近,我尝试将我的项目结构/布局之一实现为maven多模块项目。

我遵循了下面的spring指南链接,希望这将帮助您和其他官方spring多模块项目链接

邓丰
2023-03-14
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

我建议创建一个可执行Jar。

 类似资料:
  • 问题内容: 这是一个基本问题,我只是不太熟悉Maven多模块结构。说,我有一个Web应用程序。我想将一些模块连接到它(某些服务)。我是否需要仅将其中一个模块(依赖于其他模块)制作为一个Web应用程序,然后运行它?一开始我以为我可以运行整个项目,但是这个选项在我的IDE中是无效的(我现在正在使用NetBeans),这使我认为我应该运行一个类似于主模块的东西(在这种情况下为Web应用程序) )。是这样

  • 父项目为demo-mybatis子模块为mybatis-dao、demo-service。demo-mybatis pom.xml如下: 4.0.0 pom mybatis-DAO Demo-service Demo-mybatis-app org.springframework.Boot spring-boot-starter-parent 2.3.2.release com.example D

  • 本文向大家介绍SpringBoot+Maven 多模块项目的构建、运行、打包实战,包括了SpringBoot+Maven 多模块项目的构建、运行、打包实战的使用技巧和注意事项,需要的朋友参考一下 本篇文章主要介绍了SpringBoot+Maven 多模块项目的构建、运行、打包,分享给大家,具体如下: 项目使用的工具: IntelliJ IDEA JDK 1.8 apache-maven-3.3.9

  • 我有一个带有myapp-core和myapp-web模块的myapp父pom类型maven项目。myapp-core模块作为依赖项添加到MyApp-Web。 myapp-core模块中的所有类都位于根包com.myapp.core中,而myapp-web模块中的所有类都位于根包com.myapp.web中 主要的application.java也在com.myapp.web包中。由于我的核心模块根

  • 我在教程两个模块的帮助下创建了一个springboot mutimodule项目--一个后端(java类),另一个forntend(angular 5应用程序)我在后端模块中包含了前端模块的依赖项。我正在创建一个jar使用maven资源插件。我也在将静态资源复制到pom.xml中构建目录的静态文件夹中。我还有一个@Controller返回“index”。当我运行jar时,我希望看到index.ht

  • 我的maven项目结构: 我的问题是,我只需要运行父模块,它自动实例化子模块(tomcat服务器)。spring boot多模块项目有可能吗? 注意:我使用的是STS。父模块打包是pom,子模块打包是jar文件。 我的父pom文件 我的问题是如何使用Tomcat运行父项目?如果我运行父项目,子模块是否自动运行? 提前感谢!!!