简体   繁体   中英

How can I deploy a spring-boot webapp with angular 2 frontend (angular-cli build) to Tomcat?

I have a spring-boot web application and I build my frontend with angular-cli. I have set the output directory of my angular build in angular-cli.json to resources/static and I have configured the build script in package.json like this:

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

In the application.properties of the spring-boot configuration I have set the server.contextPath to 'mywebapp'.

But if I build the angular application the included js files of the created index.html in resources/static does not contain the server context path 'mywebapp':

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

but I should look like this:

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

So if I deploy my spring-boot app to tomcat the built index.html is load but it cannot find the imported js files and tries to load the files under http://localhost:8080/ instead of http://localhost:8080/mywebapp/ .

How can I deploy a spring-boot application with an angular-frontend to a tomcat server if I don't want to deploy it under the root directory?

Please take a look at my answer for using '--deploy-url' parameter. https://stackoverflow.com/a/43741602/5633515

In your case, use --deploy-url="/mywebapp/"

I've had the same question, first I've created a pom.xml file and there I've used plugins to convert my package to a war file .

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

when deploying I've found some issues and good lectures.

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

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

hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM