[英]Spring boot app fails to run in STS but runs fine on command line
[英]Maven mvn spring-boot:run - cannot find local library, fails on command line but runs in STS4
我的 Spring Boot (v2.2.0.RELEASE) 應用程序稱為項目服務。 我想將它容器化,並在此過程中注意到它不會在 Eclipse (STS4) 之外運行。 所以我想知道我應該改變什么。
該應用程序依賴於我編寫的一個小型庫來重用一些類(fscl-core-lib),該庫是通過 Maven 作為 jar 構建的,現在位於我的本地計算機上。
path-to-project/fscl-core-lib/target/fscl-core-lib-0.2.0.jar
當我從 Eclipse(實際上是 STS4)運行應用程序時,它可以工作。 當我嘗試從命令行運行時,它以不同的方式失敗,無論我嘗試什么,都與找不到庫有關。
到目前為止我已經嘗試過:
1) Maven 對 lib 的依賴
在項目服務/pom.xml 中:
<dependency>
<groupId>io.fscl</groupId>
<artifactId>fscl-core-lib</artifactId>
<version>0.2.0</version>
</dependency>
這在 STS4 中運行,但在 CLI 上失敗:
[ERROR] Failed to execute goal on project project-service: Could not resolve dependencies for project io.fscl:project-service:jar:0.2.0: Failure to find io.fscl:fscl-core-lib:jar:0.2.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
2) 告訴 Maven 在哪里可以找到 lib jar
<repositories>
<repository>
<id>fscl-core-lib</id>
<url>file://path-to-project/fscl-core-lib/target</url>
</repository>
</repositories>
...
<dependency>
<groupId>io.fscl</groupId>
<artifactId>fscl-core-lib</artifactId>
<version>0.2.0</version>
</dependency>
這在 STS4 中運行,但在 CLI 上失敗:
[ERROR] Failed to execute goal on project project-service: Could not resolve dependencies for project io.fscl:project-service:jar:0.2.0: Failure to find io.fscl:fscl-core-lib:jar:0.2.0 in file:///path-to-project/fscl-core-lib/target was cached in the local repository, resolution will not be reattempted until the update interval of fscl-core-lib has elapsed or updates are forced -> [Help 1]
所以它對本地存儲庫執行 go 但似乎期望不同的文件名。 如何確定 lib 構建過程生成的文件名,應該是什么?
3) 作為最后的手段,在 class 路徑中包含 jar
In STS4, for project-service, Project -> Properties -> Java Build Path --> Libraries --> Add external JARs..., then select path-to-project/fscl-core-lib/target/fscl-core-lib-0.2.0.jar
這在 STS4 中運行,但在 CLI 上因編譯器錯誤而失敗,因為它沒有從 libs 包中找到任何內容。
java.lang.NoClassDefFoundError: fscl/core/FSCLEntity
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_131]
我已經上下搜索了 web 以提出這三種方法,但目前無能為力。 它一定是完全顯而易見的,或者我一般都在遵循一些完全錯誤的方法?
歡迎任何建議。
作為參考,這里是庫的 pom.xml:
fscl-core-lib/pom.xml
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.fscl</groupId>
<artifactId>fscl-core-lib</artifactId>
<version>0.2.0</version>
<name>fscl-core-lib</name>
<description>Library with cross-service reusable classes for FSCL system.</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
以及應用程序項目服務/pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.fscl</groupId>
<artifactId>project-service</artifactId>
<version>0.2.0</version>
<name>project-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<repositories>
<repository>
<id>fscl-core-lib</id>
<url>file://path-to-project/fscl-core-lib/target</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fscl</groupId>
<artifactId>fscl-core-lib</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>de.bechte.junit</groupId>
<artifactId>junit-hierarchicalcontextrunner</artifactId>
<version>4.12.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons-core</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
所以,
正如 LucasP 建議的那樣,在 lib 項目中運行 mvn install 就可以了,因為它將 jar 安裝到本地存儲庫 (~/.m2... ) 中。
同時,我選擇在依賴項目中創建一個單獨的存儲庫,並將 lib jar 部署到其中(請參閱本文)。
這樣,一旦我需要構建新版本的 lib,(1)我在 pom.xml 中更新它的版本信息,(2)使用mvn package
構建它,(3)運行mvn deploy:deploy-file
命令對於每個依賴項目(我在 lib 文件夾中的deploy
bash 腳本中維護這些)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.