93

There are two independent projects (myWarProject and MyEjbProject). So when I build the myWarProject I need to install the MyEjbProject in to the local repository, so then I can define in the myWarProject as dependency and package the myWarProject successfully.

Is there a way to handle this without install the MyEjbProject separately and also without defining as parent modules.

I know this can be achieved through ant build, but wonder whether there is a way to handle through maven?

We can create the parent project with "pom" and move the other two under the parent project. However, unfortunately I cannot do this as currently we already have those two as separate projects in the CVS which I cannot change the structure. If I can handle this through pom file, this is what Im looking for.

nbro
  • 15,395
  • 32
  • 113
  • 196
kds
  • 28,155
  • 9
  • 38
  • 55
  • Hi Simon, Thanks for the quick reply. In fact MyEjbProject is another maven project which I need to build when I build the myWarProject. Is there a way to define the MyEjbProject pom in the myWarProject? – kds Mar 13 '13 at 11:27
  • Please tell me: in what way do they depend on each other? Right now it looks as if the war project depends on the ejb for building a complete web application. In this case the two projects would not be so independent after all. – Simon Hellinger Mar 13 '13 at 12:11
  • I edited my answer (added something at the end) Hopefully this will be closer to your question. – Simon Hellinger Mar 13 '13 at 12:23
  • A second edit to my answer, maybe we got lucky this time :) – Simon Hellinger Mar 14 '13 at 13:21
  • Here an example: https://medium.com/javarevisited/how-do-i-add-a-project-as-a-dependency-of-another-project-using-maven-4508283c01d1 – javing Jan 24 '22 at 16:58

1 Answers1

84

Assuming the MyEjbProject is not another Maven Project you own or want to build with maven, you could use system dependencies to link to the existing jar file of the project like so

<project>
   ...
   <dependencies>
      <dependency>
         <groupId>yourgroup</groupId>
         <artifactId>myejbproject</artifactId>
         <version>2.0</version>
         <scope>system</scope>
         <systemPath>path/to/myejbproject.jar</systemPath>
      </dependency>
   </dependencies>
   ...
</project>

That said it is usually the better (and preferred way) to install the package to the repository either by making it a maven project and building it or installing it the way you already seem to do.


If they are, however, dependent on each other, you can always create a separate parent project (has to be a "pom" project) declaring the two other projects as its "modules". (The child projects would not have to declare the third project as their parent). As a consequence you'd get a new directory for the new parent project, where you'd also quite probably put the two independent projects like this:

parent
|- pom.xml
|- MyEJBProject
|   `- pom.xml
`- MyWarProject
    `- pom.xml

The parent project would get a "modules" section to name all the child modules. The aggregator would then use the dependencies in the child modules to actually find out the order in which the projects are to be built)

<project>
   ...
   <artifactId>myparentproject</artifactId>
   <groupId>...</groupId>
   <version>...</version>

   <packaging>pom</packaging>
   ...
   <modules>
     <module>MyEJBModule</module>
     <module>MyWarModule</module>
   </modules>
   ...
</project>

That way the projects can relate to each other but (once they are installed in the local repository) still be used independently as artifacts in other projects


Finally, if your projects are not in related directories, you might try to give them as relative modules:

filesystem
 |- mywarproject
 |   `pom.xml
 |- myejbproject
 |   `pom.xml
 `- parent
     `pom.xml

now you could just do this (worked in maven 2, just tried it):

<!--parent-->
<project>
  <modules>
    <module>../mywarproject</module>
    <module>../myejbproject</module>
  </modules>
</project>
Neuron
  • 5,141
  • 5
  • 38
  • 59
Simon Hellinger
  • 1,309
  • 12
  • 19
  • Hi Simon, Yes, as you said we can create the parent project with "pom" and move the other two under the parent project. However, unfortunately I cannot do this as currently we already have those two as separate projects in the CVS which I cannot change the structure. If I can handle this through pom file , this is what Im looking for. – kds Mar 14 '13 at 11:57
  • You could edit your question with this additional information you just gave in your comment: It might clarify your problem for others trying to answer! – Simon Hellinger Mar 14 '13 at 12:09
  • Just as a last resort if nothing else helps: Since they are already separated, could you not just check them out from CVS into the appropriate subdirectory on your local file system? Check out MyEJBProject into directory "parent/myejbproject" and check out MyWarProject into "parent/mywarproject". It's probably a maintainance nightmare, though... – Simon Hellinger Mar 14 '13 at 12:15
  • Thanks Simon. I was wondering what is the best practice to handle these type of scenario? – kds Mar 14 '13 at 12:23
  • Just added another solution, maybe this could finally help. About your last question in the comments: I myself actually never had that scenario. Whenever we build separate modules, we put them in a central repository (such as sonatype nexus) for other projects to reference. So you build the first project (say, the ejb project), deploy it, then build the other one (the war project) using the already built ejb project artifact, and not the maven project itself – Simon Hellinger Mar 14 '13 at 12:27
  • This is how initially build the project and add the ejb artifact to the war pom. However, Still I need to get this done the way I have mentioned initially. In ant I could do this by calling a another build.xml from the current ant file. – kds Mar 15 '13 at 04:15
  • along with my recent edit2 in the answer I thought that maven would build your second project automatically, so there would be no need for calling that pom directly. Anyways, I seem to not know the answer to your problem, I found no way for calling one pom from another, sorry. :/ Let's wait and see what others have to say. – Simon Hellinger Mar 15 '13 at 06:00
  • Hi, I am trying to import a maven prohìject into another maven project. So I added in the dependency of the project the jar as you described in your answer. Anyway I don't know ho to use it! What should I import in the code? I tried different formulas in the import section but none where right for Eclipse. – Francesco Pegoraro Jul 31 '18 at 09:39