问题
We have a parent project with about dozen child projects. Most of the child projects have tests, and so they depend on JUnit. I thought it would make sense to pull this out to the parent pom as a managed dependency:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
In one of my child projects - still working on cleaning up this one - I need JUnit during build. So in that pom I now have:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
This works fine. The part where things fall apart, is that this project also needs to be built as a jar with dependencies, using:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>TestIntegration</finalName>
</configuration>
</plugin>
The jar-with-dependencies is missing JUnit. Note that if I remove JUnit as a managed dependency, and specify it as a normal dependency in that project, then everything builds fine.
How do I get jar, as a managed dependency scoped for test, into a jar-with-dependencies?
回答1:
Managed dependency is used to lock the version at the parent pom. It does not mean that junit will automatically be a dependency of child projects.
You need to specify it as a dependency in the child or in parent pom for it to be included in a uber jar
Update: I misunderstood the question and thought that the OP was asking about using a dependency declared in dependency mgmt only.
How do I get jar, as a managed dependency scoped for test, into a jar-with-dependencies?
Dependency management takes precedence over dependencies declared in dependency section - so redefining the dependency in dependencies with scope compile will not work. To fix this, redefine the dependency in the child pom's dependency management section.
So your parent pom has:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
Add this to your child pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
来源:https://stackoverflow.com/questions/31711024/jar-with-dependencies-and-a-managed-dependency