2

As part of our Jenkins build, Jenkins uses Maven which in turn uses a pom.xml file at the root of our project. Right now it's pretty much a no-op:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dqsalpha-dev.app</groupId>
    <artifactId>dqsalpha</artifactId>
    <version>1</version>
</project>

I want to add one thing to this pom file - I want to run a test (it's just a shell script) when maven build is run.

What is the simplest way to add a test to the Maven build via the pom file?

Something like this:

  <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.dqsalpha-dev.app</groupId>
        <artifactId>dqsalpha</artifactId>
        <version>1</version>
        <test>
          <bash>
            @test.sh
          </bash>
        </test>
   </project>

except that's totally not correct. I just realize that I also probably need to install Node.js dependencies as well, with npm install.

I tried this:

https://bitbucket.org/atlassian/bash-maven-plugin

And then my pom.xml looks like:

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dqsalpha-dev.app</groupId>
    <artifactId>dqsalpha</artifactId>
    <version>1</version>
    <build>
        <plugins>
            <plugin>

                <groupId>com.atlassian.maven.plugins</groupId>
                <artifactId>bash-maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <executions>
                    <execution>
                        <id>test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <script>
                        npm install;
                        ./test.sh
                    </script>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <example.one>Variable replacement is available from Maven.</example.one>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.atlassian.maven.plugins</groupId>
            <artifactId>bash-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

but then I get this error:

$ mvn clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building dqsalpha 1
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for com.atlassian.maven.plugins:bash-maven-plugin:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.187 s
[INFO] Finished at: 2017-04-11T16:09:24-07:00
[INFO] Final Memory: 8M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Plugin com.atlassian.maven.plugins:bash-maven-plugin:1.0-SNAPSHOT or one of its dependencies could not be resolved: Could not find artifact com.atlassian.maven.plugins:bash-maven-plugin:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginResolutionException

2 Answers 2

6

You may use exec-maven-plugin:

<plugin>
    <artifactId>exec-maven-plugin</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <executions>
        <execution>
            <id>test1</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>${basedir}/test.sh</executable>
            </configuration>
        </execution>
    </executions>
</plugin>
3

I just realize that I also probably need to install Node.js dependencies as well, with npm install.

If what you're doing in test.sh is Node.js and npm stuff, you could instead use frontend-maven-plugin, which, despite its name, is designed to install and run Node.js and npm. Here's an example, but please see https://github.com/eirslett/frontend-maven-plugin for much better information.

<plugin>
  <groupId>com.github.eirslett</groupId>
  <artifactId>frontend-maven-plugin</artifactId>
  <version>1.9.1</version>

  <configuration>
    <nodeVersion>v12.16.0</nodeVersion>
  </configuration>

  <executions>
    <execution>
      <id>Install Node.js and npm</id>
      <goals>
        <goal>install-node-and-npm</goal>
      </goals>
    </execution>

    <execution>
      <id>npm install</id>
      <goals>
        <goal>npm</goal>
      </goals>
      <configuration>
        <workingDirectory>${your.npm.script.directory}</workingDirectory>
        <arguments>install</arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.