我使用这样的@Service
类在应用程序 Boot 后运行服务。
@Service
@Conditional(LoadOnProductionEnvironment.class)
public class RunnerProduction {
static final Logger LOGGER = LoggerFactory.getLogger(RunnerProduction.class);
private final FooBarService runner;
public RunnerProduction(FooBarService runner) {
this.runner = AssertUtils.notNull(runner, "runner");
}
@PostConstruct
public void run() {
runner.run();
}
}
当我创建独立的可执行jar
包mvn clean package -Pproduction
时,应用程序启动并立即崩溃,因为production
配置文件的设置在开发环境中不可见。
是否可以创建软件包但不启动应用程序本身?也许我可以改变服务如何启动,但我想保持手动启动。
编辑:这个设置还有一个问题--如果我有一些用@Scheduled
注解的方法,然后mvn clean package
启动应用程序,它会等待,Maven构建没有完成。
编辑2:当我使用@Scheduled
应用程序启动并在mvn clean package
期间保持活动状态。
@Scheduled(timeUnit = TimeUnit.MINUTES, fixedDelay = 15)
public void scheduled() {
runner.run();
}
}
编辑3:相关问题:How to disable @Scheduled while building a Spring-boot project-无分辨率。
编辑4:@Scheduled(timeUnit = TimeUnit.SECONDS, fixedDelay = 5, initialDelay = 1)
激活时的控制台日志。(我让运行5个预定的执行,并在最后用cmd+c取消应用程序)。
jnemec@jirka-macbook manitutrader-dataprocessor % mvn clean package -Pproduction -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] ------------< com.manitutrader:manitutrader-dataprocessor >-------------
[INFO] Building manitutrader-dataprocessor 1.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ manitutrader-dataprocessor ---
[INFO] Deleting /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target
[INFO]
[INFO] >>> spring-boot:3.0.0:run (default) > test-compile @ manitutrader-dataprocessor >>>
[INFO]
[INFO] --- resources:3.2.0:resources (default-resources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 3 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 18 source files to /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes
[INFO]
[INFO] --- resources:3.2.0:testResources (default-testResources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/src/test/resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] <<< spring-boot:3.0.0:run (default) < test-compile @ manitutrader-dataprocessor <<<
[INFO]
[INFO]
[INFO] --- spring-boot:3.0.0:run (default) @ manitutrader-dataprocessor ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.12)
2023-06-14 08:38:22.976 INFO 76330 --- [ restartedMain] c.m.d.ApplicationRoot : Starting ApplicationRoot using Java 17.0.6 on jirka-macbook.local with PID 76330 (/Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes started by jnemec in /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor)
2023-06-14 08:38:22.977 DEBUG 76330 --- [ restartedMain] c.m.d.ApplicationRoot : Running with Spring Boot v2.7.12, Spring v5.3.27
2023-06-14 08:38:22.977 INFO 76330 --- [ restartedMain] c.m.d.ApplicationRoot : The following 1 profile is active: "production"
2023-06-14 08:38:22.993 INFO 76330 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-06-14 08:38:23.119 INFO 76330 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-06-14 08:38:23.144 INFO 76330 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 22 ms. Found 1 JPA repository interfaces.
2023-06-14 08:38:23.232 INFO 76330 --- [ restartedMain] ptablePropertiesBeanFactoryPostProcessor : Post-processing PropertySource instances
2023-06-14 08:38:23.233 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Skipping PropertySource configurationProperties [class org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource
2023-06-14 08:38:23.233 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemProperties [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.233 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemEnvironment [org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource] to EncryptableSystemEnvironmentPropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource random [org.springframework.boot.env.RandomValuePropertySource] to EncryptablePropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource Config resource 'class path resource [application-production.properties]' via location 'optional:classpath:/' [org.springframework.boot.env.OriginTrackedMapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource Config resource 'class path resource [application.properties]' via location 'optional:classpath:/' [org.springframework.boot.env.OriginTrackedMapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource devtools [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource class path resource [application.properties] [org.springframework.core.io.support.ResourcePropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.234 INFO 76330 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource URL [file:/Users/jnemec/.app/credentials.properties] [org.springframework.core.io.support.ResourcePropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:38:23.266 INFO 76330 --- [ restartedMain] c.u.j.f.DefaultLazyPropertyFilter : Property Filter custom Bean not found with name 'encryptablePropertyFilter'. Initializing Default Property Filter
2023-06-14 08:38:23.270 INFO 76330 --- [ restartedMain] c.u.j.r.DefaultLazyPropertyResolver : Property Resolver custom Bean not found with name 'encryptablePropertyResolver'. Initializing Default Property Resolver
2023-06-14 08:38:23.271 INFO 76330 --- [ restartedMain] c.u.j.d.DefaultLazyPropertyDetector : Property Detector custom Bean not found with name 'encryptablePropertyDetector'. Initializing Default Property Detector
2023-06-14 08:38:23.282 INFO 76330 --- [ restartedMain] c.u.j.e.DefaultLazyEncryptor : Found Custom Encryptor Bean org.jasypt.encryption.pbe.PooledPBEStringEncryptor@7a5575a0 with name: jasyptStringEncryptor
2023-06-14 08:38:23.582 INFO 76330 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-06-14 08:38:23.741 INFO 76330 --- [ restartedMain] c.m.d.ApplicationRoot : Started ApplicationRoot in 0.889 seconds (JVM running for 1.098)
2023-06-14 08:38:24.744 INFO 76330 --- [ scheduling-1] c.m.d.r.RunnerProduction : ------------------------- @Scheduled executed: 2023-06-14T08:38:24.741572
2023-06-14 08:38:29.751 INFO 76330 --- [ scheduling-1] c.m.d.r.RunnerProduction : ------------------------- @Scheduled executed: 2023-06-14T08:38:29.751377
2023-06-14 08:38:34.757 INFO 76330 --- [ scheduling-1] c.m.d.r.RunnerProduction : ------------------------- @Scheduled executed: 2023-06-14T08:38:34.757749
2023-06-14 08:38:39.764 INFO 76330 --- [ scheduling-1] c.m.d.r.RunnerProduction : ------------------------- @Scheduled executed: 2023-06-14T08:38:39.764081
2023-06-14 08:38:44.768 INFO 76330 --- [ scheduling-1] c.m.d.r.RunnerProduction : ------------------------- @Scheduled executed: 2023-06-14T08:38:44.768008
^C2023-06-14 08:38:45.877 INFO 76330 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
[INFO]
[INFO] --- resources:3.2.0:resources (default-resources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 3 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 18 source files to /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes
[INFO]
[INFO] --- resources:3.2.0:testResources (default-testResources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/src/test/resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- surefire:2.22.2:test (default-test) @ manitutrader-dataprocessor ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- jar:3.2.2:jar (default-jar) @ manitutrader-dataprocessor ---
[INFO] Building jar: /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/manitutrader-dataprocessor.jar
[INFO]
[INFO] --- spring-boot:3.0.0:repackage (repackage) @ manitutrader-dataprocessor ---
[INFO] Layout: JAR
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- spring-boot:3.0.0:repackage (default) @ manitutrader-dataprocessor ---
[INFO] Layout: JAR
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24.689 s
[INFO] Finished at: 2023-06-14T08:38:46+02:00
[INFO] ------------------------------------------------------------------------
编辑5:控制台日志与@PostConstruct
方法-现在我没有任何服务执行int这个方法,但如果我会,它将被执行。
jnemec@jirka-macbook manitutrader-dataprocessor % mvn clean package -Pproduction -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] ------------< com.manitutrader:manitutrader-dataprocessor >-------------
[INFO] Building manitutrader-dataprocessor 1.0
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ manitutrader-dataprocessor ---
[INFO] Deleting /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target
[INFO]
[INFO] >>> spring-boot:3.0.0:run (default) > test-compile @ manitutrader-dataprocessor >>>
[INFO]
[INFO] --- resources:3.2.0:resources (default-resources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 3 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 18 source files to /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes
[INFO]
[INFO] --- resources:3.2.0:testResources (default-testResources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/src/test/resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] <<< spring-boot:3.0.0:run (default) < test-compile @ manitutrader-dataprocessor <<<
[INFO]
[INFO]
[INFO] --- spring-boot:3.0.0:run (default) @ manitutrader-dataprocessor ---
[INFO] Attaching agents: []
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.12)
2023-06-14 08:42:02.796 INFO 76786 --- [ restartedMain] c.m.d.ApplicationRoot : Starting ApplicationRoot using Java 17.0.6 on jirka-macbook.local with PID 76786 (/Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes started by jnemec in /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor)
2023-06-14 08:42:02.798 DEBUG 76786 --- [ restartedMain] c.m.d.ApplicationRoot : Running with Spring Boot v2.7.12, Spring v5.3.27
2023-06-14 08:42:02.798 INFO 76786 --- [ restartedMain] c.m.d.ApplicationRoot : The following 1 profile is active: "production"
2023-06-14 08:42:02.813 INFO 76786 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-06-14 08:42:02.941 INFO 76786 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-06-14 08:42:02.986 INFO 76786 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 43 ms. Found 1 JPA repository interfaces.
2023-06-14 08:42:03.075 INFO 76786 --- [ restartedMain] ptablePropertiesBeanFactoryPostProcessor : Post-processing PropertySource instances
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Skipping PropertySource configurationProperties [class org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemProperties [org.springframework.core.env.PropertiesPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource systemEnvironment [org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor$OriginAwareSystemEnvironmentPropertySource] to EncryptableSystemEnvironmentPropertySourceWrapper
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource random [org.springframework.boot.env.RandomValuePropertySource] to EncryptablePropertySourceWrapper
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource Config resource 'class path resource [application-production.properties]' via location 'optional:classpath:/' [org.springframework.boot.env.OriginTrackedMapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource Config resource 'class path resource [application.properties]' via location 'optional:classpath:/' [org.springframework.boot.env.OriginTrackedMapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.076 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource devtools [org.springframework.core.env.MapPropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.077 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource class path resource [application.properties] [org.springframework.core.io.support.ResourcePropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.077 INFO 76786 --- [ restartedMain] c.u.j.EncryptablePropertySourceConverter : Converting PropertySource URL [file:/Users/jnemec/.app/credentials.properties] [org.springframework.core.io.support.ResourcePropertySource] to EncryptableMapPropertySourceWrapper
2023-06-14 08:42:03.107 INFO 76786 --- [ restartedMain] c.u.j.f.DefaultLazyPropertyFilter : Property Filter custom Bean not found with name 'encryptablePropertyFilter'. Initializing Default Property Filter
2023-06-14 08:42:03.111 INFO 76786 --- [ restartedMain] c.u.j.r.DefaultLazyPropertyResolver : Property Resolver custom Bean not found with name 'encryptablePropertyResolver'. Initializing Default Property Resolver
2023-06-14 08:42:03.112 INFO 76786 --- [ restartedMain] c.u.j.d.DefaultLazyPropertyDetector : Property Detector custom Bean not found with name 'encryptablePropertyDetector'. Initializing Default Property Detector
2023-06-14 08:42:03.123 INFO 76786 --- [ restartedMain] c.u.j.e.DefaultLazyEncryptor : Found Custom Encryptor Bean org.jasypt.encryption.pbe.PooledPBEStringEncryptor@5d35edbc with name: jasyptStringEncryptor
2023-06-14 08:42:03.429 INFO 76786 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-06-14 08:42:03.466 INFO 76786 --- [ restartedMain] c.m.d.r.RunnerProduction : ------------------------- @PostConstruct executed
2023-06-14 08:42:03.590 INFO 76786 --- [ restartedMain] c.m.d.ApplicationRoot : Started ApplicationRoot in 0.92 seconds (JVM running for 1.141)
2023-06-14 08:42:03.593 INFO 76786 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
[INFO]
[INFO] --- resources:3.2.0:resources (default-resources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 3 resources
[INFO] Copying 2 resources
[INFO]
[INFO] --- compiler:3.10.1:compile (default-compile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 18 source files to /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/classes
[INFO]
[INFO] --- resources:3.2.0:testResources (default-testResources) @ manitutrader-dataprocessor ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/src/test/resources
[INFO]
[INFO] --- compiler:3.10.1:testCompile (default-testCompile) @ manitutrader-dataprocessor ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- surefire:2.22.2:test (default-test) @ manitutrader-dataprocessor ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- jar:3.2.2:jar (default-jar) @ manitutrader-dataprocessor ---
[INFO] Building jar: /Users/jnemec/IdeaProjects/manitutrader/manitutrader-dataprocessor/target/manitutrader-dataprocessor.jar
[INFO]
[INFO] --- spring-boot:3.0.0:repackage (repackage) @ manitutrader-dataprocessor ---
[INFO] Layout: JAR
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- spring-boot:3.0.0:repackage (default) @ manitutrader-dataprocessor ---
[INFO] Layout: JAR
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.724 s
[INFO] Finished at: 2023-06-14T08:42:04+02:00
[INFO] ------------------------------------------------------------------------
1条答案
按热度按时间plicqrtu1#
默认情况下,在
src.test.java...
包中有一个用@SpringBootTest
注解的测试类,用于验证应用程序上下文的加载。该测试以及其他测试(如果存在)将在Maven测试阶段运行。您可以使用以下命令删除默认测试类(如果没有其他测试类)或跳过测试:
编辑:
在OP提供控制台输出后,似乎
run
目标的执行是在spring-boot-maven-plugin
中定义的,这导致了应用程序在test-compile
Maven阶段的运行。删除它可以解决问题。