简体   繁体   中英

Maven WAR: split classes JAR for different packages

I'm developing J2EE web apps with Eclipse (using m2e plugin). If my project contains some classes dedicated to a specific customer, usually I group them to a specific package, like:

  • com.myapp.logic
  • com.myapp.logic.customer1
  • com.myapp.logic.customer2

so at deploy time I can package them to different JARs using the Eclipse .jardesc descriptors, obtaining something like:

  • myapp.jar
  • myapp-customer1.jar
  • myapp-customer2.jar

The advantage of this approach is that if I need to update only customer1 classes I can update only customer1 jar (if dependencies are not changed).

Now I would like to use Maven in my webapp projects but I can't find a solution to keep jars separated. Using WAR plugin with archiveClasses configuration I can deploy only one JAR for my classes and I can't configure how to split packages into different JARs.

I've considered to use overlays but the entire project must be splitted into several projects and may become too complex (especially if I have 20/30 customers). I would prefer only a "lighter" split for classes deployment.

Alternatively, is there a way with Maven to package my classes into different JARs before WAR assembly (using different executions ) and then include them into WAR ignoring standard deploy of classes (or equivalent JAR) ?

If you want to use Maven, would be better to create a multi module project if you want to separate code in different jars.

So you would have :

  1. One parent pom (packaging pom)
  2. One or more utility jar (packaging jar)
  3. One war to pack the application (packaging war)

Here is a full example (that actually build an EAR but you can skip the EAR if you don't need) :

https://howtodoinjava.com/maven/multi-module-project-eclipse/

If you need to separate for customers you could do that with maven profiles:

    <profile>
        <id>customer2</id>
        <dependencies>
             <dependency>
                <groupId>myGroup</groupId>
                <artifactId>artificatCustomer2</artifactId>
            </dependency>        


    <profile>
        <id>customer1</id>
        <dependencies>
             <dependency>
                <groupId>myGroup</groupId>
                <artifactId>artificatCustomer1</artifactId>
            </dependency>        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM