gpt4 book ai didi

java - Spring Boot Maven 不会构建可执行 Jar,因为它发现重复项

转载 作者:行者123 更新时间:2023-12-02 02:33:50 30 4
gpt4 key购买 nike

因此,我尝试运行 mvn clean package 为我的 Spring Boot 应用程序构建可执行 jar,但它失败了,因为它发现了重复项。

以下是出现的错误消息的示例。其中大部分似乎与 Tomcat 相关:

[WARNING] Found duplicate (but equal) classes in [org.apache.tomcat.embed:tomcat-embed-core:8.5.20, org.apache.tomcat:tomcat-juli:8.5.20]:

这些重复项从哪里来?和我的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>
<!-- Output to jar format -->
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<artifactId>spring-boot-starter-web</artifactId>
<name>Spring Boot Web Starter</name>
<description>Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container
</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- JPA Data (for using Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
app.ContactRunner
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

也许是因为我导入了太多入门项目或其他什么?真的只是不确定。

编辑

使用更完整的堆栈跟踪运行它,它说我没有定义任何目标,尽管我的 POM 显然有一个目标:

org.apache.maven.lifecycle.NoGoalSpecifiedException: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format : or :[:]:.

但我认为这是由于出现了所有重复的依赖关系。我看过一些关于删除它们的帖子,但它们都没有意义,因为依赖项会通过一些更大的、包罗万象的 <dependency> 自动进入。标签

例如,一个警告是这样说的:

[WARNING] Found duplicate and different classes in [org.apache.tomcat.embed:tomcat-embed-core:8.5.20, org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final]:
[WARNING] javax.persistence.PersistenceContext
[WARNING] javax.persistence.PersistenceContextType

我需要 tomcat 和 jpa 依赖项,并且我没有专门添加 PersistenceContext,它们会自动添加。如何防止依赖项重复或防止错误?

最佳答案

删除<scope>test</scope>来自此依赖项:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.7.RELEASE</version>
<scope>test</scope>
</dependency>

删除这些依赖项:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>

总而言之:依靠spring-boot-starter-* Artifact 为您提供正确的传递依赖项,而不是尝试添加(可能存在冲突的)单独的 Spring 依赖项。

更新 1 对此的回应:

when I get rid of those dependencies, it causes the @GetMapping or @ResponseBody annotations to not be found in the project

GetMappingResponseBody接口(interface)位于 org.springframework:spring-web这是通过 org.springframework.boot:spring-boot-starter-web 提供的人工制品。所以,只要你去掉了<scope>test</scope>来自您的spring-boot-starter-web依赖,那么你就会拥有这些。尝试从命令行重建项目和/或在进行我建议的更改之后将其重新导入到 IDE 中。

您也可以删除此...

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>

...因为它是由 spring-boot-starter-web 提供的

并且,如果 h2mysql 的仅测试替代品那么你应该添加 <scope>test</scope>对此依赖项:

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>

最后,我建议使用其中一个或两个教程以小规模、安全的方式尝试 Spring Boot 的这些方面:

关于java - Spring Boot Maven 不会构建可执行 Jar,因为它发现重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696977/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com