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

问题描述 投票:4回答:1

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

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

plugin-6.7和7.6的大多数源代码和资源都相同;只有少数类不同。因此,我想将此公共代码移动到某个共享源文件夹/模块。

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

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

这有可能吗?

我试过了什么

使用Maven我已经找到了几种方法来实现这一点,但是我无法使用m2eclipse中的任何一种方法。

方法1

创建一个新的模块插件 - common,包含pom-6.7.xml和pom-7.6.xml。除了工件id或分类器之外,两个pom基本相同,并且依赖于不同的SQ API版本。父项目将这些定义为2个单独的模块使用

<module>plugin-common/pom-6.7.xml</module>
<module>plugin-common/pom-7.6.xml</module>

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

方法2

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

  • 插件常见 的src /主/ JAVA 的src / main /资源 pom.xml:6.7和7.6模块的父级 6.7 / pom.xml的 7.6 / pom.xml的

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

方法3

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

由于项目基础目录之外的'Access ... / src / main / java目录'警告,Eclipse中再次失败。

java eclipse maven m2eclipse
1个回答
1
投票

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

Maven结构:

  • 亲 common:工具和两个插件版本使用的公共代码不使用SQ API 工具:一些相关的命令行实用程序,不使用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>
        <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>

这允许插件与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>

默认配置文件现在构建一个与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引入依赖关系。

© www.soinside.com 2019 - 2024. All rights reserved.