如何将一个项目添加为另一个项目的依赖项? [英] How do I add a project as a dependency of another project?

查看:940
本文介绍了如何将一个项目添加为另一个项目的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有没有一种方法可以解决此问题,而无需单独安装MyEjbProject,也无需定义为父模块.

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

我知道这可以通过构建ant来实现,但是想知道是否存在通过maven处理的方法?

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

我们可以使用"pom"创建父项目,并将其他两个移动到父项目下.但是,不幸的是,我无法执行此操作,因为当前我们已经将这两个作为独立项目在CVS中使用,无法更改结构.如果我可以通过pom文件处理此问题,那正是我所寻找的.

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.

推荐答案

假定MyEjbProject不是您拥有的另一个Maven项目,也不是要使用maven构建的Maven项目,则可以使用系统依赖项来链接到该项目的现有jar文件,例如所以

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>

这表示通常通过将其打包为maven项目并构建它或以您似乎已经执行的方式安装它,将软件包安装到存储库通常是更好(更好的方法).

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.

但是,如果它们彼此依赖,则始终可以创建一个单独的父项目(必须是"pom"项目),并将其他两个项目声明为其模块". (子项目不必将第三个项目声明为其父项目).结果,您将为新的父项目获得一个新目录,您很可能还会在其中放置两个像下面这样的独立项目:

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

现在您可以执行此操作(在Maven 2中工作,只需尝试一下即可):

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

<!--parent-->
<project>
  <modules>
    <module>../mywarproject</module>
    <module>../myejbproject</module>
  </modules>
</project>

这篇关于如何将一个项目添加为另一个项目的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆