当所有模块的版本都由单个 属性 驱动时,在不构建父模块的情况下构建子模块

Build child module without building parent module when version of all modules are driven by a single property

我有一个具有以下层次结构的多模块项目:

parent-build 项目 pom.xml

...
    <groupId>com.my.project</groupId>
    <artifactId>parent-build</artifactId>
    <packaging>pom</packaging>
    <name>parent-build</name>
    <version>${version.product}</version>
    <properties>
        <version.product>1.0</version.product>
    </properties>
    ...

build-all 项目 pom.xml

...
    <parent>
        <artifactId>parent-build</artifactId>
        <groupId>com.my.project</groupId>
        <version>${version.product}</version>
        <relativePath>../parent-build/pom.xml</relativePath>
    </parent>
    <groupId>com.my.project</groupId>
    <packaging>pom</packaging>
    <modules>
        <module>../child-1</module>
        <module>../child-2</module>
    </modules>
    ...

child-2 项目 pom.xml

...
    <parent>
        <artifactId>parent-build</artifactId>
        <groupId>com.my.project</groupId>
        <version>${version.product}</version>
        <relativePath>../parent-build/pom.xml</relativePath>
    </parent>
    <groupId>com.my.project</groupId>
    <artifactId>child-2</artifactId>
    <packaging>jar</packaging>
    <version>${version.product}</version>
    ...
    <dependencies>
        <dependency>
            <groupId>com.my.project</groupId>
            <artifactId>child-1</artifactId>
            <version>${version.product}</version>
        </dependency>
        ...
    </dependencies>
    ...

child-1 项目 pom.xml

...
    <parent>
        <artifactId>parent-build</artifactId>
        <groupId>com.my.project</groupId>
        <version>${version.product}</version>
        <relativePath>../parent-build/pom.xml</relativePath>
    </parent>
    <groupId>com.my.project</groupId>
    <artifactId>child-1</artifactId>
    <packaging>jar</packaging>
    <version>${version.product}</version>
    ...

我想用相同的版本构建所有的 jar,这个版本应该在一个地方指定。 我在父构建 pom.xml

中声明了一个 属性

现在,当我对 build-all 项目执行 mvn clean install 时,它会按照指定的模块顺序构建所有项目。 没关系。

但在某些情况下,我只想构建 child-2 项目。

也就是说,假设我在 child-2 项目中做了一些更改,并且只想构建这个项目。 这种情况的问题是,它找不到 属性 值 version.product

编辑:

下面是我在构建 child-2 项目时遇到的错误

Could not transfer artifact com.my.project:parent-build:pom:${version.product} from/to central (http://repo1.maven.org/maven2): Illegal character in path at index 66: http://repo1.maven.org/maven2/com/my/project/parent-build/${version.product}/parent-build-${version.product}.pom

请帮忙。

提前致谢。

首先,我强烈反对这样做,但我知道您可能有一个模块尚未正常工作等。过去我使用父 pom 中的配置文件来控制模块列表:

<profiles>
    <profile>
        <id>all</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <modules>
            <module>1</module>
            <module>2</module>
            <module>3</module>
            <module>4</module>
        </modules>
    </profile>
    <profile>
        <id>profile1</id>
        <modules>
            <module>1</module>
            <module>2</module>
            <module>3</module>
        </modules>
    </profile>
</profiles>

然后:

1) mvn clean install - 您将获得所有模块

2) mvn clean install -Pprofile1 你只会得到模块 1,2 & 3.

flatten-maven-plugin

这就是我想要的。

此插件将替换您在 属性 中指定的版本,在所有有效 pom.xml 中使用实际 属性 值。

所以稍后如果您单独构建任何子项,它将构建成功。

干杯