如何将一个项目添加为另一个项目的依赖项?

有两个独立的项目(myWarProjectMyEjbProject)。因此,当我构建 myWarProject时,我需要将 MyEjbProject安装到本地存储库中,这样我就可以在 myWarProject 中将其定义为依赖项,并成功地打包 myWarProject。

有没有一种方法可以在不单独安装 MyEjbProject和不定义为父模块的情况下处理这个问题。

我知道这可以通过构建蚂蚁来实现,但是不知道是否有一种方法可以通过 maven 来处理?

我们可以使用“ pom”创建父项目,并将另外两个项目移动到父项目下。然而,不幸的是,我不能这样做,因为目前我们已经有这两个作为独立的项目在 CVS,我不能改变结构。如果我能处理这通过 POM 文件,这就是我要找的。

134215 次浏览

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>