简体   繁体   English

如何在不同的项目中重用来自 /src/test/java 的类?

[英]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:您可以执行以下操作:

  • Create a jar file from the project that you want to share in different projects.从要在不同项目中共享的项目创建一个 jar 文件。
  • Use this jar file as dependencies by giving group id, artifact id and version.通过提供组 ID、工件 ID 和版本,将此 jar 文件用作依赖项。
  • Remember to add index of this jar file in your external dependencies.请记住在您的外部依赖项中添加此 jar 文件的索引。

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.

相关问题 Java中另一个项目的主应用如何复用一个项目中的测试类 - How to reuse the test classes in one project in the main application of another project in Java 多模块项目:在来自不同模块的测试类之间重用静态方法 - multimodule project: reuse static method between test classes from different modules 从Eclipse中的另一个Java项目重用类的最佳方法是什么? - What is the best way to reuse classes from another Java project in Eclipse? 如何防止 Maven 编译 src/test/java 中的类,这些类是在 generate-sources 阶段生成的? - How to prevent Maven from compiling classes in src/test/java, which are generated during the generate-sources phase? 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java 如何重用Java类(使用eclipse) - how to reuse java classes (with eclipse) 无法将 src/main/java 类导入 maven-spring-web 项目中的 src/test/java jUnit 测试。 IDE - 日食 - Cant import src/main/java classes into src/test/java jUnit tests in maven-spring-web project. IDE - Eclipse 如何从 src 文件夹调用测试类并使用 maven 构建 - How to call test classes from src folder and build using maven 如何重用开源项目中的测试代码 - How to reuse test code from open source project 是否可以从 src/main 中的 src/test 加载测试类? - Is it possible to load test classes from src/test in src/main?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM