I am using such @Service class to run services after application 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();
}
}
When I creating standalone executable jar package mvn clean package -Pproduction, application is started and immediately crashes because setting for production profile is not visible on development environment.
Is it possible to create package but do not start the application itself? Possibly I can change how the services start but i would like to keep the manual start.
Edit: One more problem with this setup - if I have some methods annotated with @Scheduled then mvn clean package starts the application and it waits, the Maven build is not completed.
Edit 2: When I use @Scheduled application start and stay alive during mvn clean package.
@Scheduled(timeUnit = TimeUnit.MINUTES, fixedDelay = 15)
public void scheduled() {
runner.run();
}
}
Edit 3: Related question: How to disable @Scheduled while building a Spring-boot project - without resolution.
Edit 4: Console log when @Scheduled(timeUnit = TimeUnit.SECONDS, fixedDelay = 5, initialDelay = 1) is active. (I let run 5 scheduled executions and cancel application with cmd+c at the end).
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] ------------------------------------------------------------------------
Edit 5: Console log with @PostConstruct method - now I do not have any service to executed int this method, but if I would, it will be executed.
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] ------------------------------------------------------------------------
By default, there is a test class annotated with @SpringBootTest, in the src.test.java... package, to verify the loading of the application context. That test, and other tests if exist, is run during the Maven test phase.
You can delete the default test class (if there are no other test classes) or skip the tests using the following command:
mvn clean package -DskipTests
Edit:
After OP provided the console output, it appears the execution for the run goal was defined in the spring-boot-maven-plugin which was causing the running of the application during test-compile Maven phase. Removing it fixes the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With