You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

同一Maven项目中管理多版本NiFi依赖的最优方案问询

同一Maven项目适配NiFi 1.16.0与2.0.0-M3构建方案

核心思路

通过Maven Profile定义两个版本的专属配置(父POM坐标、JDK编译参数、依赖/插件版本),激活对应Profile时自动切换所有配置,无需手动修改父POM或依赖版本。


步骤1:根POM配置Profile

在项目根POM中添加两个Profile,分别对应NiFi 1.16.0和2.0.0-M3的全部配置:

<project ...>
    <!-- 用动态属性定义父POM,由Profile赋值 -->
    <parent>
        <groupId>${nifi.parent.groupId}</groupId>
        <artifactId>${nifi.parent.artifactId}</artifactId>
        <version>${nifi.version}</version>
        <relativePath>../pom.xml</relativePath> <!-- 根据你的项目结构调整路径 -->
    </parent>

    <properties>
        <!-- 默认属性(可被Profile覆盖) -->
        <nifi.version>1.16.0</nifi.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <profiles>
        <!-- NiFi 1.16.0 Profile -->
        <profile>
            <id>nifi-1.16.0</id>
            <activation>
                <activeByDefault>true</activeByDefault> <!-- 默认激活旧版本 -->
            </activation>
            <properties>
                <nifi.version>1.16.0</nifi.version>
                <nifi.parent.groupId>org.apache.nifi</nifi.parent.groupId>
                <nifi.parent.artifactId>nifi-nar-bundles</nifi.parent.artifactId>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.release>1.8</maven.compiler.release>
                <nar.plugin.version>1.5.4</nar.plugin.version> <!-- 适配NiFi 1.16.0的NAR插件版本 -->
            </properties>
        </profile>

        <!-- NiFi 2.0.0-M3 Profile -->
        <profile>
            <id>nifi-2.0.0-M3</id>
            <properties>
                <nifi.version>2.0.0-M3</nifi.version>
                <nifi.parent.groupId>org.apache.nifi</nifi.parent.groupId>
                <nifi.parent.artifactId>nifi-extension-bundles</nifi.parent.artifactId>
                <maven.compiler.source>21</maven.compiler.source>
                <maven.compiler.target>21</maven.compiler.target>
                <maven.compiler.release>21</maven.compiler.release>
                <nar.plugin.version>1.6.0</nar.plugin.version> <!-- 适配NiFi 2.0.0-M3的NAR插件版本 -->
            </properties>
        </profile>
    </profiles>

    <!-- 统一管理插件版本,由Profile中的属性控制 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.nifi</groupId>
                    <artifactId>nar-maven-plugin</artifactId>
                    <version>${nar.plugin.version}</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

步骤2:子模块依赖统一用动态版本

所有NiFi相关依赖的版本都使用${nifi.version},激活Profile时自动切换:

<!-- 子模块POM示例 -->
<dependency>
    <groupId>org.apache.nifi</groupId>
    <artifactId>nifi-api</artifactId>
    <version>${nifi.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.nifi</groupId>
    <artifactId>nifi-controller-service-api</artifactId>
    <version>${nifi.version}</version>
</dependency>

步骤3:构建命令

  • 构建NiFi 1.16.0版本(默认激活):
mvn clean install
  • 构建NiFi 2.0.0-M3版本:
mvn clean install -Pnifi-2.0.0-M3

额外适配:代码版本差异处理

如果两个NiFi版本的API存在不兼容,需要差异化代码,可以通过Profile指定不同源码目录:

在对应Profile中添加以下插件配置,指定专属源码目录(比如src/main/java-nifi2存放NiFi 2.x适配代码):

<profile>
    <id>nifi-2.0.0-M3</id>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>add-nifi2-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/java-nifi2</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

Eclipse适配

确保Eclipse已配置JDK 1.8和JDK 21,激活对应Profile后,Maven Integration插件会自动切换编译环境。若未自动切换,可手动在Project Properties > Java Build Path > Libraries中调整JRE系统库。

内容的提问来源于stack exchange,提问作者samsal77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 12:43:20