'I need to run the command based on the -DskipTests flag in pom.xml

We have an angular folder inside the java spring project.

I am using the maven build tool so if the -DskipTests flag is true I need to trigger the npm buildProdSkipTests command.

If it's false I need to trigger the npm buildProd command.

This is my pom.xml file

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>mbaasportal</artifactId>
<packaging>war</packaging>
<name>mbaasportal</name>
<parent>
    <groupId>xxxx</groupId>
    <artifactId>xxx</artifactId>
    <version>5.7.0.0</version>
</parent>
<properties>
    <middleware.version>[9.4.0.0, 9.4.0.999)</middleware.version>
    <maven.exec.skip>false</maven.exec.skip>
</properties>
<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <configuration>
                <skip>${maven.exec.skip}</skip>
            </configuration>
            <executions>
                <execution>
                    <id>npm-install</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                    <workingDirectory>${project.basedir}/src/main/webapp</workingDirectory>
                    <executable>npm</executable>
                    <arguments>
                        <argument>install</argument>
                    </arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>webpack-build</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                    <workingDirectory>${project.basedir}/src/main/webapp</workingDirectory>
                    <executable>npm</executable>
                    <arguments>
                        <argument>run</argument>
                        <argument>buildProd</argument>
                    </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>


Solution 1:[1]

Then you need to put the executions into two Maven profiles and activate them based on the property.

Solution 2:[2]

Use

<properties>
    <tests.skip>true</tests.skip>
</properties>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <skipTests>${tests.skip}</skipTests>
    </configuration>
</plugin>

mvn clean install -Dtests.skip

or for skip false you can use:

mvn -Dtests.skip=false

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J Fabian Meier
Solution 2