首页 文章

如何将具有角度2前端(angular-cli构建)的spring-boot webapp部署到Tomcat?

提问于
浏览
4

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

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

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

但是,如果我构建角度应用程序,则resources / static中创建的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/下的文件 .

如果我不想在根目录下部署一个带有角度前端的spring-boot应用程序到tomcat服务器?

2 回答

  • 0

    请查看我使用'--deploy-url'参数的答案 . https://stackoverflow.com/a/43741602/5633515

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

  • 1

    我有同样的问题,首先我创建了一个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

    希望能帮助到你 .

相关问题