You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

遭遇NoSuchMethodError错误,求独立应用的Google Pub/Sub的POM配置

Hey there, that NoSuchMethodError tied to com.google.common.base.Preconditions.checkArgument is almost always a Guava version conflict—super common when working with Google Cloud libraries like Pub/Sub. Let me share a robust pom.xml setup that should fix this, plus some tips to keep similar issues at bay.

Here's a complete, conflict-resistant configuration tailored for standalone Pub/Sub applications:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yourcompany</groupId>
    <artifactId>pubsub-standalone-app</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- Google Cloud BOM to manage consistent library versions -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>libraries-bom</artifactId>
                <version>26.37.0</version> <!-- Use the latest stable version -->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Lock Guava version to match Pub/Sub's requirements -->
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>32.1.3-jre</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Google Pub/Sub client library -->
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-pubsub</artifactId>
            <!-- Version is inherited from the BOM -->
        </dependency>

        <!-- Optional: For logging (adjust based on your setup) -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.9</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.9</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Compiler plugin to target modern Java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>11</source> <!-- Adjust to your Java version -->
                    <target>11</target>
                </configuration>
            </plugin>

            <!-- Shade plugin for building a standalone fat JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <!-- Relocate Guava to avoid conflicts with external dependencies -->
                                <relocation>
                                    <pattern>com.google.common</pattern>
                                    <shadedPattern>com.yourcompany.shaded.google.common</shadedPattern>
                                </relocation>
                            </relocations>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.yourcompany.MainClass</mainClass> <!-- Replace with your main class -->
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
2. Why This Works

Let me break down the key fixes:

  • Google Cloud BOM: This imports a curated set of compatible versions for all Google Cloud libraries, so you don't have to manually align Pub/Sub with its dependencies.
  • Guava Version Lock: The <dependencyManagement> section forces all modules to use the same Guava version that's tested with the Pub/Sub library—eliminating the version mismatch that causes your NoSuchMethodError.
  • Shade Plugin with Relocation: If you're running a standalone JAR, relocating Guava to a custom package ensures it doesn't clash with older versions that might be pulled in by other libraries in your classpath.
3. Quick Troubleshooting Tips
  • Run mvn dependency:tree in your project root to identify which dependencies are pulling in conflicting Guava versions. You can exclude them from those dependencies if needed:
    <dependency>
        <groupId>problematic-group</groupId>
        <artifactId>problematic-artifact</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  • Double-check that you haven't manually added an older Guava dependency elsewhere in your pom.xml—let the BOM or dependencyManagement handle it instead.

内容的提问来源于stack exchange,提问作者Jeryl Cook

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:52:42