0

I have maven project with main and test subfolders under folder src. However, when I build maven jar, I dont see test files in jar file (I do see files that were there in src/main; I dont see test files from src/test)

 [xx@localhost target]$ jar tvf cbm.jar | grep *Test*
 [xx@localhost target]$ 

Here is my pom.xml

  <build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
    </plugin>
</plugins>
<pluginManagement>
        <plugins>
            <plugin>
            <!-- https://maven.apache.org/plugins/maven-assembly-plugin/examples/index.html -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <finalName>afloat</finalName>
                 <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

EDIT @azurefrog: I am having issues in production and need to run the test cases on production machine.

kashili kashili
  • 955
  • 4
  • 15
  • 31
  • That is by design. Maven will not put classes from src/test/java into your jar file by default. – azurefrog May 29 '14 at 21:42
  • 2
    Why would it? Test classes aren't meant to be run by anyone in production; you're meant to use those to *test* with. – Makoto May 29 '14 at 21:43
  • 1
    http://stackoverflow.com/questions/12828416/generate-test-jar-along-with-jar-file-in-test-package shows how to have a second jar with the tests. – Joop Eggen May 29 '14 at 22:15

2 Answers2

0

It is normal behavior. Maven crate jar from src (only production code). If you want add test .class to jar you have to create your own maven plugin. Check this Guide for maven plugin development

anicos
  • 21
  • 2
0

Maven does not package test classes. What you would have to do is copy the maven project to the production machine and run mvn test from that machine.

The reason Maven behaves like this is that everything under src/test is not supposed to be used in production. It's test code, resources, etc. One common use of this is to put configuration files under src/test/resources such as Spring context files. If these files made it into the official jar file, it would be very bad in many, many cases.

That said, what you can do if you want to run test code is follow this process:

  1. Build a maven project that uses this one as a dependency.
  2. Write a some test code, either as JUnit with a standalone JUnit runner, or a simple class with a main method.
  3. Use the Maven assembly plugin to generate a jar that has all dependencies in it. This GitHub project of mine uses the assembly plugin to build an uberjar for a groovy script.
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83