为什么我的Maven项目打包测试文件?

时间:2017-04-12 11:17:30

标签: java maven

我有一个Maven项目,我试图打包,但我注意到我所有的Java测试类(但没有我的Scala测试类)和生成的Avro测试类最终都在广口瓶中。

文件夹结构看起来很好:

enter image description here

我还注意到,如果我将junit作为<scope>test</scope>的依赖项添加,我的测试将无法编译,因为它无法找到junit类,所以看起来Maven正在处理我的所有代码包括测试作为main的一部分。

波姆:

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:-4)

mvn clean install -Dmaven.test.skip = true -X