wechatpay-java 是微信官方 Java 语言客户端开发库。封装了支付的相关开发接口。本文档用于记录将项目由 Grade 转换为 Maven 的过程。
- 操作系统:macOS 13.7
- 项目地址:git@github.com:wechatpay-apiv3/wechatpay-java.git
- JDK版本:
            
            
              java
              
              
            
          
          yangzhao@192 ~ % java -version 
openjdk version "1.8.0_462"
OpenJDK Runtime Environment (Zulu 8.88.0.19-CA-macos-aarch64) (build 1.8.0_462-b08)
OpenJDK 64-Bit Server VM (Zulu 8.88.0.19-CA-macos-aarch64) (build 25.462-b08, mixed mode)安装 grade
            
            
              yaml
              
              
            
          
          // 安装gradle
brew install gradle
// 安装完成后,检查版本
gradle -v
Welcome to Gradle 9.0.0!
Here are the highlights of this release:
 - Configuration Cache is the recommended execution mode
 - Gradle requires JVM 17 or higher to run
 - Build scripts use Kotlin 2.2 and Groovy 4.0
 - Improved Kotlin DSL script compilation avoidance
For more details see https://docs.gradle.org/9.0.0/release-notes.html
------------------------------------------------------------
Gradle 9.0.0
------------------------------------------------------------
Build time:    2025-07-31 16:35:12 UTC
Revision:      328772c6bae126949610a8beb59cb227ee580241
Kotlin:        2.2.0
Groovy:        4.0.27
Ant:           Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM:  24.0.2 (Homebrew 24.0.2)
Daemon JVM:    /opt/homebrew/Cellar/openjdk/24.0.2/libexec/openjdk.jdk/Contents/Home (no JDK specified, using current Java home)
OS:            Mac OS X 13.7.7 aarch64- IDEA 配置

构建项目
            
            
              vbnet
              
              
            
          
          A problem occurred configuring root project 'wechatpay-java'.
> Could not resolve all artifacts for configuration ':classpath'.
   > Could not resolve com.diffplug.spotless:spotless-plugin-gradle:6.25.0.
     Required by:
         project : > com.diffplug.spotless:com.diffplug.spotless.gradle.plugin:6.25.0
      > No matching variant of com.diffplug.spotless:spotless-plugin-gradle:6.25.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but:
          - Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'javadocElements' declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'runtimeElements' declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'sourcesElements' declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
   > Could not resolve org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:5.0.0.4638.
     Required by:
         project : > org.sonarqube:org.sonarqube.gradle.plugin:5.0.0.4638
      > No matching variant of org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:5.0.0.4638 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.7' but:
          - Variant 'apiElements' declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'javadocElements' declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'runtimeElements' declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
          - Variant 'sourcesElements' declares a component for use during runtime, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its elements (required them packaged as a jar)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about org.gradle.plugin.api-version (required '8.7')
* Try:
> Review the variant matching algorithm at https://docs.gradle.org/8.7/userguide/variant_attributes.html#sec:abm_algorithm.
> No matching variant errors are explained in more detail at https://docs.gradle.org/8.7/userguide/variant_model.html#sub:variant-no-match.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.- Gradle 版本是 8.7 (从报错里的 org.gradle.plugin.api-version=8.7可以看出)。
- 插件版本:
- spotless-plugin-gradle:6.25.0需要 JDK11+
- sonarqube-gradle-plugin:5.0.0.4638需要 JDK11+
- 你现在项目/环境在用 JDK 8 (报错里多次提到 compatible with Java 8)。
由于我的 JDK 版本是 8,所以最好的选择是降级。
            
            
              bash
              
              
            
          
          plugins {
//    id 'com.diffplug.spotless' apply false
    id "com.diffplug.spotless" version "5.17.1" // 支持 JDK8 的版本
    id 'com.github.johnrengelman.shadow' apply false
//    id 'org.sonarqube'
    id "org.sonarqube" version "3.5.0.2730"     // 支持 JDK8 的版本
    id 'java'
    id 'maven-publish'
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}
            
            
              arduino
              
              
            
          
          // 重新构建或刷新grade,此时项目重新被识别
./gradlew build
Gradle2Maven
- 构建项目环境
            
            
              css
              
              
            
          
          yangzhao@192 wechatpay-java % ./gradlew buildEnvironment
> Task :buildEnvironment
------------------------------------------------------------
Root project 'wechatpay-java'
------------------------------------------------------------
classpath
+--- com.diffplug.spotless:com.diffplug.spotless.gradle.plugin:5.17.1
|    --- com.diffplug.spotless:spotless-plugin-gradle:5.17.1
|         +--- com.diffplug.spotless:spotless-lib:2.19.2
|         +--- com.diffplug.spotless:spotless-lib-extra:2.19.2
|         |    +--- com.diffplug.durian:durian-core:1.2.0
|         |    +--- com.diffplug.durian:durian-collect:1.2.0
|         |    |    --- com.diffplug.durian:durian-core:1.2.0
|         |    +--- org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r
|         |    |    +--- com.googlecode.javaewah:JavaEWAH:1.1.7
|         |    |    --- org.slf4j:slf4j-api:1.7.30
|         |    +--- com.googlecode.concurrent-trees:concurrent-trees:2.6.1
|         |    +--- org.codehaus.groovy:groovy-xml:3.0.9
|         |    |    --- org.codehaus.groovy:groovy:3.0.9
|         |    --- com.diffplug.spotless:spotless-lib:2.19.2
|         +--- com.diffplug.durian:durian-core:1.2.0
|         +--- com.diffplug.durian:durian-io:1.2.0
|         |    +--- com.diffplug.durian:durian-core:1.2.0
|         |    --- com.diffplug.durian:durian-collect:1.2.0 (*)
|         +--- com.diffplug.durian:durian-collect:1.2.0 (*)
|         --- org.eclipse.jgit:org.eclipse.jgit:5.10.0.202012080955-r (*)
+--- com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:7.1.2
|    --- gradle.plugin.com.github.johnrengelman:shadow:7.1.2
|         +--- org.jdom:jdom2:2.0.6
|         +--- org.ow2.asm:asm:9.2
|         +--- org.ow2.asm:asm-commons:9.2
|         |    +--- org.ow2.asm:asm:9.2
|         |    +--- org.ow2.asm:asm-tree:9.2
|         |    |    --- org.ow2.asm:asm:9.2
|         |    --- org.ow2.asm:asm-analysis:9.2
|         |         --- org.ow2.asm:asm-tree:9.2 (*)
|         +--- commons-io:commons-io:2.11.0
|         +--- org.apache.ant:ant:1.10.11
|         |    --- org.apache.ant:ant-launcher:1.10.11
|         +--- org.codehaus.plexus:plexus-utils:3.4.1
|         +--- org.apache.logging.log4j:log4j-core:2.17.1
|         |    --- org.apache.logging.log4j:log4j-api:2.17.1
|         --- org.vafer:jdependency:2.7.0
+--- org.sonarqube:org.sonarqube.gradle.plugin:3.5.0.2730
|    --- org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.5.0.2730
|         --- org.sonarsource.scanner.api:sonar-scanner-api:2.16.2.588
--- org.apache.logging.log4j:log4j-core:{require 2.17.1; reject [2.0, 2.17.1)} -> 2.17.1 (c)(c) - A dependency constraint, not a dependency. The dependency affected by the constraint occurs elsewhere in the tree.(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.                                                                                                                                                                                               A web-based, searchable dependency report is available by adding the --scan option.Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.BUILD SUCCESSFUL in 538ms8 actionable tasks: 1 executed, 7 up-to-date- 配置 maven-publish插件
maven-publish 是 Gradle 官方提供的插件,用来生成和发布 Maven 兼容的产物(jar、pom.xml 等)。
最常见用途就是:
- 在本地生成一个 pom.xml
- 或者把构建好的库发布到 ~/.m2/repository(本地 Maven 仓库)
            
            
              javascript
              
              
            
          
          plugins {
    id 'java'
    id 'maven-publish'
}
publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}- 生成 POM 文件
            
            
              vbnet
              
              
            
          
          wechatpay-java % ./gradlew generatePomFileForMavenJavaPublication
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD SUCCESSFUL in 526ms
8 actionable tasks: 1 executed, 7 up-to-date将 build/publications/mavenJava/pom-default.xml 复制到项目根目录下,重命名为 pom.xml。

