gpt4 book ai didi

java - 使用 Maven 将测试和核心项目分开

转载 作者:行者123 更新时间:2023-12-01 23:03:36 24 4
gpt4 key购买 nike

我已经找到这篇文章:Separating tests from src with Maven Project? .

I've just started working on a Java project (as I usually use .net), and one of the first > things that strikes me as odd is that in the Maven project there is a /src and a /test directory where obviously the source code and the tests should go.

In .net I preferred to have the tests in a separate assembly/project, so for example I would have:

MyProject MyProject.Tests That way I dont have to bloat my deployed code with any tests and it makes it easier to test my code in true isolation and in alot of cases I didnt bother writing tests per project, I would just have solution wide unit/integration/acceptance tests i.e MySolution.UnitTests, MySolution.IntegrationTests.

However in Java it just seems to be bundled together, and I would rather separate it out, > however I hear that Maven is a cruel mistress when you want to do things differently to the default structures.

So to reign this post back in, my main [question is]:

Is there a way to separate out the tests from the project [How?]

虽然这个问题准确地描述了我想要实现的目标,但该线程并未提供如何实现它的解决方案。

我想知道是否有办法建立一个单独的项目,专门用于 JUnit 的(单元)测试。我希望在“核心”项目中拥有实际的源代码,并在单独的“测试”项目中拥有相应的测试,而不是拥有具有 src/main 和 src/test 路径的单个项目。但是,我不知道如何配置(父)pom.xml 文件来实现这一点。

到目前为止,我定义了一个父 pom,将这两个项目声明为模块。另外,对于这两个项目,我都有一个单独的pom文件,声明所需的依赖项等。当然,测试项目的pom文件将核心项目定义为依赖项。

我想我必须配置核心项目的 pom 文件来告诉测试插件在其他项目中查找测试。但这样的行为应该如何配置呢?

最佳答案

如果您遵循 Maven 约定(同时拥有 src 和 test 文件夹),您将会更轻松。您的测试不会与编译的源代码一起部署,因此我不必担心膨胀。 Maven 将编译一个 jar 和一个测试 jar 文件(假设您使用的是 jar)。如果您确实想要单独的 src/test 模块,那么是的,具有共同父级的多模块方法是可行的方法。测试模块将依赖于源模块,但反之则不然。实际上,这相当于重新发明 Maven 已经为您所做的事情。

从长远来看,我认为使用传统方法你会更高兴,因为事情会变得更加顺利。

关于java - 使用 Maven 将测试和核心项目分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23154615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com