Search code examples
javaeclipsemavenbuild-process

run a java application and a web application in a single maven build within a reactor project


I have a reactor project with the following structure

server
- pom.xml (parent) 
-- appserver (has a server socket)
-- webserver (connects to the socket in appserver and gets data)

The appserver pom.xml has a maven-exec-plugin that runs the main method in my java class AppServer.

When I run goal verify in my topmost (server) project my build gets stuck at appserver - exec goal and never proceeds to building / running my webserver.

Ideally I would like to run my appserver first and then my webserver in a single install or verify run on my top most project.

Here is the exec maven plugin configuration in my appserver pom.

<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId> 
<executions> 
  <execution> 
    <goals> 
      <goal>java</goal> 
    </goals> 
  </execution> 
</executions> 
<configuration> 
  <mainClass>somepackage.AppServer</mainClass> 
</configuration> 

I am aware that many other questions of similar nature have been asked before and most answers revolve around use of shell scripts with antrun plugin and almost all of them atleast 3 / 4 years old and I hope there is new solution in a more platform independent manner available now.


Solution

  • There are unfortunately no better solutions than using the maven-antrun-plugin for your use-case. maven-exec-plugin can be used to launch an external process, either in the same VM with the java goal or in a forked VM with the exec goal, but in both cases, it will be blocking; meaning that the plugin will wait for the execution to finish. The possible work-around of starting a Shell script as mentioned here and here works well in Linux environment. It won't work in your case however because you need to support multiple environments.

    With the maven-antrun-plugin, you can use the Exec task and set the spawn attribute to true. This will cause Ant to run the task in the background. A sample configuration would be:

    <plugin>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.8</version>
      <executions>
        <execution>
          <phase> <!-- a lifecycle phase --> </phase>
          <configuration>
            <target>
              <property name="runtime_classpath" refid="maven.runtime.classpath" />
              <exec executable="java" spawn="true">
                <arg value="-classpath"/>
                <arg value="${runtime_classpath}"/>
                <arg value="somepackage.AppServer"/>
              </exec>  
            </target>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

    Note that this uses the maven.runtime.classpath to refer to the Maven classpath containing all runtime dependencies (see here for more info).