Build a deployable jar in a multi-module project

Viewed 493

I am having a project structure like this:

parent/
  server/    - Spring Boot Application
    pom.xml
  client-1/  - Angular Web Application
    pom.xml
  client-2/  - Angular Web Application
    pom.xml
  pom.xml

I am able to build this application where the Spring Boot Application is a jar, which contains the two Angular clients in its static/ folder.

However, currently the Spring Boot Application's pom.xml has to know about the clients and I would like to move this to the parent.

Here I am lacking some Maven skills though.


This is what should happen:

  1. Build Web Clients
  2. Build Spring Boot Application
  3. Copy web clients to static/

I am currently using the maven-resources-plugin to copy the client content but I noticed that there is an issue - if the clients are not build already, then it will copy nothing into the final jar which means the web clients are missing for deployment.

Right now, this is the pom.xml of the parent:

<?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>mz</groupId>
    <artifactId>mz-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>mz-server</module>
        <module>mz-admin</module>
        <module>mz-web</module>
    </modules>

</project>

How can I accomplish this?


Below is how I am currently building the jar using maven-resources-plugin. But as stated, in this case the server project has to know about the clients and also this does not guarantee that the clients are getting build first.

<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>validate</phase>
            <goals><goal>copy-resources</goal></goals>
            <configuration>
                <outputDirectory>${build.directory}/classes/static/</outputDirectory >
                <resources>
                    <resource>
                        <directory>../mz-admin/dist</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
        <execution>
            <id>copy-web</id>
            <phase>validate</phase>
            <goals><goal>copy-resources</goal></goals>
            <configuration>
                <outputDirectory>${build.directory}/classes/static/</outputDirectory >
                <resources>
                    <resource>
                        <directory>../mz-web/dist</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
1 Answers

Add your client-1 and client-2 as dependency to your server project. That will ensure your clients are built before your server and the files are available.

Related