将scala测试用例添加到maven项目中的jar文件中

时间:2017-01-12 12:04:56

标签: java scala maven build testcase

我想将scala测试用例包含在jar文件中。我有maven项目。 我使用了以下内容:

<assembly>
<id></id>
<formats>
    <format></format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>

<dependencySets>
    <dependencySet>
        <includes>
           <include>...</include>
        </includes>
    </dependencySet>
    <dependencySet>
        <outputDirectory></outputDirectory>
    </dependencySet>
</dependencySets>
<fileSets>
    <fileSet>
        <directory>${project.build.directory}/test/scala</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
            <include>**/*.class</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
</fileSets>

有人可以告诉我使用

将scala testcase添加到jar文件中的方法是什么
  

mvn clean package

感谢。

1 个答案:

答案 0 :(得分:1)

您需要创建一个包含测试类和外部依赖项的胖jar。 Maven Assembly Plugin是显而易见的选择:

插件示例:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
        <version>2.3</version>
            <configuration>
                <descriptor>src/main/assembly/assembly.xml</descriptor>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.example.MainTest</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    </execution>
            </executions>
</plugin>

您还需要在src\main\assembly文件夹中创建一个descriptor file assembly.xml文件,其中包含以下内容:

示例:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 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>

Build Jar:

mvn clean compile test-compile assembly:single

运行Jar:

java -jar fat-jar-include-tests.jar