Spring Boot Maven不会构建可执行jar,因为它正在查找重复项

时间:2017-10-11 20:27:43

标签: java maven spring-boot

所以我尝试运行mvn clean包为我的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,它们会自动添加它们。如何防止依赖项重复或防止错误?

2 个答案:

答案 0 :(得分:2)

从此依赖项中删除<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注释

GetMappingResponseBody接口位于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的这些方面:

答案 1 :(得分:0)

我找到了解决方案。我不知道为什么会这样,但当我交换它时:

// SET GALLERY COLUMN WIDTH
    var galleryCol = document.getElementById("setWidth");
    var y = document.getElementsByClassName("gallery-col");
    var i;
    for (i = 0; i < y.length; i++) {
      y[i].style.height = galleryCol + "px";
    }

换取

        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starters</artifactId>
        <version>1.5.7.RELEASE</version>

<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> 代码之间。它解决了我所有的问题。之后我能<parent>就好了。