如何在 Maven 和 Eclipse 中使用不同的依赖版本两次构建相同的源代码?

rse*_*den 5 java eclipse m2eclipse maven

我想构建两个不同版本的自定义 SonarQube (SQ) 插件;一个用于 SonarQube 6.7.x LTS 版本,一个用于最新的 7.6 版本。目前,我的 Maven 结构类似于以下内容:

  • 家长
    • common:两个工具和两个插件版本使用的通用代码,不使用 SQ API
    • tool:一些相关的命令行实用程序,不使用SQ API
    • plugin-6.7:SQ 6.7 的插件代码
    • plugin-7.6:SQ 7.6 的插件代码
    • dist:构建包含 CLI 实用程序、插件版本、依赖许可证和源代码的发行版 zip

插件 6.7 和 7.6 的大部分源代码和资源是相同的;只有少数类别不同。因此,我想将此通用代码移动到某个共享源文件夹/模块。

不过,我仍然想编译这个通用代码两次,以验证代码是否针对两个 SQ API 版本进行编译。在 Eclipse 中,这应该显示为两个单独的源文件夹或项目,因此我可以轻松验证公共代码不使用 6.7 API 中尚未提供的任何 SQ API,并且不使用任何已删除的 API或在 7.6 API 中已弃用。

最好我想坚持在一个构建中构建两个插件版本,所以如果可能的话我想避免使用两个单独的配置文件。

这有可能吗?

我尝试过的

通过 Maven,我找到了几种让它工作的方法,但我无法让这些方法中的任何一种与 m2eclipse 一起工作。

方法一

创建一个新模块plugin-common,包含pom-6.7.xml和pom-7.6.xml。除了工件 ID 或分类器以及对不同 SQ API 版本的依赖之外,两个 pom 本质上是相同的。父项目将它们定义为 2 个独立的模块,使用

<module>plugin-common/pom-6.7.xml</module>
<module>plugin-common/pom-7.6.xml</module>
Run Code Online (Sandbox Code Playgroud)

这种方法的问题是我无法将这些模块导入 Eclipse,因为 m2eclipse 仅支持 pom.xml 作为文件名。

方法2

与上面类似,但为 pom.xml 文件使用单独的子目录,并使用<sourceDirectory>${project.basedir}/../src/main/java</sourceDirectory>指向公共源代码:

  • 通用插件
    • src/主/java
    • src/主/资源
    • pom.xml:6.7 和 7.6 模块的父级
    • 6.7/pom.xml
    • 7.6/pom.xml

这种方法允许将两个插件通用版本导入 Eclipse,但 Eclipse 抱怨“访问项目基目录之外的 .../src/main/java 目录”。因此,它不会显示两个插件公共项目中的任何源代码。

方法3

在plugin-common中没有任何pom.xml文件,而是使用build-helper-maven-plugin将公共代码作为源文件夹添加到plugin-6.7和plugin-7.6模块中,使用<source>${project.basedir}/../plugin-common/src/main/java</source>.

由于“访问项目基目录之外的 .../src/main/java 目录”警告,这在 Eclipse 中再次失败。

rse*_*den 1

我现在选择了以下方法,效果很好。

Maven结构:

  • 家长
    • common:两个工具和两个插件版本使用的通用代码,不使用 SQ API
    • tool:一些相关的命令行实用程序,不使用SQ API
    • 插件:SonarQube插件代码
    • dist:构建包含 CLI 实用程序、插件版本、依赖许可证和源代码的发行版 zip

插件模块包含 3 个基础包:

  • myproject.plugin.common:SQ 6.7 和 7.6 实现之间共享的代码
  • myproject.plugin.sq67:SonarQube 6.7 - 7.5.x 的特定代码
  • myproject.plugin.sq76:SonarQube 7.6+ 的特定代码

plugin/pom.xml 使用 SonarQube API 依赖项和打包插件定义,类似于以下内容:

        <dependency>
            <groupId>org.sonarsource.sonarqube</groupId>
            <artifactId>sonar-plugin-api</artifactId>
            <version>${sonarqube.version}</version>
            <scope>provided</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)
        <plugin>
            <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
            <artifactId>sonar-packaging-maven-plugin</artifactId>
            <version>1.16</version>
            <extensions>true</extensions>
            <configuration>
                <pluginKey>mykey</pluginKey>
                <pluginClass>myproject.plugin.common.MyPlugin</pluginClass>
                <pluginName>PluginName</pluginName>
                <pluginDescription>Description</pluginDescription>
                <useChildFirstClassLoader>true</useChildFirstClassLoader>
                <sonarQubeMinVersion>6.7</sonarQubeMinVersion>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

这允许插件与 SonarQube 6.7+ 一起使用,即使 API 依赖版本设置为 7.6(请参阅下面的配置文件)。MyPlugin 类是一个通用类,它使用 Java 反射来加载特定于 6.7 或特定于 7.6 的实现,具体取决于插件运行的 SonarQube 版本。

最后,父pom.xml定义了以下两个配置文件:

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <sonarqube.version>7.6</sonarqube.version>
            </properties>
            <modules>
                <module>common</module>
                <module>plugin</module>
                <module>tool</module>
                <module>dist</module>
            </modules>
        </profile>
        <profile>
            <id>checkSQ6.7Compatibility</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <sonarqube.version>6.7</sonarqube.version>
            </properties>
            <modules>
                <module>common</module>
                <module>plugin</module>
            </modules>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/sq76/**</exclude>
                            </excludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
Run Code Online (Sandbox Code Playgroud)

默认配置文件现在构建一个与 SonarQube 6.7 及更高版本兼容的插件,如果在该版本的 SonarQube 上运行,则启用 7.6 特定的功能。

checkSQ6.7Compatibility 配置文件将 SonarQube API 版本覆盖为 6.7,排除任何 SonarQube 7.6 特定的文件夹,并且仅构建通用模块和插件模块(跳过工具和 dist)。这样可以验证通用包和 6.7 特定包是否可以使用 6.7 API 编译正常。

在 Eclipse 中,我现在使用默认配置文件进行常规开发,使我能够同时处理 6.7 和 7.6 特定代码。进行任何重大更改后,我只需在 Eclipse 项目配置中选择 checkSQ6.7Compatibility 配置文件,以验证我没有意外引入对任何 7.6 特定 API 的依赖项。