views:

423

answers:

2

In our Maven project, we are trying the following directory structure (with about 80 projects total, only a few are shown so that you get the idea):

myappli      (pom)
-- module1       (pom)
--|-- utils    (pom)
--|-- ejb       (pom)
--|--|-- myappli-module1-a-ejb    (jar)
--|--|-- myappli-module1-b-ejb    (jar)
--|-- war       (pom)
--|-- applet       (pom)
...
-- module6       (pom)
--|-- utils       (pom)
--|-- ejb       (pom)
--|--|-- myappli-module6-c-ejb    (jar)
--|-- war       (pom)
--|-- applet       (pom)

Note: This is a flat structure for Maven, as all non-leaf projects have a packaging value of "pom". (cf BetterBuildsWithMaven book).

We define the dependency versions in "dependencyManagement", in the "myappli" pom. This works fine.

Our problem is with the reuse of the dependencies themselves. For example, the ejb dependencies are common to all ejb projects (by design). We don't want to cut'n-paste, and maintain all that with each change!

We were thinking to use some "import notion" for the ejb dependencies, and define our ejb dependencies once at the application level. Our unsuccessful attempts were:

  • The Maven "parent pom" notion would be fine, but it is already used by the modules, so it is not available for our requirement.
  • No import facility found in Maven (except for dependencyManagement)
  • XML entity definition is not recognized. We tried a pom like the following, and got the error
    "Reason: Parse error reading POM. Reason: could not resolve entity named 'ejbDependencies'":

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE project [
    <!ENTITY ejbDependencies SYSTEM "./ejbDependencies.txt">
    ]>
    <project ...
    ...
    &ejbDependencies;
    ...


Edited : I am trying the solution suggested by Robert, but something is wrong.

When I compile my ejb project, it doesn't find the dependencies themselves. I get an error when compiling (mvn compile), saying the javax.ejb package is missing.

Note: I did run "mvn install" on the dependencies project before.

This is my configuration :

<project ...>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.company</groupId>
    <artifactId>myproj-maven</artifactId>
    <version>3.1-SNAPSHOT</version>
  </parent>

  <groupId>com.company</groupId>
  <artifactId>myproj-maven-ejb</artifactId>
  <version>${myproj-version}</version>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>javax.ejb</groupId>
      <artifactId>ejb</artifactId>
    </dependency>

    <dependency>
      <groupId>ojdbc</groupId>
      <artifactId>ojdbc</artifactId>
    </dependency>
  </dependencies>
</project>

---------------------------------
<project ...>
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.company</groupId>
    <artifactId>myproj-identite-ejb</artifactId>
    <version>3.1-SNAPSHOT</version>
  </parent>

  <groupId>com.company</groupId>
  <artifactId>myproj-identite-metier</artifactId>
  <name>SNR IDENTITE METIER</name>
  <version>2.0.1</version>
  <packaging>ejb</packaging>

  <dependencies>
    <dependency>
      <groupId>com.company</groupId>
      <artifactId>myproj-maven-ejb</artifactId>
      <version>${myproj-version}</version>
      <type>pom</type>
    </dependency>
  </dependencies>
</project>


I don't know if it changes something, but we have a hierarchy that relates the two poms.
We have a strict Maven structure, where each directory declares all subdirectories as maven modules, and each subdirectory declares the parent as a maven parent.
And the common parent directory is part of this structure.

+---maven
|   \---ejb
+---identite
|   +---ejb
|   |   \---SNR_IDENTITE_METIER


Edited:

The answer given by reef seem correct. It is impossible to do with Maven, because our dependency are provided, and therefore not transitive :-(

We really have many problems with setup up Maven. So many little things just don't work. Today I found out that the site target cannot handle properties, that we are using for version numbers!

+4  A: 

You can use pom dependencies to import dependencies into arbitrary projects.

A pom project can look similar to:

<project>
  <groupId>com.example</groupId>
  <artifactId>persistence-deps</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>${hibernateVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-annotations</artifactId>
      <version>${hibernateAnnotationsVersion}</version>
    </dependency>
  </dependencies>
</project>

And is imported as:

<dependency>
  <groupId>com.example</groupId>
  <artifactId>persistence-deps</artifactId>
  <version>1.0</version>
  <type>pom</type>
</dependency>

See Maven, the definitive guide - Grouping Dependencies for details.

Robert Munteanu
+1 for an answer that looks perfect. I'm trying it right now. (note : a few minutes ago, you also edited my "typo" in another answer ;-) )
KLE
I tried this. I run "mvn install" on the dependencies project. Then I compile my ejb project, it doesn't find the dependencies themselves.
KLE
To check, I run a help:effective-pom. It doesn't replace the dependency by the set of dependencies. It this normal? How could I check where I am wrong?
KLE
I can't speak for your specific scenario, but I would try to either replicate it with a very simple project or find a working example somewhere else.
Robert Munteanu
+2  A: 

Hi,

Do your imported dependencies have a provided scope? Indeed this scope is not transitive (see http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency%5FScope).

This could be the reason of the non-replacement.

reef
+1 it is the reason indeed, a small test proved it. Thanks.
KLE