java为什么我的JAR的Maven依赖项没有包含在我的WAR中?
我有两个Eclipse项目:(1)构建my-library
;和(2)my-deployable
,它构建my-deployable-1.0.war
my-library
是在my-deployable
的pom.xml
中列出的依赖项
my-library
在其{
my-library
已注册到我的Nexus,我可以检索它
当我使用goalsclean compile package
构建my-deployable-1.0.war
时,my-library-1.0.jar
包含在WEB-INF\lib
文件夹中,但是它的2个依赖项没有包含在预期的WAR中。我的印象是,我不需要在my-deployable
的pom.xml
中明确指定它们,因为它们已经在my-library
的pom.xml
中列出
为什么my-library
JAR的依赖项在构建时没有包含在my-deployable
WAR中强>
my-library
的pom.xml
内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my</groupId>
<artifactId>my-library</artifactId>
<version>1.0</version>
<name>My Library</name>
<description>This is my library JAR. It will be included in my WAR.</description>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.17</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
</project>
my-deployable
的pom.xml
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.my</groupId>
<artifactId>my-deployable</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>My Deployable</name>
<description>A deployable WAR for Spring Boot that has a dependency of my-library</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.my</groupId>
<artifactId>my-library</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
# 1 楼答案
你的方法是正确的。他们注定要被纳入战争
您的依赖项及其作用域看起来也是正确的
我会检查Maven的日志中是否有
[WARNING]
和[ERROR]
的内容。可能您的POM缺失或无效# 2 楼答案
我已经用上面的POM创建了2个骨架项目。由于spring boot maven插件缺少mainClass定义,“my deployable”pom无法生成
错误:
如果我添加以下指向一个带有空main方法的类的部分,我可以编译和打包该项目
生成的war文件确实包含我的库的依赖项。或者在匿名化pom时删除了某个密钥,或者错过了构建错误
# 3 楼答案
当JAR注册到内部Nexus存储库时(此任务被移交给一个辅助团队),分配给它的
pom.xml
是Sonatype Nexus生成的pom.xml
,而不是JAR中包含的。生成的文件没有关联的依赖项生成的无效
pom.xml
类似于:我通过检查Nexus中每个JAR的
pom.xml
来发现这一点,并将其与每个JAR中的相同内容进行比较。m2文件夹和每个项目;我硬交换了电话号码。m2pom.xml
使用关联项目中的文件,构建了我的WAR,出现了可传递的依赖项下一步是使用正确的
pom.xml
修复所有Nexus条目