【问题标题】:How can I deploy a spring-boot webapp with angular 2 frontend (angular-cli build) to Tomcat?如何将带有 angular 2 前端(angular-cli build)的 spring-boot webapp 部署到 Tomcat?
【发布时间】:2017-10-22 06:45:53
【问题描述】:

我有一个 spring-boot web 应用程序,我使用 angular-cli 构建我的前端。我已将 angular-cli.json 中的 Angular 构建的输出目录设置为 resources/static,并且我已在 package.json 中配置了构建脚本,如下所示:

"scripts" : {"build": "ng build --base-href /mywebapp", ...}

在 spring-boot 配置的 application.properties 中,我将 server.contextPath 设置为“mywebapp”。

但如果我构建 Angular 应用程序,资源/静态中创建的 index.html 的包含 js 文件不包含服务器上下文路径“mywebapp”:

<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>

但我应该是这样的:

<script type="text/javascript" src="/mywebapp/inline.bundle.js"></script>
<script type="text/javascript" src="/mywebapp/polyfills.bundle.js"></script>

因此,如果我将 spring-boot 应用程序部署到 tomcat,则构建的 index.html 已加载,但它找不到导入的 js 文件,并尝试加载 http://localhost:8080/ 而不是 http://localhost:8080/mywebapp/ 下的文件。

如果我不想在根目录下部署带有 angular-frontend 的 spring-boot 应用程序,如何将它部署到 tomcat 服务器?

【问题讨论】:

  • 很有趣,因为你在问怎么做,你实际上展示了怎么做,我找不到。您真正的问题是“如何将 Angular 和 Spring 部署到某个路径?”。

标签: spring angular tomcat spring-boot angular-cli


【解决方案1】:

请查看我对使用“--deploy-url”参数的回答。 https://stackoverflow.com/a/43741602/5633515

在你的情况下,使用--deploy-url="/mywebapp/"

【讨论】:

【解决方案2】:

我也有同样的问题,首先我创建了一个 pom.xml 文件,然后我使用插件将我的包转换为 war 文件。

<plugins>

  <!-- ############################## -->
  <!-- npm scripts                    -->
  <!-- ############################## -->

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>exec-project-dependencies-install</id>
        <phase>generate-sources</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>install</argument>
          </arguments>
        </configuration>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>

      <!-- run `ng build -prod -aot` -->
      <!-- * clean dist/ before generating distribution files -->
      <execution>
        <id>exec-compile</id>
        <phase>generate-sources</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>run</argument>
            <argument>ci</argument>
          </arguments>
        </configuration>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

  <!-- generate zip -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
      <descriptors>
        <descriptor>src/assembly/static.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
   <!--generate zip -->
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
      <descriptors>
        <descriptor>src/assembly/static.xml</descriptor>
      </descriptors>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

在部署时,我发现了一些问题和很好的讲座。

理论 - https://kosbr.github.io/2016/10/09/angular-build.html

问题 - https://github.com/angular/angular-cli/issues/4517 - https://github.com/angular/angular-cli/pull/4090

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2020-06-17
    • 1970-01-01
    • 2019-06-19
    • 2017-07-24
    • 2017-12-03
    • 2017-09-21
    • 1970-01-01
    • 2017-06-23
    相关资源
    最近更新 更多