简体   繁体   English

Netbeans中的Maven项目:如何将依赖项添加到“依赖项”和“测试依赖项”中?

[英]Maven project in Netbeans: How to add dependency to both 'Dependencies' and 'Test Dependencies'?

I have a Maven project in Netbeans 7.1 IDE. 我在Netbeans 7.1 IDE中有一个Maven项目。

I'd like to add the same dependency to both Dependencies and Test Dependencies . 我想在DependenciesTest Dependencies添加相同的依赖项。

Adding to one removes it from the other. 添加到其中一个将从另一个中删除它。

Duplicating the dependency in pom.xml and including in one of them: 在pom.xml中复制依赖项,并包括其中之一:

<scope>test</scope>

..doesn't work either. ..也不起作用。

Help! 救命!

More Details: 更多细节:

Assume I have projects MyProject and MyDependency . 假设我有MyProjectMyDependency项目。

MyProject contains MyDependency as a default scope (ie compile scope) dependency: MyProject包含MyDependency作为默认范围(即compile范围)依赖项:

<dependencies>
          <dependency>
                    <groupId>my.group.id</groupId>
                    <artifactId>AnArtifactId</artifactId>
                    <version>1.0-SNAPSHOT</version>
          </dependency>
</dependencies>

MyProject contains several classes in the Source Packages folder (ie MyProject/src/main/... ) which reference classes within MyDependency source packages. MyProjectSource Packages文件夹中包含几个类(即MyProject/src/main/... ),这些类引用MyDependency 包中的类。 These work perfectly; 这些完美地工作; Netbeans shows no red error flags and those classes compile successfully. Netbeans没有显示红色错误标志,并且这些类已成功编译。

MyProject contains several classes in the Test Packages folder (ie MyProject/src/test/... ) which reference classes within MyDependency test packages. MyProjectTest Packages文件夹中包含几个类(即MyProject/src/test/... ),这些类引用MyDependency 测试包中的类。 Netbeans displays red error flags in MyProject for these references. Netbeans在MyProject为这些引用显示红色错误标志。

MyDependency has been cleaned, built and stored in local Maven repo using mvn clean install -DskipTests . MyDependency已使用mvn clean install -DskipTests清理,构建并存储在本地Maven存储mvn clean install -DskipTests Running the same command for MyProject causes errors within the test classes only; MyProject运行相同的命令只会导致测试类中的错误; the non-test classes compile fine. 非测试类可以编译。

I discovered the solution is to duplicate the pom dependency entry as follows: 我发现解决方案是复制pom依赖项,如下所示:

<dependencies>
          <dependency>
                    <groupId>my.group.id</groupId>
                    <artifactId>AnArtifactId</artifactId>
                    <version>1.0-SNAPSHOT</version>
          </dependency>
          <dependency>
                    <groupId>my.group.id</groupId>
                    <artifactId>AnArtifactId</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <scope>test</scope>
                    <type>test-jar</type>
          </dependency>
</dependencies>

Specifying solely <scope>test</scope> would indicate that the jar containing source packages of MyDependency should be used as a dependency for the test packages of MyProject . 仅指定<scope>test</scope>表示将包含MyDependency源包的jar用作MyProject的测试包的依赖项。

However, by specifying <type>test-jar</type> the test jar (ie jar containing test packages) for MyDependency is used as a dependency for the test packages of MyProject . 但是,通过指定<type>test-jar</type>MyDependency的测试jar(即包含测试包的jar)用作MyProject的测试包的依赖项。

Dependencies也自动是Test Dependencies ,但不是相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM