TomEE exec Maven plugin: modify the runner JVM!


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.

Advertisements

2 thoughts on “TomEE exec Maven plugin: modify the runner JVM!

  1. Vicente Rossello

    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!

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s