构建spring boot项目时出错[重复]

Posted

技术标签:

【中文标题】构建spring boot项目时出错[重复]【英文标题】:Error while building spring boot project [duplicate] 【发布时间】:2018-02-20 20:58:30 【问题描述】:

我无法构建我的 springboot 项目, 我正在使用另一个 springboot jar 中的类,我将其放在构建路径上,它通过 eclipse 在本地运行,但没有构建,这是错误

"[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ContentManagementService 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ contentmanagementapi ---
[INFO] Deleting /home/bhargavgundu/git/contentmanagementservice/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ contentmanagementapi ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ contentmanagementapi ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 42 source files to /home/bhargavgundu/git/contentmanagementservice/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[32,31] package com.conduiraonline.apis does not exist
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[42,31] package com.conduiraonline.daos does not exist
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[52,35] cannot find symbol
  symbol:   class SlackService
  location: package com.conduiraonline.services
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[85,17] cannot find symbol
  symbol:   class UserDAO
  location: class com.conduiraonline.controller.ContentManagementControllerV1
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[87,17] cannot find symbol
  symbol:   class SlackService
  location: class com.conduiraonline.controller.ContentManagementControllerV1
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[95,17] cannot find symbol
  symbol:   class LicenseV5
  location: class com.conduiraonline.controller.ContentManagementControllerV1
[INFO] 6 errors 
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.917s
[INFO] Finished at: Tue Sep 12 17:16:44 IST 2017
[INFO] Final Memory: 24M/396M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project contentmanagementapi: Compilation failure: Compilation failure:
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[32,31] package com.conduiraonline.apis does not exist
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[42,31] package com.conduiraonline.daos does not exist
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[52,35] cannot find symbol
[ERROR] symbol:   class SlackService
[ERROR] location: package com.conduiraonline.services
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[85,17] cannot find symbol
[ERROR] symbol:   class UserDAO
[ERROR] location: class com.conduiraonline.controller.ContentManagementControllerV1
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[87,17] cannot find symbol
[ERROR] symbol:   class SlackService
[ERROR] location: class com.conduiraonline.controller.ContentManagementControllerV1
[ERROR] /home/bhargavgundu/git/contentmanagementservice/src/main/java/com/conduiraonline/controller/ContentManagementControllerV1.java:[95,17] cannot find symbol
[ERROR] symbol:   class LicenseV5
[ERROR] location: class com.conduiraonline.controller.ContentManagementControllerV1
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
"

无论我在哪里使用外部 jar 中的类/函数,它都会显示错误,有人可以帮忙吗? 我的POM-

这是我的 POM

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

    <groupId>com.conduiraonline</groupId>
    <artifactId>contentmanagementapi</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <name>ContentManagementService</name>
    <description>ContentManagementService</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>com.conduiraonline</groupId>
            <artifactId>licenseapi</artifactId>
            <version>0.0.1</version>
            <classifier>exec</classifier>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

【问题讨论】:

编译时出错,检查是否包含必要的JAR文件,添加的JAR文件是否足够? 你需要在你的pom中添加你的外部jar的maven依赖 @cralfaro 我确实添加了它,检查具有“licenseapi”的行 添加编译 每当您想将 jar 添加到项目中时,请将其作为依赖项添加到项目中,而不是将其添加到构建路径中,因为如果存储库中没有依赖项,maven 将无法识别构建路径 jar创建 maven 依赖脚本 ***.com/questions/4955635/… 并在类路径级别运行它 【参考方案1】:

您必须在 gradle 或 maven 构建文件中添加 jars/包含 jars 的文件夹的路径。

例如在 gradle 中添加

    compile fileTree(dir: 'path_to_ext_libs', include: '*.jar')

到 build.gradle 中的依赖项,然后将包含 libs 文件夹中的所有 jar。

或者在maven中

<repositories>
   <repository>
     <id>local-files</id>
     <name>local-files</name>
     <url>file://c:\test\filerepo</url>
  </repository>

【讨论】:

你能告诉我如何在 maven 中做到这一点吗?是不是和在POM中添加一样? 我从构建路径添加了 jar,它出现在“引用的库”中,应该可以正常工作吗? 信任 gradle 或 maven 或信任您的 IDE。【参考方案2】:

TL;DR

从对com.conduiraonline:licenseapi的依赖中移除&lt;classifier&gt;exec&lt;/classifier&gt;

更多详情...

你用&lt;dependency&gt;来表达com.conduiraonline:contentmanagementapicom.conduiraonline:licenseapi之间的关系是正确的。或者换一种说法;这是告诉 Maven 模块 com.conduiraonline:contentmanagementapi 依赖于模块 com.conduiraonline:licenseapi正确方法。

但是,您已将com.conduiraonline:licenseapi 的依赖定义为:

    <dependency>
        <groupId>com.conduiraonline</groupId>
        <artifactId>licenseapi</artifactId>
        <version>0.0.1</version>
        <classifier>exec</classifier>
    </dependency>

classifier 告诉 Maven 在本地 Maven 存储库中的 com/conduiraonline 文件夹中查找名为 licenseapi.exec 的文件(如果失败,请尝试从您在 Maven 设置中配置的任何远程存储库中解决该依赖关系.xml 或在这个 pom.xml 中)。

我认为在本地 Maven 存储库中的文件夹 com/conduiraonline 中没有名为 licenseapi.exec 的文件,因为 exec 是一个非常不寻常的 Maven 工件分类器。

只要在您的本地 Maven 存储库中安装了版本为 0.0.1com.conduiraonline:licenseapi 的 JAR,此依赖项就可以工作:

    <dependency>
        <groupId>com.conduiraonline</groupId>
        <artifactId>licenseapi</artifactId>
        <version>0.0.1</version>
    </dependency>

您通过为licenseapi 模块运行mvn clean install,在本地Maven 存储库中为com.conduiraonline:licenseapi 安装一个版本为0.0.1 的JAR(假设licenseapi 模块的pom.xml 具有&lt;version&gt;0.0.1&lt;/version&gt;) .

【讨论】:

这行得通,谢谢 :) 我有一个疑问,一旦我这样做了,那个 jar 就不需要出现在类路径上,对吗? 通过声明该依赖项,您是在告诉 Maven com.conduiraonline:contentmanagementapicom.conduiraonline:licenseapi 具有编译依赖项。我不完全确定您所说的“该 jar 不需要出现在类路径上”是什么意思,但是通过声明该依赖关系,您是在说您 确实希望 JAR 出现在 classapth 上。

以上是关于构建spring boot项目时出错[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 2、Hibernate、PostgreSQL 中通过 JDBC 语句执行 DDL 时出错

Spring Boot Data JPA 的问题 - 创建名称为 [重复] 的 bean 时出错

连接PostgreSQL数据库的Spring Boot应用程序错误[重复]

使用 spring.io 演示项目时 Spring Boot 出错

摇篮 | Spring boot 依赖不排除

Spring Boot 2.x 之 Logging