java Maven 多模块中的自定义 pom.xml 文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12102388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:36:02  来源:igfitidea点击:

custom pom.xml filename in maven multimodule

javamaven-2maven-3

提问by Toilal

I have a maven3 multimodule project, and for a strange reason i need to customize POM filename for one of my child module (ie: module-pom.xml)

我有一个 maven3 多模块项目,出于一个奇怪的原因,我需要为我的一个子模块(即:module-pom.xml)自定义 POM 文件名

Is it possible to configure this in the parent pom ?

是否可以在父 pom 中配置它?

The strange reason is a bit long to explain sorry, but you will have the plain context.

奇怪的原因解释抱歉有点长,但你会有简单的上下文。

Context

语境

  • I'm working on a closed source project that also use LGPLed projects. this project is called main

  • I want mainto declare modules of every projects, closed and opened. The full build should be made with a unique mvn clean packagecommand.

  • Inside the mainreactor project, i have lgpl-reactormultimodule project containing 3 LGPL modules (API, Plugins and Distribution project). Some developper will have access to lgpl-reactoronly, so i also want this project to build from its folder with mvn clean packagecommand, like a fully standalone project.

  • I also have main-distproject that is a maven-assembly-plugin only project (to build the distribution).

  • 我正在开发一个也使用 LGPLed 项目的闭源项目。这个项目叫做main

  • 我想main声明每个项目的模块,关闭和打开。完整的构建应该使用唯一的mvn clean package命令进行。

  • main反应器项目中,我有lgpl-reactor包含 3 个 LGPL 模块(API、插件和分发项目)的多模块项目。一些开发人员lgpl-reactor只能访问,所以我也希望这个项目使用mvn clean package命令从它的文件夹中构建,就像一个完全独立的项目。

  • 我还有main-dist一个项目,它是一个仅 maven-assembly-plugin 的项目(用于构建发行版)。

The problem

问题

  • If I add parent reference to lgpl-reactorpom.xml, the global mainbuild works perfectly, and assembly created by main-distis valid, but the standalone build of lgpl-reactor fails (main pom.xml not found).

  • If I remove parent reference from lgpl-reactorpom.xml, the standalone lgpl-reactorbuild works, but assembly created by main-distis NOT valid (missing.

  • 如果我添加对lgpl-reactorpom.xml 的父引用,则全局main构建完美运行,并且由创建的程序集main-dist有效,但 lgpl-reactor 的独立构建失败(未找到主 pom.xml)。

  • 如果我从lgpl-reactorpom.xml 中删除父引用,则独立lgpl-reactor构建有效,但由创建的程序集main-dist无效(丢失。

How to solve this ?

如何解决这个问题?

  • use another POM file module-pom.xmlfor lgpl-reactormodule declaration inside mainmodules declaration list. When perfoming the full build from mainproject, module-pom.xmlcontains reference to parent POM and is working properly.

  • use the default POM file pom.xmlfor standalon build of lgpl-reactor. This POM can hardly reference the parent pom with the relativePathproperty of <parent>tag

  • 使用另一个 POM 文件module-pom.xmllgpl-reactormain模块声明列表中进行模块声明。从main项目执行完整构建时,module-pom.xml包含对父 POM 的引用并且工作正常。

  • 将默认 POM 文件pom.xml用于lgpl-reactor. 这个POM很难引用带有tagrelativePath属性的父pom<parent>

But HOW can i do that ? if possible ? Or is there any better solution ?

但我怎么能做到呢?如果可能的话 ?或者有没有更好的解决方案?

Directory of the Main Reactor Project

主反应堆项目目录

lgpl-lib [LGPL Library]
lgpl-ext [LGPL Reactor Project]
closed-core [Closed source module]
closed-spring [Closed source module]
closed-web [Closed source module]
closed-webserver [Closed source module]
main-dist [Main assembly module]
pom.xml [Main Reactor POM]

Directory of the LGPL Reactor Project

LGPL 反应堆项目目录

lgpl-api [LGPL API module]
lgpl-plugins [LGPL Plugins module]
lgpl-ext-dist [LGPL assembly module]
main-pom.xml [Main Reactor POM, copy of main pom.xml]
pom.xml [Standalone LGPL Reactor POM]
module-pom.xml [Module LGPL Reactor POM]

Main Reactor POM(pom.xml & lgpl-reactor/main-pom.xml)

主反应器 POM(pom.xml & lgpl-reactor/main-pom.xml)

    ...
    <modelVersion>4.0.0</modelVersion>
    <groupId>main.group</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Main</name>
    <packaging>pom</packaging>
    ...
    <modules>
        <module>closed-core</module>
        <module>closed-web</module>
        <module>closed-webserver</module>
        <module>closed-spring</module>
        <module>lgpl-reactor</module>
        <module>lgpl-lib</module>
        <module>main-dist</module>
    </modules>
    ...

lgpl-reactor/pom.xml

lgpl-reactor/pom.xml

...
<modelVersion>4.0.0</modelVersion>
<artifactId>lgpl-reactor</artifactId>
<name>LGPL Reactor</name>
<packaging>pom</packaging>

<parent>
    <groupId>main.group</groupId>
    <artifactId>main</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>main-pom.xml</relativePath>
</parent>
...
    ...
<modules>
    <module>lgpl-api</module>
    <module>lgpl-plugins</module>
    <module>lgpl-ext-dist</module>
</modules>
    ...

pom.xml of main-dist

pom.xml 的 main-dist

<project>
    <parent>
        <groupId>main.group</groupId>
        <artifactId>main</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>main-dist</artifactId>

    <packaging>pom</packaging>

    <description>Main Distribution</description>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>plugins-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>assembly.xml</descriptor>
                            </descriptors>
                            <appendAssemblyId>false</appendAssemblyId>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>closed</groupId>
            <artifactId>closed-webserver</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>closed</groupId>
            <artifactId>closed-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

assembly.xml of main-dist

汇编.xml main-dist

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>plugins-assembly</id>
    <formats>
        <format>dir</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <fileSets>
        <fileSet>
            <directory>../closed-webserver/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>

    <dependencySets>
        <dependencySet>
            <excludes><exclude>main.group:closed-webserver</exclude></excludes>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>

    <moduleSets>
        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>

            <includes>
                <include>main.group:closed-webserver</include>
            </includes>

            <binaries>
                <outputFileNameMapping>${module.artifactId}${dashClassifier?}.${module.extension}</outputFileNameMapping>
                <unpack>false</unpack>
                <includeDependencies>false</includeDependencies>
            </binaries>
        </moduleSet>

        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>

            <includes>
                <include>main.group:closed-spring</include>
            </includes>

            <binaries>
                <outputFileNameMapping>${module.artifactId}${dashClassifier?}.${module.extension}</outputFileNameMapping>
                <unpack>false</unpack>
                <includeDependencies>false</includeDependencies>
            </binaries>

        </moduleSet>

        <moduleSet>
            <useAllReactorProjects>true</useAllReactorProjects>

            <includes>
                <include>main.group:lgpl-ext-dist</include>
            </includes>

            <binaries>
                <outputDirectory>plugins</outputDirectory>
                <unpack>false</unpack>
                <includeDependencies>true</includeDependencies>
            </binaries>

        </moduleSet>
    </moduleSets>

</assembly>

回答by Krunal Shah

You can override default pom.xml. Maven have -f command option.

您可以覆盖默认的 pom.xml。Maven 有 -f 命令选项。

mvn -f <path>/pom.xml clean install

回答by khmarbaise

I would suggest to change the folder structure into the following.

我建议将文件夹结构更改为以下内容。

root (pom.xml)
  +-- closed-core
  +-- closed-web
  +-- closed-webserver
  +-- closed-spring
  +-- lgpl-reactor
         +-- lgpl-lib
         +-- lgpl-dist
         +-- lgpl-etc..

Than you don't need a separte module-pom.xml file. You can work with pom.xml as default.

比您不需要单独的 module-pom.xml 文件。您可以默认使用 pom.xml。

If you wan't to build lgpl-reactory you can simply give:

如果您不想构建 lgpl-reactory,您可以简单地给出:

mvn -pl lgpl-reactory clean package 

if you have dependencies within the other modules to lgpl you can use:

如果你在其他模块中有对 lgpl 的依赖,你可以使用:

mvn -am -pl lgpl-reactory clean package 

This will build also all dependent modules.

这也将构建所有依赖模块。

回答by Toilal

A solution is to apply the inverse logic, and use "mvn -f standalone-pom.xml clean package" to build lgpl-reactor as a standalone project ... But it'll be better if standalone project is build with a standard maven2 command line.

一个解决方案是应用逆逻辑,并使用“mvn -f standalone-pom.xml clean package”来构建lgpl-reactor作为一个独立的项目......但如果独立项目使用标准的maven2构建会更好命令行。