Search code examples
maventomcatjakarta-eewildflyweb-deployment-project

How can I create (deploy) multiproject?


I have three pools(apps) that transform data sequentially and communicate with each other by socket (or get, post - nevermind). And I have servlets, that accept data and transfer data to one of the starting pool. How can I run it all in one container (tomcat, wildfly)? Make 3 jar, 1 war artifact or something else? that should be written in the pom.xml?


Solution

  • Well, communication between the modules is important:

    • If you are content with them communicating by direct, local API invocation, you must necessarily build one single war containing the three modules (typically one JAR for each module).

    • But if you want a distributed application, the communication should be remote (either through TCP sockets, RMI, HTTP, ReST, etc). In this way, you may build one assembly for each module: It should be a WAR if the module contains servlets/JSPs, or a JAR if it does not.

    In Maven, this translates to:

    1. Create a parent project which contains all of the modules, with packaging=pom, and one <module> declaration for each module.
    2. Then, create a module project (within the parent) for each module, with package=jar or war (depending on the above decission).
    3. Set the proper dependencies between them in case you need to expose their public APIs as the way to inter-communicate.

    It is also important to deploy and start each module depending on its type (as commented by @Carlitos Way):

    • WARs are necessarily deployed to a servlet container (Tomcat, for example).
    • JARs can be started from the command-line with a shell script.

    Start an application which listens to a port

    Each one of the non-web modules should have a main class which will be in charge of receiving the required arguments from the command line (at least the port number) and start the listening class - whatever it is.

    package module.cmd;
    
    public class MyMainClass
    {
        public static void main(String[] arguments)
        {
            int port=Integer.parseInt(argument[0]);
            new MyListener(port).start();
        }
    }
    

    To run it, it would be enough to execute:

    mvn exec:java -Dexec.mainClass="module.cmd.MyMainClass" -Dexec.args="8001"