4

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.

KomodoDave
  • 7,239
  • 10
  • 60
  • 92

2 Answers2

3

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.

KomodoDave
  • 7,239
  • 10
  • 60
  • 92
0

Dependencies are automatically Test Dependencies as well, but not the other way around.

Yuriy Zubarev
  • 2,821
  • 18
  • 24
  • Thanks, I thought as much. So why is it that my classes in `Source Packages` recognise the dependency yet those in `Test Packages` cannot? It's not just a Netbeans glitch, attempting to build with `mvn` in bash doesn't work for the test classes :( Could it be because I've put the references used by the tests into 'Test Packages' within my dependency? i.e. can a project's test package see a dependency's test package when the dependency is the default compile ? – KomodoDave Feb 14 '12 at 20:32
  • I can't due to company policy. However I've added to my OP as much additional info as I feel will be useful. Any further advice would be very much appreciated. – KomodoDave Feb 14 '12 at 20:43
  • Kudos for respecting company policy. Unfortunately it's still important to see POM file. You could try to minimize and obfuscate it. – Yuriy Zubarev Feb 14 '12 at 20:49
  • The only reference from one project to another is in the location I've exemplified within 'More Details' in my OP, i.e. within a single `` tag which itself is within a `` level `` tag. No more of the pom should be needed, but thank you anyway. – KomodoDave Feb 14 '12 at 20:51