繁体   English   中英

为什么我的 JAR 的 Maven 依赖项没有包含在我的 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-librarymy-deployablepom.xml中列出的依赖项。
my-library在其pom.xml引用了 2 个依赖项(2 个 Apache 公共 JAR)。

my-library已注册到我的 Nexus,我可以检索它。

当我使用目标clean compile package构建my-deployable-1.0.warmy-library-1.0.jar包含在WEB-INF\\lib文件夹中,但它的 2 个依赖项未包含在预期的 WAR 中。 我的印象是我不需要在my-deployablepom.xml中明确指定它们,因为它们已经列在my-librarypom.xml中。

为什么在构建时my-library JAR 的依赖项没有包含在my-deployable WAR 中?

my-librarypom.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-deployablepom.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM