Tuesday, October 17, 2017

Creating test jar in Maven

My project is moduled as common, server, web, dao

For test cases I am using Random beans

Since the model objects are used across all the modules, to have a randomizer setup for each of these module is duplication.

So I am adding this randomizer related classes and configuration only at common module, and referencing at other module for test scope

At common :
<project>

  ...

  <build>

    <plugins>

      ...

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-jar-plugin</artifactId>

        <version>3.0.2</version>

        <executions>

          <execution>

            <goals>

              <goal>test-jar</goal>

            </goals>

          </execution>

        </executions>

      </plugin>

      ...

    </plugins>

  </build>

  ...

</project>



At dao :
<project>
  ...
  <dependencies>
    <dependency>
      <groupId>com.foo</groupId>
      <artifactId>common</artifactId>
      <type>test-jar</type>
      <version>version</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>


You can read more about this here

If you are building your project with maven.test.skip=true this test-jar is not generated. To overcome this use profile activation. i.e. at dao, add test-jar dependency only if the maven.test.skip != true

<profiles>

     <profile>

         <activation>

            <property>

               <name>maven.test.skip</name>

               <value>!true</value>

            </property>

         </activation>

         <dependencies>

            <dependency>

             <groupId>com.foo</groupId>

        <artifactId>common</artifactId>
        <type>test-jar</type>
        <version>version</version>
        <scope>test</scope>
        </dependency>

         </dependencies>

      </profile>

    </profiles>

No comments:

Post a Comment