如何使用Maven项目在JAR中添加测试类

时间:2018-04-09 09:12:51

标签: java maven maven-assembly-plugin

我正在尝试将我的测试类包含在Maven项目中生成的JAR中。

我创建了Maven项目并使用JUnit创建了测试类。

当我尝试构建JAR时,这些测试类不包含在生成的JAR中。

2 个答案:

答案 0 :(得分:1)

您可以生成一个包含测试类和资源的jar。 请参考maven官方网站 - 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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我已经解决了添加内容的问题。我们将获得两个罐子。一个是类,另一个是测试类。

<强> 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>