TomEE Maven plugin allows to create an all in one executable jar. If you never heard about it, it is different from TomEE Embedded Maven Plugin in the way it creates an actual TomEE and runs it instead of making it embedded – it forks a JVM if you prefer.
The plugin is pretty configurable but if you need some dynamic configuration it can be quite hard. Instead of proposing an advanced configuration for this purpose the plugin now supports a way to execute code before/after the TomEE execution (of course before is the most impacting place :)).
To do so you need to add in your tomee-maven-plugin configuration a preTask:
<plugin> <groupId>org.apache.tomee.maven</groupid> <artifactId>tomee-maven-plugin<artifactId> <version>7.x.y</version> <configuration> <preTasks> <preTask>com.company.MySetup</preTask> <preTasks> </configuration> </plugin>
Then you just need to write a Runnable called MySetup:
public class MySetup implements Runnable { @Override public void run() { // executed before tomee fork is started } }
But how to modify TomEE system properties since there is a fork? For that purpose the runnable can take a Map of attributes. This map is originally intended to share a state between pre and post tasks but a particular entry is used to get from the tasks some additional jvm attributes:
public class MySetup implements Runnable { private final Map<?, ?> state; public MySetup(Map<?, ?> state) { this.state = state; } @Override public void run() { state.put("jvmArgs", "-Dhttp=" + System.getProperty("http", "*8080")); } }
And if you use this class and ${http} as http port in your server.xml then you have a dynamic propagation of the http port from the runner to the tomee instance which means you have a configurable dynamic http port :).
These pre/post hooks can do much more like starting/stopping servers (external activemq, database etc…) and are a nice way to have an all in one server easy to handle.
Hi Romain,
Very useful. Would it possible to have http default value in catalina.properties and override it this way in tomee:exec? I see values are not overriden this way.
Thanks!
It is doable using stand tomcat placeholders and system properties – -DadditionalSystemProperties=”-D….” – on the runner.