1

I want to build WAR and then run JUnit tests on that WAR. Is this possible?

I have following in my pom. What changes I should make in order to achieve?

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>${maven-war-plugin-version}</version>
    <configuration>
        <webXml>WEB-INF/web.xml</webXml>
        <webResources>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <targetPath>WEB-INF/classes</targetPath>
            </resource>
            <resource>
                <directory>${basedir}/src/main/sqls</directory>
                <targetPath>WEB-INF/classes</targetPath>
            </resource>
        </webResources>
        <outputFileNameMapping>@{groupId}@.@{artifactId}@-@{version}@.@{extension}@</outputFileNameMapping>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${maven-surefire-plugin-version}</version>
    <configuration>
        <argLine>-Xms256m -Xmx512m -XX:MaxMetaspaceSize=256m -XX:GCTimeRatio=19
            -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=4
            -XX:NewRatio=2 ${jvm.args.preferences} ${jvm.args.jacoco}</argLine>
    </configuration>
</plugin>
dur
  • 15,689
  • 25
  • 79
  • 125
Sandeep
  • 663
  • 2
  • 8
  • 18
  • 1
    If you want to perform tests on the packaged project, you should write integration-tests. – Tunaki Jun 15 '16 at 15:09
  • Use [maven-failsafe-plugin](https://maven.apache.org/surefire/maven-failsafe-plugin/) instead of maven-surefire cause what you are doing is integration testing and not unit testing. Apart from that i would suggest to put the integration tests into a separate module. And then your integration tests will run after packaging (using `mvn clean verify`). – khmarbaise Jun 15 '16 at 15:09
  • 5
    Possible duplicate of [Maven - deploy webapp to tomcat before JUnit test](http://stackoverflow.com/questions/16935290/maven-deploy-webapp-to-tomcat-before-junit-test) – Tunaki Jun 15 '16 at 15:13

1 Answers1

0

I don't think you need to change anything in your pom.xml.

There are 4 ways to use the WAR Plugin:

  1. using the package phase with the project package type as war
  2. invocation of the war:war goal
  3. invocation of the war:exploded goal
  4. invocation of the war:inplace goal

mvn compile war:war

or

mvn package -DskipTests -DskipITs

see: Apache Maven WAR Plugin

Mahmoud
  • 9,729
  • 1
  • 36
  • 47