多模块Maven项目 – 在jar中包含测试beans

huangapple go评论79阅读模式
英文:

Multi-module Maven project - include test beans in jar

问题

如何将位于测试目录(src/test/)下的Spring Beans 包含在导入到另一个模块的JAR中?

我有几个Maven项目:ms1、ms2、ms3和general-shared。general-shared是一个多模块的Maven项目。它包含了几个模块:general-shared-utills、general-shared-fs等等...

在每个子模块中,我有一个专用的pom.xml,其中包含了所有需要的依赖项,在general-shared的pom中,我有关于构建和模块的详细信息:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<modules>
    <module>general-shared-utills</module>
    <module>general-shared-fs</module>
</modules>

似乎我在子模块中为测试(位于src/test)创建的所有Bean都没有被导入到Maven模块(例如ms1)中。

一个例子:

在子模块general-shared-utills中,我有以下类:

在/src/main:

@Component
public class Shop {
    @Autowired
    private AWSService awsService;
}

@Component
@Profile("prod")
public class AWSService {
    ...
}

在/src/test:

@Component
@Profile("test")
public class AWSServiceMock extends AWSService {
    ...
}

当我在ms1的pom中导入general-shared-utills模块时,我希望它能包含AWSServiceMock类。目前,ms1的测试无法找到任何类型为AWSService的bean,因为它没有具有测试配置文件的AWSService类型的bean:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'a.x.y.aws.AWSService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
英文:

How can I include Spring beans under test dir (src/test/) in the jar that is imported to another module ?

I have a few Maven projects : ms1, ms2, ms3 and general-shared. The general-shared is a multi-module Maven project. It contains a few modules : general-shared-utills, general-shared-fs etc...

In each sub module I had a dedicated pom.xml with all the dependencies that are needed and in the general-shared pom I have details regarding the build and the modules :

  &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;source&gt;11&lt;/source&gt;
                    &lt;target&gt;11&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
    &lt;modules&gt;
        &lt;module&gt;general-shared-utills&lt;/module&gt;
        &lt;module&gt;general-shared-fs&lt;/module&gt;
    &lt;/modules&gt;

It seems that all the beans that I created for tests (located in src/test) in the sub modules aren't imported to the Maven module (ms1 for example).

An example :

In sub module general-shared-utills I have the following classes :

Under /src/main :

@Component
public class Shop
    @Autowired
    private AWSService awsService

@Component
@Profile(&quot;prod&quot;)
public class AWSService 
...

Under /src/test:

@Component
@Profile(&quot;test&quot;)
public class AWSServiceMock extends AWSService 

When I import the general-shared-utills module in the pom of ms1 I want it to include the AWSServiceMock class. Right now, the tests of ms1 can't find any bean of type AWSService because it doesn't have a bean of AWSService type with test profile :

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type &#39;a.x.y.aws.AWSService &#39; available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

答案1

得分: 0

我将相关的bean(模拟对象)从src/test目录移动到了src/main目录。

我还在Maven的网站上找到了这个解决方案

如果有人有另外一个实用的回答,我会很乐意听取并更新帖子的答案。

英文:

I moved the relevant beans(mocks) from the src/test dir to src/main.

I also found this solution on maven`s site.

if someone will have another practical answer I'll be happy to hear and update the post`s answer.

huangapple
  • 本文由 发表于 2020年10月5日 02:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64198147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定