用 Maven 配置文件打包

huangapple go评论63阅读模式
英文:

Package Maven With Profile

问题

I am running spring boot, and I have a spring boot with 3 profiles. They are as follows.

applicaton.properties
{Empty file}

application-local.properties
test.variable = '1'

application-server.properties
test.variable = '2'

Now, when I run my springboot, I have one or the other profiles always running, depending on if its my server or local env. I have an @value to parse these values. However, when I run mvn package, it freaks out, since it does not pick a profile to package, and when compiling and trying to compile the @value line it cannot find test.variable, since it does not exist on applicaton.properties. How do I mvn package to compile this to a jar?

Also, I understand that when running this application, all I need to do is add -Dspring.profiles.active={profile}, but say I compile under the local profile, can I still run the jar under the server profile properly later?

Thanks!

A bit more of an update... I am attempting to run this command: mvn package -P local

Maven throws this exception:

{bunch of other stacktrace stuff}
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'test.variable' in value "${test.variable}"

Please note that when I run this application in intellij, and in my run configurations, when I select to run 'local' as my profile, it works perfectly, so its just a matter of getting maven to use the local profile properly.

英文:

I am running spring boot, and I have a spring boot with 3 profiles. They are as follows.

applicaton.properties
{Empty file}

application-local.properties
test.variable = '1'

application-server.properties
test.variable = '2'

Now, when I run my springboot, I have one or the other profiles always running, depending on if its my server or local env. I have an @value to parse these values. However, when I run mvn package, it freaks out, since it does not pick a profile to package, and when compiling and trying to compile the @value line it cannot find test.variable, since it does not exist on applicaton.properties. How do I mvn package to compile this to a jar?

Also, I understand that when running this application, all I need to do is add -Dspring.profiles.active={profile}, but say I compile under the local profile, can I still run the jar under the server profile properly later?

Thanks!

A bit more of an update... I am attempting to run this command: mvn package -P local

Maven throws this exception:

    {bunch of other stacktrace stuff}
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'test.variable' in value "${test.variable}"

Please note that when I run this application in intellij, and in my run configurations, when I select to run 'local' as my profile, it works perfectly, so its just a matter of getting maven to use the local profile properly.

答案1

得分: 1

Spring profile(Spring配置文件)只是应用程序的一个参数,由Spring框架用于设置不同的Bean/属性,从而定义不同的应用程序行为。同时,Spring profile是运行时参数,仅在运行应用程序时使用。Maven profile(Maven配置文件)与Spring profile之间没有关联。

所以,当你写道:

"当我运行mvn package时,它会出现问题,因为它没有选择一个要打包的配置文件,而在编译和尝试编译@Value行时,它找不到test.variable,因为它在application.properties中不存在。如何使用mvn package将其编译成一个JAR文件?"

这不太合适,因为:

  1. "maven" 不知道Spring配置文件(Spring profiles)。
  2. "maven" 不使用(也不应该使用)Spring配置文件(Spring profiles)来进行编译/打包应用程序,因为Spring配置文件(Spring profiles)是运行时参数。

"如何使用mvn package将其编译成一个JAR文件?"

要将Spring Boot应用程序打包成JAR文件,可以使用spring-boot-maven-plugin。最简单的方式是使用https://start.spring.io/,它会为你准备一个初始的Maven项目骨架。

英文:

Spring profile is just a parameter for your application that is used by spring framework to set up different beans/properties and hence define different application behaviour. Also spring profile is runtime parameter and used only during the running the application. There is no relation between maven profile and spring profile.

So when you wrote

> when I run mvn package, it freaks out, since it does not pick a profile to package, and when compiling and trying to compile the @value line it cannot find test.variable, since it does not exist on applicaton.properties. How do I mvn package to compile this to a jar?

it does not make much sence - because

  1. maven does not know aboout spring profiles.
  2. maven does not use (and should not use) spring profile for compiling/packaging your application since spring profile is it runtime parameter.

> How do I mvn package to compile this to a jar?

To package your spring boot application to jar one can use spring-boot-maven-plugin. The easiest way to start is to use https://start.spring.io/, which prepate initial maven project sceleton for you.

答案2

得分: 0

Multiple options to solve this:

  • 为您的变量提供默认值
@Value("${test.variable:my-default-value}")
String variable;
  • 通过Maven为测试设置配置文件
<build>
  <plugins>
     ...
	<plugin>
     <groupId>org.apache.maven.plugins</groupId>
	 <artifactId>maven-surefire-plugin</artifactId>
		<configuration>
	   	  <argLine>-Dspring.profiles.active=local</argLine>
		</configuration>
	</plugin>
  • 通过src/test/resources/application.properties提供测试变量
# 在test/resources文件夹中的application.properties
test.variable = test
英文:

Multiple options to solve this:

  • Provide a default value for your variable
@Value(&quot;${test.variable:my-default-value}&quot;)
String variable;
  • Set profile via maven for tests
&lt;build&gt;
  &lt;plugins&gt;
     ...
	&lt;plugin&gt;
     &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
	 &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
		&lt;configuration&gt;
	   	  &lt;argLine&gt;-Dspring.profiles.active=local&lt;/argLine&gt;
		&lt;/configuration&gt;
	&lt;/plugin&gt;

  • Provide test variable via src/test/resources/application.properties
# application.properties in test/resources folder
test.variable = test

...

huangapple
  • 本文由 发表于 2023年5月7日 14:48:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定