maven-Spring Boot/Angular 2/4项目war构建

maven-Spring Boot/Angular 2/4项目war构建,angular,maven,spring-boot,war,Angular,Maven,Spring Boot,War,我有一个Spring Boot和Angular 2/4项目,我想将其打包成一个WAR并在Tomcat上使用。我有一个包含2个模块的父项目(1个用于Angular,1个用于Spring Boot),具有以下pom.xml配置: parent/pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://

我有一个Spring Boot和Angular 2/4项目,我想将其打包成一个WAR并在Tomcat上使用。我有一个包含2个模块的父项目(1个用于Angular,1个用于Spring Boot),具有以下pom.xml配置:

parent/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>

<groupId>mk.edu.ukim.feit.bolt</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

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

<modules>
    <module>api</module>
    <module>frontend</module>
</modules>

4.0.0
mk.edu.ukim.feit.bolt
教程和我最终成功地从父目录构建了运行mvn clean install的项目,然后进入api目录并运行mvn spring boot:run,应用程序在localhost:8080上运行,然而,在构建war文件时,Maven不知何故没有获取从Angular编译的静态文件,并给了我一个404。有人能告诉我这里可能出了什么问题吗

我还尝试将“prod”NPM脚本设置为“ng build--prod--base href=\”/\”,认为它可能需要做些什么,但没有帮助。有什么想法吗?

我找到的解决方案:

  • 将单个“index.html”文件复制到“src/main/resources/templates” 服务器项目下的文件夹。请注意,HTML文件结构必须非常严格(所有打开的标签必须关闭等)
  • 添加maven依赖项“spring boot starter thymeleaf”
  • 为请求映射“/”到“索引”模板创建HomeController
  • 样本:

    @Controller
    public class HomeContoller {
        @RequestMapping("/")
        public String index() {
            return "index";
        }
    }
    

    如果有人知道更干净/更好的解决方案并能与您分享,那将是一件非常棒的事情

    <?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>
    
    <artifactId>api</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <name>Bolt API</name>
    <description>Bolt messaging app REST API</description>
    <packaging>war</packaging>
    
    <parent>
        <groupId>mk.edu.ukim.feit.bolt</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </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-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
        <!--</dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${h2.version}</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.6.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.9</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>mk.edu.ukim.feit.bolt</groupId>
            <artifactId>frontend</artifactId>
            <version>${project.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
    @Controller
    public class HomeContoller {
        @RequestMapping("/")
        public String index() {
            return "index";
        }
    }