简体   繁体   English

如何在项目上下文之外包含maven模块?

[英]How to include a maven module outside of the project context?

I'd like to have a module in some kind of global project directory, so that I can include that module in all other projects that use that common code. 我想在某种全局项目目录中有一个模块,这样我就可以将该模块包含在使用该公共代码的所有其他项目中。

But how can I then tell a maven parent pom to include and compile this global shared module? 但是,我怎么能告诉maven父pom包含并编译这个全局共享模块呢? The following does not work: 以下不起作用:

svn/MyGlobalProject/project-commons/pom.xml           //should be shared among different projects
svn/MyProject/web-parent/trunk/pom.xml                //the parent pom used to build the application
svn/MyProject/web-parent/trunk/project-domain/pom.xml //submodule 1
svn/MyProject/web-parent/trunk/project-web/pom.xml    //submodule 2

parent pom.xml: parent pom.xml:

<project>
        <groupId>de.project</groupId>
    <artifactId>project-parent</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>project-domain</module>
        <module>project-web</module>
        <module>../project-commons</module> <!-- Error -->
    </modules>
</project>

mvn package results in: mvn package导致:

Child module trunk\project-commons of trunk\pom.xml does not exist

If you run mvn install on that global project, it will be installed in your local repository. 如果在该全局项目上运行mvn install ,它将安装在本地存储库中。 Your other projects can then reference it as a dependency: 然后,您的其他项目可以将其作为依赖项引用:

<dependency>
  <groupId>whatever</groupId>
  <artifactId>project-commons</artifactId>
  <version>1.0</version>
</dependency>

The downside of this simplistic approach is that your other projects won't compile until you've checked-out project-commons and run mvn install . 这种简单方法的缺点是,在您检出project-commons并运行mvn install之前,您的其他项目将无法编译。

A more advanced approach is to deploy a network-accessible repository (such as Artifactory or Nexus) which you can deploy global artifacts to. 更高级的方法是部署可以部署全局工件的网络可访问存储库(例如Artifactory或Nexus)。 Artifactory has a community edition which is free. Artifactory有一个免费的社区版本。 You can then list this repository in your settings file and Maven will resolve artifacts that are uploaded to it. 然后,您可以在设置文件中列出此存储库,Maven将解析上传到它的工件。

Using relative path to include some submodules is not a good practice...you will have a lot of problems. 使用相对路径来包含一些子模块不是一个好习惯......你会遇到很多问题。

Can not you just put the common project as a dependency of the parent module... 你不能把公共项目作为父模块的依赖项......

In this way you will have that "common project" in all the submodule that declare the parent project as a parent... 通过这种方式,您将在所有子模块中具有“公共项目”,将父项目声明为父项...

Why do you want to compile your "common" project every time you compile the parent pom? 为什么每次编译父pom时都要编译“常用”项目?

Edited: 编辑:

YOUR SVN: 你的SVN:

svn/MyGlobalProject/project-commons/pom.xml           //should be shared among different projects
svn/MyProject/web-parent/trunk/pom.xml                //the parent pom used to build the    application
svn/MyProject/web-parent/trunk/project-domain/pom.xml //submodule 1
svn/MyProject/web-parent/trunk/project-web/pom.xml    //submodule 2

PARENT POM.XML: 父母POM.XML:

<project>
    <groupId>de.project</groupId>
    <artifactId>project-parent</artifactId>
    <packaging>pom</packaging>

    <dependencies>
       <dependency>
      <groupId>de.project</groupId>
      <artifactId>project-commons</artifactId>
      <version>${common.project.version}</version>
       <dependency>
    <dependencies>

<modules>
    <module>project-domain</module>
    <module>project-web</module>
</modules>

Just like this... in that way project-domain and project-web will inherit that dependency, and you will able to use it everywhere you want in submodules... 就像这样......通过这种方式,project-domain和project-web将继承该依赖关系,并且您将能够在子模块中的任何位置使用它...

Than the use of dependency management could be a good improvements 比使用依赖管理可能是一个很好的改进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM