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

不问归期 提交于 2019-12-23 02:34:17

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/35437358/run-a-java-application-and-a-web-application-in-a-single-maven-build-within-a-re

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!