简体   繁体   English

为什么我的 JAR 的 Maven 依赖项没有包含在我的 WAR 中?

[英]Why aren't the Maven dependencies of my JAR being included in my WAR?

I have 2 Eclipse projects: (1) my-library , which builds my-library-1.0.jar ;我有 2 个 Eclipse 项目:(1) my-library ,它构建my-library-1.0.jar and (2) my-deployable , which builds my-deployable-1.0.war .和 (2) my-deployable ,它构建my-deployable-1.0.war

my-library is a dependency listed in the pom.xml of my-deployable . my-librarymy-deployablepom.xml中列出的依赖项。
my-library has 2 dependencies (2 Apache commons JARs) referenced in its pom.xml . my-library在其pom.xml引用了 2 个依赖项(2 个 Apache 公共 JAR)。

my-library is registered to my Nexus and I can retrieve it. my-library已注册到我的 Nexus,我可以检索它。

When I build my-deployable-1.0.war using goals clean compile package , my-library-1.0.jar is included in the WEB-INF\\lib folder, but its 2 dependencies are not being included in the WAR where they are expected.当我使用目标clean compile package构建my-deployable-1.0.warmy-library-1.0.jar包含在WEB-INF\\lib文件夹中,但它的 2 个依赖项未包含在预期的 WAR 中。 I am under the impression I do not need to specify them explicitly in the pom.xml of my-deployable because they are already listed in the pom.xml of my-library .我的印象是我不需要在my-deployablepom.xml中明确指定它们,因为它们已经列在my-librarypom.xml中。

Why aren't dependencies for my-library JAR getting included in my-deployable WAR when built?为什么在构建时my-library JAR 的依赖项没有包含在my-deployable WAR 中?

The pom.xml for my-library reads: 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>

The pom.xml for my-deployable reads: 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>

Your approach is correct.你的方法是正确的。 They are meant to be included into the war.他们注定要被卷入战争。

Your dependencies and their scopes also look correct.您的依赖项及其范围看起来也正确。

I would check the log of Maven for [WARNING] s and [ERROR] s.我会检查[WARNING] s 和[ERROR] s 的 Maven 日志。 Maybe you have missing or invalid POM problem.也许您有丢失或无效的 POM 问题。

I've created 2 skeleton projects with your poms above.我已经用上面的 poms 创建了 2 个骨架项目。 The 'my-deployable' pom doesn't build as the spring-boot-maven-plugin is missing a mainClass definition由于 spring-boot-maven-plugin 缺少 mainClass 定义,因此无法构建“my-deployable”pom

Error:错误:

[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]

If I add the following section pointing at an class with an empty main method, I can compile and package the project如果我添加以下部分指向一个具有空 main 方法的类,我可以编译和打包项目

<configuration>
    <mainClass>com.my.MainClass</mainClass>
</configuration>

The resulting war file does indeed contain the dependencies of my-library.生成的 war 文件确实包含 my-library 的依赖项。 Either you've removed something key when you anonymised your pom, or you've missed the build error?您在匿名化 pom 时删除了某些键,或者您错过了构建错误?

在此处输入图片说明

When the JAR was registered to the in-house Nexus repository (this task is handed off to a secondary team), the pom.xml assigned to it was a Sonatype Nexus-generated pom.xml , not the one included with the JAR.当 JAR 被注册到内部的 Nexus 存储库时(这个任务交给了一个辅助团队),分配给它的pom.xml是一个 Sonatype Nexus 生成的pom.xml ,而不是包含在 JAR 中的那个。 The generated file did not have dependencies associated.生成的文件没有关联的依赖项。

The invalid, generated pom.xml is similar to:无效的生成的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>

I discovered this by checking the pom.xml for each JAR in Nexus, comparing it to the same in each .m2 folder and each project;我通过检查 Nexus 中每个 JAR 的pom.xml发现了这一点,并将其与每个 .m2 文件夹和每个项目中的相同; I hard-swapped the .m2 pom.xml with the file from the associated project, built my WAR, and the transitive dependencies appeared.我将 .m2 pom.xml与关联项目中的文件进行了硬交换,构建了我的 WAR,并出现了传递依赖项。

The next step is to repair all of the Nexus entries with the correct pom.xml .下一步是使用正确的pom.xml修复所有 Nexus 条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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