Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven (EJB) project with client and server artifacts

Tags:

maven-2

Here's my variation on the "multiple artifacts from Maven build" question:

I'm porting from Ant to Maven. My application is an EJB server that is packaged as an EAR, but it also exposes a client JAR for use by other client apps. This jar contains the EJB interfaces, facade class and some helpers.

I know that the Maven way is to have one artifact per project (POM); however, both artifacts (server EAR and client JAR) need to be built from the same source tree - server and client share, for example, the EJB and 'home' interfaces.

How do I do this in Maven?

Do I have one project containing two POMs, say server-pom.xml & client-pom.xml? I was thinking I could also have a parent POM (pom.xml) that can be used to build both client and server with one foul swoop? However, the lifecycles diverge after the 'package' phase, since the server has to go through assembly (tar/gzip), while the client is done after 'package' and can simply be installed into the repository.

Any advice/experience on the best way to approach this?

like image 596
Cornel Masson Avatar asked Dec 18 '09 15:12

Cornel Masson


2 Answers

I know that the Maven way is to have one artifact per project (POM); however, both artifacts (server EAR and client JAR) need to be built from the same source tree - server and client share, for example, the EJB and 'home' interfaces.

There are some exception to the "one artifact per project" rule, EJB projects being one of them. So, the maven-ejb-plugin can be configured to generate the EJB jar and a client JAR like this:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ejb-plugin</artifactId>
        <configuration>
           <generateClient>true</generateClient>
        </configuration>
      </plugin>
    </plugins>
  </build>

To use the ejb client in another project, just add it as dependency of <type>ejb-client</type>:

<project>
  [...]
  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>ejb-project</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>ejb-client</type>
    </dependency>
  </dependencies>
  [...]
</project>

See Generating an EJB client, Using the ejb-client as a dependency and the documentation of the ejb mojo for more details (including how to customize the classes included/excluded from the ejb client).

like image 77
Pascal Thivent Avatar answered Nov 16 '22 02:11

Pascal Thivent


I've used multimodule projects to solve this before.

project/
    pom.xml  <- type=pom, lists sub modules
    ejb/
        src/main/java, etc.
        pom.xml <- type=ejb, describes ejb module, has dependency on "jar" module
    jar/ 
        src/main/java, etc.
        pom.xml <- type=jar, simple, builds jar
    ear/ 
        pom.xml <- type=ear, has reference to ejb module that it should use
    ...

I've use this approach for rather complex projects that may have a dozen different modules that must be built together. See the ear docs for referencing the ejb.

The parent pom.xml uses the modules tag:

<modules>
    <module>jar</module>
    <module>ejb</module>
    <module>ear</module>
</modules>

And the child pom.xml's use the parent tag:

<parent>
    <groupid>mygroup</group>
    <artifactId>parentName</artifactId>
</parent>
like image 41
John Paulett Avatar answered Nov 16 '22 01:11

John Paulett