-->

How to add test classes in JAR with Maven project

2019-08-20 12:23发布

问题:

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.

回答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>



回答2:

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>