gpt4 book ai didi

java - Eclipse 导入 Maven 配置文件中声明的依赖项

转载 作者:行者123 更新时间:2023-11-30 06:46:12 25 4
gpt4 key购买 nike

TL;DR

我必须在 Maven 配置文件中声明一些依赖项,而 Eclipse 无法解析这些包。下图中的示例。

全文

大家好:D

现在我正在一个非常可疑的项目中工作。我正在开发一个基于 Spring Web MVC 的 Web 应用程序,并且正在使用 Google AppEngine DevServer 进行本地开发,尽管出于显而易见的原因我正在认真考虑从 GAE 迁移。

该项目基于 GAE 的原因是因为我只是继续别人之前开始的工作。

无论如何,在开发和增加应用程序功能的同时,持久性引擎成为了需要。我决定实现 MongoDB,是的,我知道除了 GAE 环境中的数据存储之外,您不能使用任何其他东西。作为持久层,我使用 MongoRepository。到目前为止一切都还好。

测试时间一到,问题就来了。为了实现集成测试,我使用了内存数据库 Fake Mongo Fongo除了NoSQLUnit用于数据填充。

主要问题是 Fongo 需要 mongo java 驱动程序才能工作,但我无法声明它,也无法声明任何其他 JDBC 依赖项,因为 GAE DevServer 无法工作。我不在我的应用程序中使用 java-mongo-driver 任何软件,而是在集成测试中通过 Fongo 间接使用。

解决方案是简单地跳过所有测试源编译并在单独的 Maven 中声明它们,因此每次我想在本地服务器上部署时,我只需跳过所有测试。这不是问题,因为我使用 Jenkins 进行 CI 并使用 SonarQube 进行覆盖(使用 JaCoCo),因此我可以简单地在那里执行测试。

现在我的 pom.xml 包含常规部分中的所有应用程序依赖项,因此我可以 mvn clean install -Dmaven.test.skip=true然后我有一个 <profile>声明如下:

<profile>
<id>testing</id>
<dependencies>
<dependency>
<groupId>com.github.fakemongo</groupId>
<artifactId>fongo</artifactId>
<version>2.0.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.lordofthejars</groupId>
<artifactId>nosqlunit-mongodb</artifactId>
<version>0.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>
<build>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<junitArtifactName>junit:junit</junitArtifactName>
<encoding>UTF-8</encoding>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
</configuration>
</plugin>
</plugins>

</build>
</profile>

所以我可以 mvn clean test -Ptesting每当 Jenkins 检测到推送时。

问题是我无法在 Eclipse 中编写代码,因为它无法解析导入,因为它们是在配置文件中声明的。例如。 Eclipse can't resolve dependencies declared in a Maven profile

如何导入和使用我在 Maven 配置文件中声明的包?

谢谢:)

P.S.:我决定讲述完整的故事,这样也许其他正在或将要遇到同样问题的人可以找到它。我没有找到 java-mongo-driver 和 GAE 的任何解决方案,而不是定义配置文件并跳过测试。

最佳答案

找到解决方案。

事实证明,M2Eclipse 正在使用 Activity 配置文件,但默认情况下,它们不处于 Activity 状态。当您声明一个配置文件时,如果您想使用它,显然,您必须激活它在 Eclipse 中声明的依赖项。为此,您必须:

  1. 右键单击您的项目 > Maven > 选择 Maven 配置文件...

enter image description here 2. 只需选择要导入工作区的所有配置文件即可。 (选中它们)

enter image description here 3. 工作完成。

enter image description here

关于java - Eclipse 导入 Maven 配置文件中声明的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43656629/

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