【问题标题】:How can I include unit tests in maven assembly?如何在 Maven 程序集中包含单元测试?
【发布时间】:2010-11-04 06:48:21
【问题描述】:

原因:我们的项目使用 Ant 作为命令行界面。使用 maven 的程序集插件制作新程序集后,我想进行初始测试以查看是否所有组件都已正确组装。因此,我需要在最终程序集中包含单元测试。组装后,初始测试将被称为

> ant initTest

build.xml:

<target="initTest">
  <junit>
   <test class="MyTest" />
  </junit>
</target>

问题是: 我想将我的单元测试保存在 src/test/java 中,not 将它们移动到 src/main/java。 有没有办法告诉程序集插件包含我的单元测试?程序集描述符中的简单 include 不会这样做...

【问题讨论】:

    标签: maven-2 assemblies junit include


    【解决方案1】:

    有两个步骤:

    1. 将测试和主代码一起打包到 jar 中。
    2. 依赖于制作程序集的模块中的“-tests”jar。

    要打包测试,您需要绑定 jar:test-jar 目标。例如

    <build>
      <plugins>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
            <execution>
              <id>test-jar</id>
              <phase>package</phase>
              <goals>
                <goal>test-jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    

    然后在组装模块中,您可以依赖生成的工件。

    <dependencies>
      <dependency>
        <groupid>${project.groupId}</groupId>
        <artifactId>some-artifact</artifactId>
        <version>${project.version}</version>
        <classifier>tests</classifier>
      </dependency>
    </dependencies>
    

    关键位是“分类器”。

    【讨论】:

    • 依赖可能也应该有范围“test”
    【解决方案2】:

    @Dominic-Mitchell 的回答根本不适合我。最终起作用的是将测试类的fileSet 添加到我的程序集 xml 中。 请注意,fileSet 目录是不同的!这让我很困惑。

    测试类使用${project.build.directory},主类使用${project.build.outputDirectory}

    <?xml version='1.0' encoding='UTF-8'?>
    <assembly>
        <id>toolbar-test</id>
        <formats>
            <format>jar</format>
        </formats>
    
        <includeBaseDirectory>false</includeBaseDirectory>
    
        <fileSets>
            <fileSet>
                <directory>${project.build.directory}/test-classes</directory>
                <outputDirectory>/</outputDirectory>
            </fileSet>
            <fileSet>
                <directory>${project.build.outputDirectory}</directory>
                <outputDirectory>/</outputDirectory>
            </fileSet>
        </fileSets>
    
        <dependencySets>
            <dependencySet>
                <scope>runtime</scope>
                <unpack>true</unpack>
                <unpackOptions>
                    <excludes>
                        <exclude>**/LICENSE*</exclude>
                        <exclude>**/README*</exclude>
                    </excludes>
                </unpackOptions>
            </dependencySet>
            <dependencySet>
                <scope>test</scope>
                <unpack>true</unpack>
            </dependencySet>
        </dependencySets>
    </assembly>
    

    信用到期 - 我在这篇博文中找到了这个解决方案:http://alexgaddie.blogspot.com/2010/02/creating-uber-jar-with-maven.html
    我不需要博文中的 profile 部分。

    【讨论】:

      【解决方案3】:

      以下对我们有用。

      pom.xmlsn-p:

      <build>
          <plugins>
      
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>2.6</version>
                  <executions>
                      <execution>
                          <goals>
                              <goal>test-jar</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>           
          </plugins>
      </build>
      

      assembly.xmlsn-p:

      <dependencySets>
          <dependencySet>
              <outputDirectory>/lib</outputDirectory>
              <useProjectArtifact>true</useProjectArtifact>
              <useProjectAttachments>true</useProjectAttachments>
              <scope>runtime</scope>
          </dependencySet>
      </dependencySets>
      

      关键是useProjectAttachments标签。

      【讨论】:

        猜你喜欢
        • 2015-03-05
        • 2010-12-04
        • 2011-08-28
        • 2011-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        相关资源
        最近更新 更多