Maven project in Netbeans: How to add dependency to both 'Dependencies' and 'Test Dependencies'?
-
29-04-2021 - |
Domanda
I have a Maven project in Netbeans 7.1 IDE.
I'd like to add the same dependency to both Dependencies
and Test Dependencies
.
Adding to one removes it from the other.
Duplicating the dependency in pom.xml and including in one of them:
<scope>test</scope>
..doesn't work either.
Help!
More Details:
Assume I have projects MyProject
and MyDependency
.
MyProject
contains MyDependency
as a default scope (i.e. compile
scope) dependency:
<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 (i.e. MyProject/src/main/...
) which reference classes within MyDependency
source packages. These work perfectly; Netbeans shows no red error flags and those classes compile successfully.
MyProject
contains several classes in the Test Packages
folder (i.e. MyProject/src/test/...
) which reference classes within MyDependency
test packages. Netbeans displays red error flags in MyProject
for these references.
MyDependency
has been cleaned, built and stored in local Maven repo using mvn clean install -DskipTests
. Running the same command for MyProject
causes errors within the test classes only; the non-test classes compile fine.
Soluzione
I discovered the solution is to duplicate the pom dependency entry as follows:
<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
.
However, by specifying <type>test-jar</type>
the test jar (i.e. jar containing test packages) for MyDependency
is used as a dependency for the test packages of MyProject
.
Altri suggerimenti
Dependencies
are automatically Test Dependencies
as well, but not the other way around.