0

I am trying to include my test classes in a generated JAR in a Maven Project.

I have created the Maven project and created the test classes with JUnit.

When I am trying to build the JAR, these test classes are not included in the generated JAR.

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Ravikiran S
  • 9
  • 1
  • 3
  • testclasses should not be part of the jar. – Jens Apr 09 '18 at 09:15
  • 3
    You can package test classes as a separate `test-jar`, see here: https://stackoverflow.com/questions/7751860/how-do-i-include-a-dependencys-test-jar-into-a-maven-projects-deployment – lexicore Apr 09 '18 at 09:17
  • A jar is allowed to have only source code, resources, configuration and manifest files. – Manmohan_singh Apr 09 '18 at 09:45
  • I've slightly improved the grammar and removed the maven-plugin tag, as the documentation for that tag says to only use it for questions relating to the development of Maven plugins. – DaveyDaveDave Apr 09 '18 at 20:46
  • The requirement is to run test classes in Jmeeter. We need a Jar file with test classes as well. We got the solution with assembly file. – Ravikiran S Apr 12 '18 at 09:41

2 Answers2

1

You can produce a jar which will include your test classes and resources. Please refer maven official site - https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html

<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>
Ramesh Fadatare
  • 561
  • 4
  • 12
0

I have resolved issue be adding things. We will get two jars. One with classes and other with test classes.

assembly.xml

<?xml version="1.0" encoding="UTF-8"?> 
<assembly 
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi chemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd <http://maven.apache.org/xsd/assembly-1.1.3.xsd> "> 

    <id>fat-tests</id> 
    <formats> 
        <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
        <dependencySet> 
            <outputDirectory>/</outputDirectory> 
            <useProjectArtifact>true</useProjectArtifact> 
            <unpack>true</unpack> 
            <scope>test</scope> 
        </dependencySet> 
    </dependencySets> 
    <fileSets> 
        <fileSet> 
            <directory>${project.build.directory}/test-classes</directory> 
            <outputDirectory>/</outputDirectory> 
            <includes> 
                <include>**/*.class</include> 
            </includes> 
            <useDefaultExcludes>true</useDefaultExcludes> 
        </fileSet> 
    </fileSets> 
</assembly>

POM.xml

<build> 
        <plugins> 
            <plugin> 
                <artifactId>maven-assembly-plugin</artifactId> 
                <version>2.3</version> 
                <configuration> 
                    <descriptor>src/main/assembly.xml</descriptor> 
                </configuration> 
                <executions> 
                    <execution> 
                        <id>make-assembly</id> 
                        <phase>package</phase> 
                        <goals> 
                            <goal>single</goal> 
                        </goals> 
                    </execution> 
                </executions> 
            </plugin> 

             <plugin> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-surefire-plugin</artifactId> 
                <configuration> 
                    <skip>false</skip> 
                </configuration> 
            </plugin> 

        </plugins> 
    </build> 
Ravikiran S
  • 9
  • 1
  • 3