多模块Maven项目中Driver binary downloader插件配置复用问题
刚好我之前处理过类似的Maven多模块资源共享场景,既然你已经用pluginManagement搞定了插件配置的复用,那针对RepositoryMap.xml的问题,给你两个可行的方案,优先推荐第一种,更符合Maven的依赖管理规范:
方案1:通过测试JAR+Classpath引用实现复用
这个方法的核心是把RepositoryMap.xml打包到core模块的测试JAR中,子模块依赖这个测试JAR后,直接通过classpath路径引用文件,不用每个子模块都复制一份。
步骤1:调整core模块的资源结构
把RepositoryMap.xml移动到core模块的src/test/resources目录下(因为这是测试场景用到的资源)。
步骤2:配置core模块打包测试JAR
在core模块的pom.xml中添加maven-jar-plugin的配置,让Maven打包包含测试资源的测试JAR:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
步骤3:在core的pluginManagement中指定文件的classpath路径
修改core模块pluginManagement里的driver-binary-downloader-maven-plugin配置,把repositoryMapLocation指向classpath中的文件:
<pluginManagement> <plugins> <plugin> <groupId>com.lazerycode.selenium</groupId> <artifactId>driver-binary-downloader-maven-plugin</artifactId> <version>你的插件版本号</version> <configuration> <!-- 保留你原有的其他配置 --> <repositoryMapLocation>classpath:RepositoryMap.xml</repositoryMapLocation> </configuration> </plugin> </plugins> </pluginManagement>
步骤4:子模块依赖core的测试JAR
在每个子模块的pom.xml中添加对core测试JAR的依赖:
<dependencies> <dependency> <groupId>你的core模块groupId</groupId> <artifactId>core</artifactId> <version>项目版本号</version> <scope>test</scope> <type>test-jar</type> </dependency> </dependencies>
这样子模块在运行测试时,classpath中会自动包含core的测试资源,driver-binary-downloader插件就能通过classpath:RepositoryMap.xml找到文件,不用再在子模块中放置这个文件了。
方案2:通过资源插件复制文件到子模块
如果不想用测试JAR,也可以用maven-resources-plugin在子模块构建时自动从core模块复制RepositoryMap.xml:
在每个子模块的pom.xml中添加以下配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-repository-map</id> <phase>process-test-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.testOutputDirectory}</outputDirectory> <resources> <resource> <!-- 这里的路径要根据你的模块结构调整,确保能找到core模块的资源目录 --> <directory>${project.basedir}/../core/src/test/resources</directory> <includes> <include>RepositoryMap.xml</include> </includes> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>
这个方法的缺点是依赖固定的模块目录结构,如果后续模块结构调整,路径可能需要修改,不如方案1灵活。
内容的提问来源于stack exchange,提问作者StopTheRain

