开发者

Maven Java EE and module project pom

i am new to maven.

i have a Java EE Web project and a model project,web 开发者_JAVA百科project is depend on model because module has some standard classed which need to be jar in web.

web needs model jar file.

how can i write the 2 projects which one is depend on another ?

give me a sample coding on both project pom.


A typical maven multi-modules project structure would involve 3 modules here: an aggregating parent module (allowing to start a multi-module build on all modules), the model module and the web module. Something like this:

.
├── mymodule
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   └── resources
│       └── test
│           ├── java
│           └── resources
├── mywebapp
│   ├── pom.xml
│   └── src
│       └── main
│           ├── resources
│           └── webapp
│               ├── index.jsp
│               └── WEB-INF
│                   └── web.xml
└── pom.xml

Where the parent pom.xml (at the root) would be like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.stackoverflow</groupId>
  <artifactId>Q4176120</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Q4176120 - Parent POM</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    ...
  </dependencies>
  <modules>
    <module>mywebapp</module>
    <module>mymodule</module>
  </modules>
</project>

The mymodule/pom.xml would be a regular POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q4176120</artifactId>
    <groupId>com.stackoverflow</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <!--groupId>com.stackoverflow</groupId--> <!-- inherited -->
  <artifactId>mymodule</artifactId>
  <packaging>jar</packaging>
  <name>Q4176120 - Module</name>
  <dependencies>
    ...
  </dependencies>
</project>

And mywebapp/pom.xml would declare a dependency on the mymodule artifact:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>Q4176120</artifactId>
    <groupId>com.stackoverflow</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <!--groupId>com.stackoverflow</groupId--> <!-- inherited -->
  <artifactId>mywebapp</artifactId>
  <packaging>war</packaging>
  <name>Q4176120 - Maven Webapp</name>
  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>mymodule</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>Q4176120</finalName>
  </build>
</project>

With this structure, you can start a multi-modules build from the parent directory i.e. run a goal on all modules (and maven will calculate the right build order):

$ mvn install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] Q4176120 Parent POM
[INFO] Q4176120 Module
[INFO] Q4176120 Maven Webapp
[INFO]                                                                         
...

Note that I also used inheritance in the above samples (mymodule and mywebapp inherit from a parent that is declared in the <parent> element) so that I can group common parts in a parent POM and avoid repeating things. This is not mandatory, you can use aggregation without inheritance but this is very handy and aggregation and inheritance often go together in practice.

Resources

  • Maven Documentation
    • Project Inheritance vs Project Aggregation
  • Maven Book
    • 3.6.2. Multi-module vs. Inheritance


You create 2 maven projects: web project and jar project. web project will depend on the model project. So you will add the model project as a dependency in the web projects pom.xml

I suggest you read maven basics here: http://www.sonatype.com/books/maven-book/

You model project pom.xml might look like this

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>>model-module</artifactId>
<name>My Model Module</name>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>

Your web app pom.xml might look like this. notice the dependencies section

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.abc</groupId>
<artifactId>>my-web-app</artifactId>
<name>My First Web Application</name>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
    <dependency>
      <groupId>com.abc</groupId>
      <artifactId>model-module</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    </dependency>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜