首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >maven - Spring Boot/角2/4项目战争构建

maven - Spring Boot/角2/4项目战争构建
EN

Stack Overflow用户
提问于 2017-09-03 15:21:41
回答 1查看 1.1K关注 0票数 5

我有一个Spring和Range2/4项目,我想把它打包成一个单独的WAR,并在Tomcat上服务。我有一个带有两个模块的父项目(一个用于角模块,一个用于Spring ),具有以下pom.xml配置:

父/体

代码语言:javascript
复制
<?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>

前端/头文件

代码语言:javascript
复制
<?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>frontend</artifactId>


<name>Bolt Client</name>
<description>Bolt messaging app frontend client</description>

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

<build>
    <plugins>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.4</version>

            <configuration>
                <nodeVersion>v7.9.0</nodeVersion>
                <npmVersion>4.6.1</npmVersion>
                <workingDirectory>src/main/frontend</workingDirectory>
            </configuration>

            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                </execution>

                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                </execution>

                <execution>
                    <id>npm run prod</id>

                    <goals>
                        <goal>npm</goal>
                    </goals>

                    <configuration>
                        <arguments>run prod</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

    <resources>
        <resource>
            <directory>target/frontend</directory>
            <targetPath>static</targetPath>
        </resource>
    </resources>
</build>

api/put.xml

代码语言:javascript
复制
<?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>

我遵循教程,最后成功地构建了从父目录运行mvn干净安装的项目,然后进入api目录并运行mvn spring-boot:run,应用程序在localhost:8080上提供服务,但是在构建war文件时,Maven不知怎么没有从http://localhost:8080上获取编译的静态文件,给了我一个404。有人能告诉我这里可能出了什么问题吗?

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

EN

回答 1

Stack Overflow用户

发布于 2021-08-12 09:11:51

我有一个类似的布局,其中的角度应用程序和webapp是在单独的maven模块。要在webapp中捆绑这个角应用程序,我执行以下操作:-

  • 使棱角应用程序文件生成在target/classes/static/frontend目录中。我使用architect.build.options部分中的angular.json文件中的outputPath参数来配置它。我还在这里配置了bashHrefdeployUrl设置。请注意,architect.serve.options是不同的,因此我可以使用ng服务进行调试和测试。 {“"./node_modules/@angular/cli/lib/config/schema.json",”:"$schema":1,"newProjectRoot":"projects","projects":{“scrapit2myob-正前方”:{ "projectType":“应用程序”,“根”:“projects/scrapit2myob-前端”,"sourceRoot":“projects/scrapit2myob-前端/src”,“架构师”:{“构建者”:{“构建者”:“@architect/build-角:浏览器”,“选项”:{ "aot":true,"outputPath":“目标/类/静态/前端”,“索引”:outputPath。"main":“projects/scrapit2myob-前端/src/main.ts”、"baseHref":“/前端/”、"deployUrl":“/前端/”、. }、. }、“服务”:{“构建者”:{“构建者”:“@angular/builder:dev-server”,“选项”:{ "browserTarget":“scrapit2myob-前端:build”,"proxyConfig":"proxy.config.json","baseHref":"/","deployUrl":"/“},.
  • 让maven将这个棱角应用程序打包成一个jar文件。所有编译的角文件都应该出现在该jar的静态/前端文件夹中。
  • 添加角app文件作为web应用程序项目的maven依赖项。由于角文件位于静态/前端文件夹中,默认情况下它们应该可以从web应用程序公开访问。
  • 在webapp中,我有一个WebMvcConfigurer来配置重定向。这是必要的,如果您有深入的链接在您的角度前端,如果用户刷新该链接从浏览器,您需要服务器为这些链接服务index.html。角度路由器然后负责渲染正确的组件..。 @Configuration public class WebMvcConfiguration实现WebMvcConfigurer {@覆盖公共ViewControllerRegistry(ViewControllerRegistry注册表){ registry.addViewController("/silent-refresh.html").setViewName("forward:/frontend/index.html");registry.addViewController("/receipts/**").setViewName("forward:/frontend/index.html");registry.addViewController("/tickets/**").setViewName("forward:/frontend/index.html");registry.addViewController("/supplier-mapping/**").setViewName("forward:/frontend/index.html");registry.addViewController("/item-mapping/**").setViewName("forward:/frontend/index.html");registry.addViewController("/account-mapping/**").setViewName("forward:/frontend/index.html");registry.addViewController("/job/**").setViewName("forward:/frontend/index.html");registry.addViewController("/login").setViewName("login");registry.addViewController("/swagger-ui").setViewName("redirect:/swagger-ui/index.html");registry.setOrder(Ordered.HIGHEST_PRECEDENCE);}
  • 我使用/静态/前端而不是仅仅使用/static的原因是,如果您要保护您的api,那么您可以配置匹配/frontend的urls以被spring安全过滤器忽略. 公共类ServerSecurityConfig扩展WebSecurityConfigurerAdapter { 公共空配置(WebSecurity web)抛出异常{web.ignoring.antMatchers(“/web.ignoring.antMatchers/**”); }}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46024913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档