Maven: Excluding tests from build

大憨熊 提交于 2019-12-14 03:41:58

问题


I have some classes I'm using as tests in my src/test/java folder of my project. When I run maven using the standard maven compile plugin. Those items are compiled into .class files and are included in the jar where the compiled code is packaged.

I've created these tests for myself to run within eclipse, prior to running maven and building my release. They are just sanity tests and should not be included in the build. I'd rather not put them in a seperate project, because, to me, they make sense here. How can I tell maven that I do not want it to compile/include the files in that directory?

I beleive the maven compiler plugin is generating the jar as follows:

<plugin>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
  <source>1.6</source>
  <target>1.6</target>
 </configuration>
</plugin>

回答1:


I understand from your comment on this answer that the "tests" aren't unit tests, but just ordinary classes that you want excluded from the final artifact? As such, your best option is to make use of the <exclude> tag with the maven-jar-plugin as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <excludes>
            <exclude>**/yoursortoftestpackage/YourSortOfTestClass*</exclude>
        </excludes>
    </configuration>
</plugin>

Hope that helps!

Cheers,




回答2:


Annotation for junit @Ignore will ignore the class while building in maven.

Edit:

You can configure maven-surefire-plugin.

<project>
<build>
<project>
<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.4.2</version>
    <configuration>
      <excludes>
        <exclude>**/TestCircle.java</exclude>
        <exclude>**/TestSquare.java</exclude>
      </excludes>
    </configuration>
  </plugin>
</plugins>



回答3:


Put your custom test files in a folder src/localtest/java i think, maven will not know it is there.




回答4:


Use the option of -Dmaven.test.skip will skip both compilation and execution of the tests. ( use -DskipTests just skips the test execution, the tests are still compiled)

The reference link

mvn clean install -Dmaven.test.skip



来源:https://stackoverflow.com/questions/14717743/maven-excluding-tests-from-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!