相关疑难解决方法(0)

Spring Boot:是否可以使用胖jar在任意目录中使用外部application.properties文件?

是否可以拥有多个application.properties文件?(编辑:请注意,这个问题演变为标题上的问题.)

我试着有2个文件.

  • 第一个是在应用程序Jar中的根文件夹上.
  • 第二个是在classpath中指定的目录.

2个文件都命名为'application.properties'.

是否可以"合并"两个文件的内容?(并且第二个属性值覆盖第一个属性值)或者,如果我有一个文件,则忽略另一个文件?

更新1:可以"合并"内容.昨天似乎第一个被忽略了,但似乎是因为当时有些东西被打破了.现在它运作良好.

更新2:它又回来了!同样,只应用了两个文件中的一个.这很奇怪......它是在我使用Spring Tool Suite构建app jar文件之后开始的.似乎Jar版本总是忽略第二个(在类路径中),而在STS上运行的扩展版本的行为会有所不同.我可以从哪里开始调查?

更新3:

Jar版本的行为实际上是正确的.这是java.exe的规范.如果指定了-jar选项,则java.exe将忽略-classpath选项和CLASSPATH环境变量,并且类路径将仅包含jar文件.因此,类路径上的第二个application.properties文件将被忽略.

现在,我如何才能加载类路径上的第二个application.properties?

更新4:

我设法在使用-jar选项时在外部路径中加载application.properties文件.

关键是PropertiesLauncher.

要使用PropertiesLauncher,必须像这样更改pom.xml文件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->
            </configuration>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

为此,我引用了以下StackOverflow问题:spring boot属性启动程序无法使用.BTW,在Spring Boot Maven插件文档(http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/maven-plugin/repackage-mojo.html)中,没有提到指定ZIP触发器使用了PropertiesLauncher.(也许在另一份文件中?)

在构建jar文件之后,我可以看到通过检查jar中META-INF/MENIFEST.MF中的Main-Class属性来使用PropertiesLauncher.

现在,我可以按如下方式运行jar(在Windows中):

java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

请注意,应用程序jar文件包含在loader.path中.

现在加载了C:\ My\External\Dir\config中的application.properties文件.

作为奖励,jar中也可以访问该目录中的任何文件(例如,静态html文件),因为它位于加载器路径中.

对于UPDATE 2中提到的非jar(扩展)版本,可能存在类路径顺序问题.

(顺便说一句,我将问题的标题更改为更具体地解决了这个问题.)

configuration spring spring-boot

54
推荐指数
4
解决办法
8万
查看次数

在Eclipse中的Spring-boot项目中使用Maven"无法找到主类"

我非常绝望,因为我不再能编译我的Maven项目.我有一个逻辑项目分成7个maven模块到目前为止编译得很好,但今天我介绍了一个功能,我不小心生成了一个循环maven依赖.因此,我重构了代码并消除了循环依赖.自从我更新了pom文件以反映我的项目不再构建的修复程序.当我使用"-X clean install"运行maven时,我得到以下java.lang.IllegalStateException:无法找到主类

[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] agiletunes-parent
[INFO] agiletunes-util
[INFO] agiletunes-security-common
[INFO] agiletunes-marketing-persistence
[INFO] agiletunes-common-ui
[INFO] agiletunes-security-client
[INFO] agiletunes-marketing-application
[INFO] agiletunes-security-server
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-parent 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.2.RELEASE:repackage (default) @ agiletunes-parent ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ agiletunes-parent ---
[INFO] Installing C:\Users\Alexander\Documents\Development\agileTunes\agiletunes-parent\pom.xml to C:\Users\Alexander\Documents\Development\mavenArtifacts\com\agiletunes\agiletunes-parent\0.0.1\agiletunes-parent-0.0.1.pom
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building agiletunes-util 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) …
Run Code Online (Sandbox Code Playgroud)

eclipse maven spring-boot

33
推荐指数
9
解决办法
6万
查看次数

使用 Spring Boot 从 Gradle 多模块项目构建的 Jar 不起作用

我创建了 spring-boot gradle 多模块项目,其中包含 3 个模块:控制器、服务、存储库。主文件位于控制器模块中,名为 MySpringBootApplication。

我可以构建这个项目(使用 gradle build)并可以获得 jar 文件。但是在命令行中启动这个 jar 后,我遇到了下一个错误:

Error: Could not find or load main class com.epam.esm.config.MySpringBootApplication Caused by: java.lang.ClassNotFoundException: com.epam.esm.config.MySpringBootApplication.
Run Code Online (Sandbox Code Playgroud)

为了修复此错误,我在 main 中的MANIFEST.MF文件Main-Class中添加了属性,但此操作没有帮助。那么有人可以帮忙吗?build.gradle

主要构建.Gradle 文件

        plugins {
            id 'java'
            id 'io.spring.dependency-management' version '1.0.11.RELEASE'
            id 'org.springframework.boot' version '2.4.3'
            id 'application'
        }
        
        group = 'com.myproject'
        version = 'snapshot-1.0.0'
        
        repositories {
            mavenCentral()
        }
        
        bootJar {
            enabled = false
        }
        
        jar {
            enabled = true
            manifest {
                attributes(
                        "Main-Class": "com.myproject.config.MySpringBootApplication")
            }
        } …
Run Code Online (Sandbox Code Playgroud)

java gradle multi-module spring-boot

3
推荐指数
1
解决办法
4919
查看次数