开发者

Java 6 Enterprise project (WAR) in Maven3 (Modules)

I have Java 6 Enterprise project (WAR) in Maven3 created base on

  • http://www.sonatype.com/books/mvnex-book/reference/multimodule-web-spring.html
  • http://www.znetdevelopment.com/blogs/2009/02/18/j2ee-project-structures

with this structure:

  • EnterpriseProject (Super POM)
    • ep-model (produces JAR)
    • ep-persist (produces JAR)
    • ep-service (produces JAR)
    • ep-ui (produces JAR)
    • ep-webapp (puts it all together and produces WAR)

with dependencies (->):

ep-webapp -> ep-ui -> ep-model
                   -> ep-service -> ep-model
                                 -> ep-persist -> ep-model 

When I run Enterprise Project goal "package", war is created in EnterpriseProject\ep-webapp\target\ and this war contains all needed libraries: ep-model, ep-persist, ep-service, ep-ui.

Q1) Is this solutions right?

Q2) How should I add additional modules (how set dependencies)?

  • ep-common (produces JAR)
  • ep-ws (webservices; produces JAR)

Should I only add ep-common dependency into ep-weapp pom (just for adding ep-common lib to war) or everywhere where I use ep-common?

ep-webapp -> ep-ui -> ep-model
                   -> ep-service -> ep-model
                                 -> ep-persist -> ep-model 
          -> ep-common
          -> ep-ws

Thanks a lot for tips

My present solution: EnterpriseProject Super pom:

<modelVersion>4.0.0</modelVersion>
<groupId>com.ep</groupId>
<artifactId>enterprise-project</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Enterprise Project</name>
<modules>
    <module>../ep-model</module>
    <module>../ep-persist</module>
    <module>../ep-service</module>
    <module>../ep-ui</module>
    <module>../ep-webapp</module>
</modules>

ep-webapp pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ep/pom.xml</relativePath>
</parent>

<artifactId>ep-webapp</artifactId>
<packaging>war</packaging>
<name>webapp</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-ui</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

ep-ui pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ep/pom.xml</relativePath>
</parent>

<artifactId>ep-ui</artifactId>
<packaging>jar</packaging>
<name>ui</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-model</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-service</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

ep-model pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ep/pom.xml</relativePath>
</parent>

<artifactId>ep-model</artifactId>
<packaging>jar</packaging>
<name>model</name>

ep-service pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ep/pom.xml</relativePath>
</parent>

<artifactId>ep-service</artifactId>
<packaging>jar</packaging>
<name>service</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-model</artifactId>
        <vers开发者_C百科ion>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-persist</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

ep-persist pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../ep/pom.xml</relativePath>
</parent>

<artifactId>ep-persist</artifactId>
<packaging>jar</packaging>
<name>persist</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ep-model</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>


Q1) Is this solutions right?

Seems a well-structured set of POMs to me.

Q2) How should I add additional modules (how set dependencies)? [ep-common, ep-ws]

As for ep-commons, I'm guessing it's stuff common to all modules (common types, exceptions definitions and such) — if so, you need to reference it wherever you use it, otherwise Maven won't be able to compile it, as it creates classpath basing on dependencies. I understand your worry (that you have to repeat the dependency many times), but since you use variables ${project.groupId} and ${project.version}, you are not running the risk of referencing different version of the same module in different dependent projects.

If you are a Maven purist, and I'll risk a claim you might be, you could add the ep-commons to a <dependencyManagement> section of the root POM, but that will only save you one line in each POM (for version).

One notice: if you're going to use the release plugin in future, the "${}" references will have to be replaced by hard-coded group id and version strings, but the plugin takes care of that itself.

As for ep-ws, I'm not quite sure what's there (JAX-WS classes generated from WSDLs?). If only that, then do the same as for ep-commons.


Thank you MaDa for your clear answer. So if I understand correctly I can (if I'm purist :-)) add all dependencies also into root POM into dependencyManagement and remove version from I children where is given dependency used.

I have the last question about ep-ws. Probably I will use SpringWS & JAXB (or Castor) (because I prefer top-down approach and manual work without generating class by tool and I don't know if it is possible by JAX-WS). Q3 ep-ws depends on ep-model and ep-service (transitively on ep-persist), but generally not on ep-webapp but I need add this dependency into ep-webapp just for adding ep-ws library into war, am'I right?

About dependencyManagement this blog


So I reworked my project and I hope that this is quite ok:

Java 6 Enterprise project (WAR) in Maven3 (Modules)

EnterpriseProject super pom:

<modelVersion>4.0.0</modelVersion>
<groupId>com.ep</groupId>
<artifactId>enterprise-project</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Enterprise Project</name>

<modules>
    <module>common</module>
    <module>model</module>
    <module>persist</module>
    <module>service</module>
    <module>ui</module>
    <module>webapp</module>
    <module>ws</module>
</modules>

<build>
    <defaultGoal>package</defaultGoal>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>webapp</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>model</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>persist</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>service</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ui</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>ws</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

webapp pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>webapp</artifactId>
<packaging>war</packaging>
<name>webapp</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ui</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>ws</artifactId>
    </dependency>

common pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>common</artifactId>
<packaging>jar</packaging>
<name>common</name>

model pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>model</artifactId>
<packaging>jar</packaging>
<name>model</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>common</artifactId>
    </dependency>
</dependencies>    

persist pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>persist</artifactId>
<packaging>jar</packaging>
<name>persist</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>common</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>model</artifactId>
    </dependency>
</dependencies>

service pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service</artifactId>
<packaging>jar</packaging>
<name>service</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>common</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>model</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>persist</artifactId>
    </dependency>
</dependencies>

ui pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>ui</artifactId>
<packaging>jar</packaging>
<name>ui</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>common</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>model</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>service</artifactId>
    </dependency>
</dependencies>

ws pom:

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.ep</groupId>
    <artifactId>enterprise-project</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>ws</artifactId>
<packaging>jar</packaging>
<name>ws</name>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>common</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>model</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>service</artifactId>
    </dependency>
</dependencies>   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜