maven-dependency-plugin demo

本文介绍如何使用Maven依赖插件将项目的jar依赖复制到target/lib目录中,通过配置插件的目标、阶段及输出路径等参数实现自动化打包。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将maven 项目的jar copy 到项目的target/lib目录下

 XML Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.1</version>
  <executions>
    <execution>
      <id>copy-dependencies</id>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
        <type>jar</type>
        <includeTypes>jar</includeTypes>
        <outputDirectory>${project.build.directory}/lib</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

<think>我们正在解决用户在使用OpenRewriteMaven插件时遇到的`NoSuchMethodError`错误。根据用户提供的信息,错误发生在执行`rewrite-maven-plugin:run`时,具体是`UpdateJavaCompatibility`构造函数的兼容性问题。这通常是由于插件版本与依赖的OpenRewrite核心库版本不匹配导致的。###问题分析`NoSuchMethodError`通常表示在运行时调用的方法在编译时存在,但在实际运行的类路径中找不到。这可能是由于:1.**版本不兼容**:`rewrite-maven-plugin`的版本与它所依赖的`rewrite-core`或`rewrite-spring`版本不匹配。2.**依赖冲突**:项目中可能存在多个不同版本的OpenRewrite库,导致类加载器加载了错误版本的类。 ###解决方案####1. **检查插件和依赖版本兼容性**根据OpenRewrite官方文档,确保`rewrite-maven-plugin`版本与`rewrite-spring`(或其他配方模块)版本兼容。以下是已知兼容版本:|插件版本|配方版本 ||----------|----------| |5.8.1|5.7.0|在`pom.xml`中配置示例: ```xml<build><plugins><plugin><groupId>org.openrewrite.maven</groupId><artifactId>rewrite-maven-plugin</artifactId><version>5.8.1</version><!--确保插件版本 --><configuration><activeRecipes><recipe>org.openrewrite.java.spring.NoAutowiredAnnotation</recipe></activeRecipes></configuration><dependencies><dependency><groupId>org.openrewrite.recipe</groupId><artifactId>rewrite-spring</artifactId><version>5.7.0</version><!--确保配方版本--></dependency></dependencies></plugin></plugins></build> ```####2.**解决依赖冲突** 如果版本配置正确但问题仍然存在,可能是依赖冲突。执行以下步骤: -**检查依赖树**:运行`mvndependency:tree`,查找是否有其他依赖引入了不同版本的OpenRewrite库(如`rewrite-core`)。 -**排除冲突依赖**:在`rewrite-maven-plugin`的依赖中排除冲突的传递依赖。例如:```xml<dependency><groupId>org.openrewrite.recipe</groupId><artifactId>rewrite-spring</artifactId><version>5.7.0</version><exclusions><exclusion><groupId>org.openrewrite</groupId><artifactId>rewrite-core</artifactId></exclusion></exclusions></dependency>```然后显式指定正确的`rewrite-core`版本:```xml<dependency><groupId>org.openrewrite</groupId><artifactId>rewrite-core</artifactId><version>8.12.0</version> <!--与rewrite-spring5.7.0兼容的核心版本 --></dependency>```####3. **清理Maven本地仓库**损坏的依赖可能导致`NoClassDefFoundError`或`NoSuchMethodError`[^2]: 1.删除本地Maven仓库中与OpenRewrite相关的目录(默认路径:`~/.m2/repository/org/openrewrite`)。2.重新运行`mvnrewrite:run`,让Maven重新下载依赖。####4.**验证Java版本兼容性**确保项目使用的Java版本与OpenRewrite插件兼容:- OpenRewrite5.x需要 Java8或更高版本。 -在`maven-compiler-plugin`中配置正确的Java版本(如11)[^1][^4]:```xml<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.10.1</version><configuration><release>11</release></configuration></plugin>```###错误示例分析用户遇到的错误可能是: ```logjava.lang.NoSuchMethodError:org.openrewrite.java.UpdateJavaCompatibility.<init>(Ljava/lang/Integer;Ljava/lang/Integer;)V``` 这表明插件试图调用一个两个参数的`UpdateJavaCompatibility`构造函数,但实际加载的类中没有该方法。这通常是因为: -项目中的`rewrite-core`版本过低(低于8.12.0),而该构造函数是在较新版本中引入的。 -解决方案:升级`rewrite-core`到与配方兼容的版本(如8.12.0)。 ###总结步骤1. **固定版本组合**:使用`rewrite-maven-plugin:5.8.1` +`rewrite-spring:5.7.0`。 2. **排除冲突**:通过`<exclusions>`解决传递依赖冲突。 3. **清理仓库**:删除本地OpenRewrite依赖后重试。4.**检查Java版本**:确保编译器插件配置正确。执行上述步骤后,`NoSuchMethodError`应得到解决。如果问题仍然存在,请提供完整的错误日志以便进一步分析。--- ###相关问题1. **如何调试OpenRewrite的配方执行过程?**在Maven命令中添加`-Drewrite.debug=true`参数,输出详细的转换日志。2.**OpenRewrite转换后代码格式混乱怎么办?**运行`mvn rewrite:fix-Drewrite.failOnDryRunResults=false`自动格式化代码,或集成Spotless插件。 3. **除了Maven,OpenRewrite支持Gradle吗?**是。OpenRewrite提供Gradle插件,配置方式类似(需添加`rewrite-gradle-plugin`)。4.**转换过程中如何排除特定文件?**在配方配置中添加`.exclude("**/*Test.java")`,或使用`@SuppressWarnings("rewrite")`注解。5.**OpenRewrite是否支持自定义配方开发?**支持。可通过Java或YAML定义自定义配方,需继承`org.openrewrite.Recipe`类。 [^1]: Maven Compiler Plugin配置Java版本[^2]:解决Maven依赖损坏的方法[^4]: SpringBoot Maven插件配置示例</think>### 解决 OpenRewrite Maven 插件 5.8.1 的 NoSuchMethodError 问题 当执行 `rewrite-maven-plugin:run` 时出现 `NoSuchMethodError`(特别是 `UpdateJavaCompatibility` 构造函数相关错误),这通常是由 **API 版本不兼容** 引起的。以下是系统化的解决方案: --- ### 原因分析 1. **版本冲突** `rewrite-maven-plugin` 与 `rewrite-core` 或 `rewrite-spring` 版本不匹配,导致插件调用不存在的方法[^2]。 2. **依赖传递问题** Maven 依赖树中存在多个冲突的 OpenRewrite 版本,导致加载了错误版本的类。 3. **缓存污染** 本地 Maven 仓库中存在损坏或部分下载的依赖项(常见于网络不稳定的环境)[^2]。 --- ### 解决方案 #### 第一步:强制统一版本号 在 `pom.xml` 中显式指定兼容版本组合: ```xml <properties> <!-- 使用兼容版本组合 --> <rewrite-maven-plugin.version>5.8.1</rewrite-maven-plugin.version> <rewrite-spring.version>5.7.0</rewrite-spring.version> <rewrite-core.version>8.12.0</rewrite-core.version> </properties> <build> <plugins> <plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>${rewrite-maven-plugin.version}</version> <dependencies> <!-- 强制指定核心库版本 --> <dependency> <groupId>org.openrewrite</groupId> <artifactId>rewrite-core</artifactId> <version>${rewrite-core.version}</version> </dependency> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>${rewrite-spring.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> ``` #### 第二步:清理 Maven 缓存 删除损坏的依赖(关键步骤): ```bash # 清理本地仓库中的 OpenRewrite 相关依赖 rm -rf ~/.m2/repository/org/openrewrite # 重新下载所有依赖 mvn clean dependency:purge-local-repository ``` #### 第三步:验证依赖树 检查版本冲突: ```bash mvn dependency:tree -Dincludes=org.openrewrite ``` 输出中应确保所有 `rewrite-core` 版本统一为 **8.12.0**。 #### 第四步:添加 Java 版本配置 确保编译器插件与 OpenRewrite 兼容[^1][^4]: ```xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <!-- 与 OpenRewrite 兼容的 Java 版本 --> <release>11</release> </configuration> </plugin> ``` --- ### 错误示例分析 典型错误日志: ``` java.lang.NoSuchMethodError: org.openrewrite.java.UpdateJavaCompatibility.<init>(Ljava/lang/Integer;Ljava/lang/Integer;)V ``` 这表示: 1. 插件预期调用带两个 `Integer` 参数的构造函数 2. 实际加载的类中不存在此方法 3. **根本原因**:`rewrite-core` 版本低于 8.1.0(该构造函数在 8.1.0 引入) --- ### 预防措施 1. **版本兼容矩阵** | 插件版本 | 最小 rewrite-core 版本 | |----------|------------------------| | 5.8.x | 8.12.0 | | 5.7.x | 8.1.0 | 2. **锁定依赖版本** 在 `dependencyManagement` 中全局锁定: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.openrewrite</groupId> <artifactId>rewrite-core</artifactId> <version>8.12.0</version> </dependency> </dependencies> </dependencyManagement> ``` --- ### 验证是否修复 执行命令并检查日志: ```bash mvn rewrite:run -X | grep "Loaded rewrite-core" ``` 正常输出应显示统一版本: ``` [DEBUG] Loaded rewrite-core version: 8.12.0 ``` --- ### 相关问题 1. **如何解决 OpenRewrite 执行后的编译错误?** 检查构造函数注入是否导致循环依赖,需重构代码[^4]。 2. **Maven 构建时出现 `Invalid POM` 错误如何解决?** 清理本地仓库并重试,确保依赖完整下载[^2]。 3. **OpenRewrite 是否支持 JDK 17?** 是,需配置 `maven-compiler-plugin` 的 `<release>17</release>`[^1]。 4. **转换后 Lombok 注解冲突怎么办?** 移除 `@Autowired` 后需手动删除冗余的 `@RequiredArgsConstructor`[^4]。 5. **如何在 IntelliJ 中调试 OpenRewrite 配方?** 添加 `-Drewrite.debug=true` 参数并查看 `rewrite.yml` 日志文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值