Warm tip: This article is reproduced from stackoverflow.com, please click
eclipse java maven xml

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

发布于 2020-04-08 09:25:31

I have 2 Eclipse projects: (1) my-library, which builds my-library-1.0.jar; and (2) my-deployable, which builds my-deployable-1.0.war.

my-library is a dependency listed in the pom.xml of my-deployable.
my-library has 2 dependencies (2 Apache commons JARs) referenced in its pom.xml.

my-library is registered to my Nexus and I can retrieve it.

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

Why aren't dependencies for my-library JAR getting included in my-deployable WAR when built?

The pom.xml for my-library reads:

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

<?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>
Questioner
JoshDM
Viewed
65
JoshDM 2020-02-04 23:21

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. The generated file did not have dependencies associated.

The invalid, generated pom.xml is similar to:

<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; I hard-swapped the .m2 pom.xml with the file from the associated project, built my WAR, and the transitive dependencies appeared.

The next step is to repair all of the Nexus entries with the correct pom.xml.