Spring Boot 如何制作Sping Boot 多模块项目的单个war文件

l0oc07j2  于 2023-06-22  发布在  Spring
关注(0)|答案(4)|浏览(93)

我的主要根pom和web模块pom和模型模块pom都在那里,所以如何使单个war文件,而不是每个模块的多个war文件。或者任何其他方法使war文件从这些jar生成,war文件包含application.properties和WEB-INF和META-INF文件夹与lib文件夹

主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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>web</module>
        <module>model</module>
    </modules>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.codemaster</groupId>
    <artifactId>college-management-system</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>college-management-system</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>13</java.version>
    </properties>

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

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


</project>

web模块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">
    <parent>
        <artifactId>college-management-system</artifactId>
        <groupId>com.codemaster</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.codemaster</groupId>
            <artifactId>model</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

模型模块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">
    <parent>
        <artifactId>college-management-system</artifactId>
        <groupId>com.codemaster</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>model</artifactId>
    <packaging>jar</packaging>

</project>

那么如何使每个模块只有单个警告文件而不是多个警告文件

wnrlj8wa

wnrlj8wa1#

您的多模块项目结构和POM应该是这样的
Parent - pom.xml -> Package pom
Module 1- pom.xml -> Package 罐
Module 2- pom.xml -> Package 罐
Web-pom.xml-> Package 战

父POM

<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>2.2.0.RELEASE</version>
   <relativePath /> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>application-base</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <modules>
   <module>module1</module>
   <module>module2</module>
   <module>web</module>
  </modules>
  <dependencies>
            ----
  </dependencies>
</project>

模块POM

<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>com.example</groupId>
     <artifactId>application-base</artifactId>
     <version>1.0.0</version>
    </parent>
     <packaging>jar</packaging>
     <artifactId>module</artifactId>
     <name>Module</name>
     <description>/description>
    <dependencies>
      ------
    </dependencies>
</project>

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>
   <parent>
   <groupId>com.example</groupId>
   <artifactId>application-base</artifactId>
   <version>1.0.0</version>
   </parent>
   <artifactId>web</artifactId>
   <name>web</name>
   <packaging>war</packaging>
   <description></description>
   <dependencies>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
     <groupId>com.example</groupId>
     <artifactId>module</artifactId>
     <version>1.0.0</version>
    </dependency>
   </dependencies>
   <build>
    <plugins>   
    </plugins>
   </build>
</project>
gudnpqoy

gudnpqoy2#

在你的例子中,你在web模块(pom.xml)中添加了一个jar的打包,将web模块打包改为war

旧代码

<packaging>jar</packaging>

新编码

<packaging>war</packaging>

请遵循下面的spring Boot maven多模块项目指南以获取更多详细信息

Spring官方文档

https://spring.io/guides/gs/multi-module/

Git hub spring Boot maven多模块示例

https://github.com/spring-guides/gs-multi-module.git

kyks70gy

kyks70gy3#

也许.... java ~~.war与classpath包括~~. jar文件

nx7onnlm

nx7onnlm4#

在本例中,简单的答案是需要将web模块pom.xml中的打包从jar更改为war

<packaging>war</packaging>

详细答案是:你需要parent.pom来描述所有的模块,你所要做的就是为每个模块指定打包,并将它们作为依赖项添加到你的 * WEB * 模块中。以下是父、Web和一个模块(核心)pom文件作为示例:

    • parent. 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-project</artifactId>
    <version>3.0</version>
    <packaging>pom</packaging>

    <modules>
        <module>core</module>
        <module>web</module>
        <module>api</module>
    </modules>
</project>
    • core. 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-project-core</artifactId>
    <version>3.0</version>
    <packaging>jar</packaging>

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

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

    <dependencies>
    </dependencies>
    <build>
    </build>
</project>
    • 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-project-web</artifactId>
    <version>3.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath /> 
    </parent>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo-project-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>demo-project-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</project>

相关问题