tm1701
tm1701

Reputation: 7581

Spring Boot application - build process with Angular does not update Angular part

My Spring Boot application contains a Java part and an Angular part. The Angular part (with an index.html,etc) is generated into the folder src/main/resources/static. Works fine. I noticed that when building the final JAR often (?) the static part is lost.

How come? What can I do to get the static / HTML part into the final Spring Boot JAR?

My <build> part of the pom.xml is shown below. All not-interesting parts (like unit testing, etc) is removed.

<build>
    ... 
    <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
      <execution>
        <id>npm install</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>initialize</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>install</argument>
          </arguments>
        </configuration>
      </execution>
      <execution>
        <id>build Angular production code</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>compile</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>run</argument>
            <argument>build</argument>
            <argument>--prod</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
  </plugin>
  ...
</build>

Upvotes: 1

Views: 1754

Answers (2)

Carlos Cavero
Carlos Cavero

Reputation: 3176

I am using validate phase to copy the static angular resources to spring:

mvn clean install

Using a parent pom to split angular from Spring project:

<modules>
    <module>phs-frontend</module>
    <module>phs-frontend-web</module>
</modules>

Where phs-frontend project is an angular project with a pom file and maven clean plugin:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <filesets>
                    <fileset>
                        <directory>dist</directory>
                        <includes>
                            <include>*</include>
                        </includes>
                    </fileset>
                </filesets>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v6.10.3</nodeVersion>
                        <npmVersion>5.6.0</npmVersion>
                    </configuration>
                </execution>

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

                <execution>
                    <id>npm run build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>

            </executions>
        </plugin>

    </plugins>
</build>

And the phs-frontend-web is the Spring project:

<build>
    <plugins>

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
        <outputDirectory>${project.build.outputDirectory}/static/</outputDirectory>
                        <encoding>UTF-8</encoding>
                        <nonFilteredFileExtensions>
                            <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
                            <nonFilteredFileExtension>woff</nonFilteredFileExtension>
                            <nonFilteredFileExtension>woff2</nonFilteredFileExtension>
                        </nonFilteredFileExtensions>
                        <resources>
                            <resource>
                                <directory>${project.basedir}/phs-frontend/dist</directory>
                                <filtering>true</filtering>
                                <includes>
                                    <include>**/*</include>
                                </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>           

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 1

tm1701
tm1701

Reputation: 7581

Thank you @Carlos Cavero! You pointed me in the right direction. Thank you so much!

The problem was that the npm build was done in the 'compile' phase. On some machines the 'compile' phase of the Java code was done before that 'compile' with the Angular 'npm build'.

The solution was to put the Angular (typescript) npm phase BEFORE the compile phase. That is: in the generate-resource phase. It could not be the 'validate' phase, because that one is done before the 'install' with the 'npm install'.

This is now the correct Angular / npm build phase.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
    <executions>
      <execution>
        <id>npm install</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>initialize</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>install</argument>
          </arguments>
        </configuration>
      </execution>
      <execution>
        <id>build Angular production code</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>generate-resources</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>run</argument>
            <argument>build</argument>
            <argument>--prod</argument>
          </arguments>
        </configuration>
      </execution>
    </executions>
 </plugin>

Upvotes: 2

Related Questions