jar-with-dependencies 和托管依赖
jar-with-dependencies and a managed dependency
我们有一个包含大约十几个子项目的父项目。大多数子项目都有测试,因此它们依赖于 JUnit。我认为将它作为托管依赖项拉到父 pom 是有意义的:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
在我的一个子项目中——仍在清理这个项目——我在构建期间需要 JUnit。所以在那个 pom 我现在有:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
这很好用。
事情分崩离析的部分是,该项目还需要构建为具有依赖项的 jar,使用:
<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>
jar-with-dependencies 缺少 JUnit。请注意,如果我将 JUnit 作为托管依赖项删除,并将其指定为该项目中的普通依赖项,那么一切都会正常构建。
如何将 jar 作为测试范围内的托管依赖项获取到具有依赖项的 jar 中?
托管依赖用于锁定父 pom 的版本。并不意味着junit会自动成为子项目的依赖。
您需要将其指定为子 pom 或父 pom 中的依赖项,以便将其包含在 uber jar 中
更新:我误解了这个问题并认为 OP 询问的是仅使用在依赖项 mgmt 中声明的依赖项。
How do I get jar, as a managed dependency scoped for test, into a
jar-with-dependencies?
依赖管理优先于依赖部分中声明的依赖 - 因此重新定义具有编译范围的依赖中的依赖将不起作用。要解决这个问题,请在子 pom 的依赖管理部分重新定义依赖。
所以你的父 pom 有:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
将此添加到您的子 pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
我们有一个包含大约十几个子项目的父项目。大多数子项目都有测试,因此它们依赖于 JUnit。我认为将它作为托管依赖项拉到父 pom 是有意义的:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
在我的一个子项目中——仍在清理这个项目——我在构建期间需要 JUnit。所以在那个 pom 我现在有:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>compile</scope>
</dependency>
这很好用。 事情分崩离析的部分是,该项目还需要构建为具有依赖项的 jar,使用:
<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>
jar-with-dependencies 缺少 JUnit。请注意,如果我将 JUnit 作为托管依赖项删除,并将其指定为该项目中的普通依赖项,那么一切都会正常构建。
如何将 jar 作为测试范围内的托管依赖项获取到具有依赖项的 jar 中?
托管依赖用于锁定父 pom 的版本。并不意味着junit会自动成为子项目的依赖。
您需要将其指定为子 pom 或父 pom 中的依赖项,以便将其包含在 uber jar 中
更新:我误解了这个问题并认为 OP 询问的是仅使用在依赖项 mgmt 中声明的依赖项。
How do I get jar, as a managed dependency scoped for test, into a jar-with-dependencies?
依赖管理优先于依赖部分中声明的依赖 - 因此重新定义具有编译范围的依赖中的依赖将不起作用。要解决这个问题,请在子 pom 的依赖管理部分重新定义依赖。
所以你的父 pom 有:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
将此添加到您的子 pom:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>