我正在尝试运行以下命令来启动 springboot 应用程序
java -Dlog4j.configuration=file:///log4j.properties -Dspring.profiles.active=local -cp ./target/app-1.0.0-SNAPSHOT-jar-with-dependencies.jar MainClass
上面的 jar 是使用
maven-assembly-plugin
准备的,其配置是
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
主类看起来像
@SpringBootApplication
@Slf4j
public class MainClass {
public static void main(String... args) {
log.info(System.getProperty("spring.profiles.active"));
ApplicationContext ctx = SpringApplication.run(AcctrvDataProcessor.class, args);
log.info(Arrays.toString(ctx.getEnvironment().getActiveProfiles()));
int exitCode = SpringApplication.exit(ctx);
System.exit(exitCode);
}
}
当我从上面的命令运行应用程序时,我看到以下日志:
15:45:07.091 [main] INFO MainClass - local
15:46:07.091 [main] INFO MainClass - No active profile set, falling back to 1 default profile: "default"
什么可能阻止设置弹簧配置文件活动?
为什么要调用主类?这是一个 Spring Boot 应用程序,因此请尝试通过调用 jar 来启动。
java -jar -Dlog4j.configuration=file:///log4j.properties -Dspring.profiles.active=local app-1.0.0-SNAPSHOT-jar-with-dependencies.jar
请注意,在应用程序 jar 之前添加 -D 参数非常重要,否则它们将无法被识别。
如果您硬性要求使用-cp(类路径)并且不直接调用jar,那么您可以设置一个环境变量来设置shell中的活动配置文件,然后尝试以这种方式启动spring应用程序。
export SPRING_PROFILES_ACTIVE=local