2017-04-12 68 views
2

我有一个Maven项目,我试图打包,但我注意到,我所有的Java测试类(但没有我的Scala测试类)和生成的Avro测试类都在jar中结束。为什么我的Maven项目打包测试文件?

文件夹结构看起来不错:

enter image description here

我也注意到,如果我添加的JUnit与<scope>test</scope>依赖,我的测试将无法编译,因为它不能找到JUnit类,所以它看起来像Maven正在把我所有的代码都包括在内,包括测试作为主要的一部分。

双响炮:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.me</groupId> 
    <artifactId>proj</artifactId> 
    <version>1.0.0</version> 

    <properties> 
     <log4j.version>2.8.1</log4j.version> 
    </properties> 

    <dependencies> 
     ... 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.scalatest</groupId> 
       <artifactId>scalatest-maven-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>test</id> 
         <goals> 
          <goal>test</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>net.alchim31.maven</groupId> 
       <artifactId>scala-maven-plugin</artifactId> 
       <version>3.2.1</version> 
       <executions> 
        <execution> 
         <id>scala-compile-first</id> 
         <phase>process-resources</phase> 
         <goals> 
          <goal>add-source</goal> 
          <goal>compile</goal> 
         </goals> 
        </execution> 
        <execution> 
         <id>scala-test-compile</id> 
         <phase>process-test-resources</phase> 
         <goals> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
       <configuration> 
        <scalaVersion>2.11.8</scalaVersion> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.avro</groupId> 
       <artifactId>avro-maven-plugin</artifactId> 
       <version>1.8.1</version> 
       <executions> 
        <execution> 
         <phase>generate-sources</phase> 
         <goals> 
          <goal>schema</goal> 
         </goals> 
         <configuration> 
          <sourceDirectory>${project.basedir}/src/test/resources/</sourceDirectory> 
          <outputDirectory>${project.basedir}/src/test/java</outputDirectory> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.6.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
       <executions> 
        <execution> 
         <phase>compile</phase> 
         <goals> 
          <goal>compile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-source-plugin</artifactId> 
       <executions> 
        <execution> 
         <id>attach-sources</id> 
         <goals> 
          <goal>jar</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 


</project> 
+0

会出现这种情况了用Java编写的,以及试验,或只是Scala呢? –

+0

看看这个,即使它不完全相同的问题:http://stackoverflow.com/questions/13157815/intellij-idea-sudenly-wont-recognize-tests-in-test-folder –

+0

@JiriTousek现在,我看看它,没有一个Scala测试是在jar中,所以它只是Java的。 – karoma

回答

0

想通了。

问题出在avro-maven-plugin上。

它的配置有sourceDirectoryoutputDirectory,它们都有测试源路径。

<configuration> 
    <sourceDirectory>${project.basedir}/src/test/resources/</sourceDirectory> 
    <outputDirectory>${project.basedir}/src/test/java</outputDirectory> 
</configuration> 

显然,这些都与认为这些目录是主要的源目录,并与主类的其余部分一起编译他们编译器插件干扰。这也解释了为什么我的测试在junit依赖项被赋予测试范围时未能编译。

的解决方案是使用testSourceDirectorytestOutputDirectory代替:

<configuration> 
    <testSourceDirectory>${project.basedir}/src/test/resources/</testSourceDirectory> 
    <testOutputDirectory>${project.basedir}/src/test/java</testOutputDirectory> 
</configuration> 
-4

MVN全新安装-Dmaven.test.skip =真-X

+0

这只会跳过测试,而不是解决问题。 –

+0

这是对这个问题的正确答案。 – Bevor

相关问题