温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - Why aren't the Maven dependencies of my JAR being included in my WAR?
eclipse java maven xml

java - 为什么我的WAR中没有包含JAR的Maven依赖关系?

发布于 2020-04-08 11:16:46

我有2个Eclipse项目:(1)my-library,该项目构建my-library-1.0.jar和(2)my-deployable,生成my-deployable-1.0.war

my-library是的中列出的依赖pom.xmlmy-deployable
my-library在中引用了2个依赖项(2个Apache Commons JAR)pom.xml

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

当我my-deployable-1.0.war使用Goals进行构建时clean compile packagemy-library-1.0.jar包含在WEB-INF\lib文件夹中,但是预期的WAR中没有包含它的2个依赖项。我的印象是,我不需要在pom.xmlof中明确指定它们my-deployable因为它们已在pom.xmlof中列出my-library

为什么在构建WAR时不将my-libraryJAR的依赖项包含在my-deployableWAR中?

pom.xml用于my-library读取:

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

pom.xml用于my-deployable读取:

<?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>

查看更多

提问者
JoshDM
被浏览
127
JoshDM 2020-02-04 23:21

当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>

我通过检查pom.xmlNexus中的每个JAR并将其与每个.m2文件夹和每个项目中的JAR进行比较,发现了这一点pom.xml用相关项目中的文件对.m2 进行了硬交换,构建了我的WAR,并出现了传递性依赖项。

下一步是使用正确的修复所有Nexus条目pom.xml