简体   繁体   English

从Maven构建中排除测试文件夹

[英]Exclude test folder from maven build

This is one of the most asked questions here in so... but why any answer works for me? 这是这里最常见的问题之一...但是为什么有任何答案对我有用? i believe i have somehow a wrong maven structure in my project 我相信我的项目中的Maven结构有某种错误

I have a project like 我有一个像

basedir
|-src
|--many many many packages
|-test
|--testpackages
|-pom.xml

So my pom.xml is in the same level as src and test folders and it isn't the typical structure like java/main/ java/test... it is the root of the project folder 所以我的pom.xmlsrctest文件夹处于同一级别,它不是像java / main / java / test这样的典型结构...它是项目文件夹的根目录

<build>
        <sourceDirectory>${basedir}/src</sourceDirectory>
        <testSourceDirectory>${basedir}/test</testSourceDirectory>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>

                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                <excludes>
                <exclude>**/test/**</exclude>
                </excludes>
                </configuration>


            </plugin>

        </plugins>
    </build>

but when i run maven install and open the generated jar the folder test is in there... how can i solve and make it ignore this folder? 但是当我运行maven install并打开生成的jar时,文件夹测试就在其中...我该如何解决并使它忽略此文件夹?

Add -DskipTests to mvn command as a workaround. 作为解决方法,将-DskipTests添加到mvn命令。 For instance: 例如:

mvn package -DskipTests

Yes, I believe your maven project structure is not correct. 是的,我相信您的Maven项目结构不正确。 Maven excludes src/test/java for packaging by default so test folder should be inside src and not in the same level as shown below: Maven默认不包含src/test/java进行打包,因此test文件夹应位于src内,而不是如下所示的同一级别:

在此输入图像描述

For more details visit Maven doc 有关更多详细信息,请访问Maven文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM