簡體   English   中英

未找到 Maven 多模塊依賴包

[英]Maven Multi-module dependency package not found

我有一個多模塊項目:父 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 http://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>1.4.3.RELEASE</version>
    </parent>

    <groupId>be.bodyreset</groupId>
    <artifactId>bodyreset-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Bodyreset Parent</name>
    <description>Manages the common properties and dependencies for the child modules</description>

    <modules>
        <module>bodyreset-commons</module>
        <module>messaging-contract</module>
        <module>discovery-microservice</module>
        <module>api-gateway-microservice</module>
        <module>rest-client-microservice</module>
        <module>email-commons</module>
        <module>security-microservice</module>
        <module>user-microservice</module>
        <module>point-of-service-microservice</module>
    </modules>

    <properties>
        <docker.image.prefix>bodyreset</docker.image.prefix>
        <java.version>1.8</java.version>
        <powermock.version>1.6.6</powermock.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            ...
        </dependencies>
    </dependencyManagement>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.spotify</groupId>
                    <artifactId>docker-maven-plugin</artifactId>
                    <version>0.4.11</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.0</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

rest-client-microservice 的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>be.bodyreset</groupId>
        <artifactId>bodyreset-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>rest-client-microservice</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Rest Client Microservice</name>
    <description>Manages the REST clients to use for inter-service communication</description>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/</targetPath>
                            <directory>${project.build.directory}</directory>
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

email-commons 模塊的 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>be.bodyreset</groupId>
        <artifactId>bodyreset-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>email-commons</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Email Commons</name>
    <description>Enables sending emails via SMPT</description>

    <dependencies>
        <dependency>
            <groupId>be.bodyreset</groupId>
            <artifactId>rest-client-microservice</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>be.bodyreset</groupId>
            <artifactId>rest-client-microservice</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

如果我嘗試在父項目上執行maven clean install ,我會得到以下結果:

[INFO] ------------------------------------------------------------------------
[INFO] Building Email Commons 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ email-commons ---
[INFO] Deleting /home/developer/ideaProjects/bodyreset/email-commons/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ email-commons ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 2 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.0:compile (default-compile) @ email-commons ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 17 source files to /home/developer/ideaProjects/bodyreset/email-commons/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToRecipientConverter.java:[4,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToRecipientConverter.java:[16,63] cannot find symbol
  symbol: class UserDTO
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToRecipientConverter.java:[22,30] cannot find symbol
  symbol:   class UserDTO
  location: class be.bodyreset.mail.converter.UserDTOToRecipientConverter
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToSenderConverter.java:[4,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToSenderConverter.java:[5,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToSenderConverter.java:[15,60] cannot find symbol
  symbol: class UserDTO
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/converter/UserDTOToSenderConverter.java:[21,27] cannot find symbol
  symbol:   class UserDTO
  location: class be.bodyreset.mail.converter.UserDTOToSenderConverter
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[7,32] package be.bodyreset.rest.client does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[8,32] package be.bodyreset.rest.client does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[9,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[10,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[34,13] cannot find symbol
  symbol:   class PointOfServiceClient
  location: class be.bodyreset.mail.service.impl.SenderServiceImpl
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[37,13] cannot find symbol
  symbol:   class UserClient
  location: class be.bodyreset.mail.service.impl.SenderServiceImpl
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/SenderServiceImpl.java:[63,13] cannot find symbol
  symbol:   class PointOfServiceDTO
  location: class be.bodyreset.mail.service.impl.SenderServiceImpl
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/RecipientServiceImpl.java:[7,32] package be.bodyreset.rest.client does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/RecipientServiceImpl.java:[8,29] package be.bodyreset.rest.dto does not exist
[ERROR] /home/developer/ideaProjects/bodyreset/email-commons/src/main/java/be/bodyreset/mail/service/impl/RecipientServiceImpl.java:[32,13] cannot find symbol
  symbol:   class UserClient
  location: class be.bodyreset.mail.service.impl.RecipientServiceImpl
[INFO] 17 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Bodyreset Parent .................................. SUCCESS [0.690s]
[INFO] Bodyreset Commons ................................. SUCCESS [7.444s]
[INFO] Messaging Contract ................................ SUCCESS [1.751s]
[INFO] Discovery Microservice ............................ SUCCESS [19.925s]
[INFO] API Gateway Microservice .......................... SUCCESS [17.272s]
[INFO] Rest Client Microservice .......................... SUCCESS [15.271s]
[INFO] Email Commons ..................................... FAILURE [1.026s]
[INFO] Security Microservice ............................. SKIPPED
[INFO] User Microservice ................................. SKIPPED
[INFO] Point Of Service Microservice ..................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:04.292s
[INFO] Finished at: Thu Jan 19 11:53:25 CET 2017
[INFO] Final Memory: 63M/656M
[INFO] ------------------------------------------------------------------------

我已經在這個問題上打破了幾天,但我不知道為什么這不起作用。

我想到了。 rest-client-microservice 是一個 Spring Boot 項目,使用以下插件:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
        </execution>
    </executions>
</plugin>

jar 被重新打包,所有的包和類都放在 BOOT-INF 文件夾中。 這就是 Maven 無法找到它們的原因。 您可以通過像這樣定義插件來解決此問題:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>

使用此配置,Spring Boot Maven 插件將創建 2 個 JAR:主要的 JAR 將與通常的 Maven 項目相同,而第二個將附加分類器並作為可執行 JAR。

在 rest-client-microservice Spring Boot 項目中將此配置添加到 spring-boot-maven-plugin。

<configuration>
 <classifier>exe</classifier>
</configuration>

所以它看起來像這樣。

           <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exe</classifier>
                </configuration>
            </plugin>

對於此版本及更高版本,我正在使用 Spring Boot 2.2.7,從父 pom.xml 中刪除以下內容:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

解決了問題

來源: https ://spring.io/guides/gs/multi-module/

要告訴 Maven 不要為 Library 項目構建可執行 jar,您必須從 Spring Initializr 創建的 pom.xml 中刪除以下塊:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

對於 Spring Boot 2,bootRepackage 不再作為任務存在。 它被 bootJar 任務取代。 為了使用 Gradle 構建兩個 jar,我將其添加到我的基本模塊中:

jar {
    enabled = true
}

對於那些在 SO 上嘗試了一切但沒有成功的可憐人,看在上帝的份上,請確保您的模塊路徑是正確的。

我的一個模塊路徑就像

moduleA/main/java/com/blah/...

但它缺少src目錄,因此將其修復為

moduleA/src/main/java/com/blah/...

解決了這一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM