Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a project as a dependency of another project?

Tags:

maven-3

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.

like image 429
kds Avatar asked Mar 13 '13 10:03

kds


People also ask

How do I add dependency from one project to another?

Right-click the utility project, and select Maven>Add Dependency. Type a dependency name in the Enter groupID… field (e.g., commons-logging) to search for a dependency. Select the dependency, and click OK.

How do I add a project as a dependency of another project in Eclipse?

Instead open the project properties of the second project and select "Java Build Path". On the right side on the "Projects" tab you can add your first project as "required project on the build path". The class files of project 1 are now added to the class path of project 2.


1 Answers

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> 
like image 185
Simon Hellinger Avatar answered Nov 12 '22 03:11

Simon Hellinger