所以我尝试运行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:没有目标
已为此版本指定。您必须指定一个有效的生命周期
阶段或目标,格式为:或
:[:]:。
但是我认为这是由于出现了所有重复的依赖项。我看到了一些有关删除它们的帖子,但是它们都没有意义,因为依赖项是通过一些较大的拱形
<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-*
工件为您提供正确的传递依赖关系,而不是尝试添加(可能会发生冲突)单个Spring依赖关系。更新1以响应此问题:
当我摆脱这些依赖关系时,它将导致在项目中找不到@GetMapping或@ResponseBody批注
GetMapping
和ResponseBody
接口位于通过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
提供的并且,如果
h2
是mysql
的仅测试替代品,则应将<scope>test</scope>
添加到此依赖项:<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
</dependency>
最后,我建议使用这两个教程中的一个或两个来以小规模,安全的方式处理Spring Boot的这些方面:
https://spring.io/guides/gs/accessing-data-jpa/
https://spring.io/guides/gs/accessing-data-rest/