[英]Why is my maven dependency in included .jar not found in final .war
[英]Why aren't the Maven dependencies of my JAR being included in my WAR?
我有 2 個 Eclipse 項目:(1) my-library
,它構建my-library-1.0.jar
; 和 (2) my-deployable
,它構建my-deployable-1.0.war
。
my-library
是my-deployable
的pom.xml
中列出的依賴項。
my-library
在其pom.xml
引用了 2 個依賴項(2 個 Apache 公共 JAR)。
my-library
已注冊到我的 Nexus,我可以檢索它。
當我使用目標clean 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>
你的方法是正確的。 他們注定要被卷入戰爭。
您的依賴項及其范圍看起來也正確。
我會檢查[WARNING]
s 和[ERROR]
s 的 Maven 日志。 也許您有丟失或無效的 POM 問題。
我已經用上面的 poms 創建了 2 個骨架項目。 由於 spring-boot-maven-plugin 缺少 mainClass 定義,因此無法構建“my-deployable”pom
錯誤:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.718 s
[INFO] Finished at: 2020-02-01T22:31:35+00:00
[INFO] Final Memory: 28M/120M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.12.RELEASE:repackage (repackage) on project my-deployable: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.12.RELEASE:repackage failed: Unable to find main class -> [Help 1]
如果我添加以下部分指向一個具有空 main 方法的類,我可以編譯和打包項目
<configuration>
<mainClass>com.my.MainClass</mainClass>
</configuration>
生成的 war 文件確實包含 my-library 的依賴項。 您在匿名化 pom 時刪除了某些鍵,或者您錯過了構建錯誤?
當 JAR 被注冊到內部的 Nexus 存儲庫時(這個任務交給了一個輔助團隊),分配給它的pom.xml
是一個 Sonatype Nexus 生成的pom.xml
,而不是包含在 JAR 中的那個。 生成的文件沒有關聯的依賴項。
無效的生成的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>
<description>POM was created by Sonatype Nexus</description>
</project>
我通過檢查 Nexus 中每個 JAR 的pom.xml
發現了這一點,並將其與每個 .m2 文件夾和每個項目中的相同; 我將 .m2 pom.xml
與關聯項目中的文件進行了硬交換,構建了我的 WAR,並出現了傳遞依賴項。
下一步是使用正確的pom.xml
修復所有 Nexus 條目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.