[英]How to reuse classes from /src/test/java in different project?
I have a class in a commons library that I placed into /src/test/java
.我在公共库中有一个类,我将其放入/src/test/java
。 Then I want to reuse that class in any project having the commons library as dependency.然后我想在任何具有公共库作为依赖项的项目中重用该类。
But the file cannot be imported.但是无法导入文件。
Common custom library:常用自定义库:
/src/test/java/com/myproject/utils/TestfileReader.java
Implementation project:实施项目:
<dependencies>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
</dependency>
usage:用法:
/src/test/java/com/myproject/itest/SomeTest.java
import com.myproject.utils.TestfileReader;
Result: the import cannot be resolved.结果:无法解析导入。
But if I copy that file to /src/main/java/...
it can be found correctly.但是如果我将该文件复制到/src/main/java/...
它可以正确找到。 So my commons library seems to be fine in general.所以我的公共图书馆总体上似乎没问题。
Question: how can I make this file visible only to my tests, while keeping it in /src/test/java
folder?问题:如何使该文件仅对我的测试可见,同时将其保存在/src/test/java
文件夹中?
you can do this by specifying the type to test-jarscope.您可以通过为 test-jarscope 指定类型来完成此操作。
<dependency>
<groupId>com.myproject</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
You can control whether your resources under your src/test folder will get shipped or not, eg via the maven-resource-plugin
See here: http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html你可以控制你的 src/test 文件夹下的资源是否会被运送,例如通过maven-resource-plugin
见这里: http : //maven.apache.org/plugins/maven-resources-plugin/testResources-mojo .html
An example here would copy all sources found under src/test/java
into your final build.此处的示例将在src/test/java
下找到的所有源复制到您的最终构建中。
<testResources>
<testResource>
<directory>${project.basedir}/src/test/java</directory>
</testResource>
</testResources>
You may do the following:您可以执行以下操作:
Absolutely, one can do it..绝对可以的..
First You need to navigate to Build Path-->Configure Build Path--> Source tab
首先,您需要导航到Build Path-->Configure Build Path--> Source tab
then in the Source tab search/check [your Project Name]/src/main/java
and change然后在 Source 选项卡中search/check [your Project Name]/src/main/java
并更改
"contains test sources"
from No to Yes and save it. "contains test sources"
从“否”到“是”并保存。
This will resolved issue of import packages from "src/test/java" to "src/main/java" successfully这将成功解决从“src/test/java”导入包到“src/main/java”的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.