2017-04-12 69 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

相關問題