1

在我们的多模块 maven (3) 项目中,我们使用了 maven checkstyle 插件。看起来是这样,因为我们已经将 guava 依赖项转移到了父 pom,我们不能再成功执行 checkstyle:checkstyle 目标,因为它失败并出现以下异常:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-
plugin:2.10:checkstyle (default-cli) on project init: Execution default-cli of goal 
org.apache.maven.plugins:maven-checkstyle-plugin:2.10:checkstyle failed: An API 
incompatibility was encountered while executing org.apache.maven.plugins:maven-
checkstyle-plugin:2.10:checkstyle: java.lang.NoSuchMethodError: 
com.google.common.collect.ImmutableSortedSet.of([Ljava/lang/Comparable;)Lcom/google
/common/collect/ImmutableSortedSet;
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.apache.maven.plugins:maven-checkstyle-plugin:2.10

原因可能是 maven checkstyle 插件依赖于 checkstyle 框架,该框架依赖于 google-collections 框架(现在包含在 google guava 框架中),即 checkstyle 调用了一个不属于 google 集合的方法番石榴了。

这是正在使用的父 pom 的摘录:

<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ourcompany.ourproject</groupId>
<artifactId>ourproject</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>our project</name>

<modules>
    <module>init</module>

...

</modules>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>4.11</junit.version>
    <maven-compiler-plugin.version>3.0</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.14</maven-surefire-plugin.version>
    <maven-checkstyle-plugin.version>2.10</maven-checkstyle-plugin.version>
    <maven-pmd-plugin.version>3.0.1</maven-pmd-plugin.version>
    <maven-resources-plugin.version>2.6</maven-resources-plugin.version>
    <java.source.version>1.6</java.source.version>
    <java.target.version>1.6</java.target.version>
    <google.guava.version>14.0.1</google.guava.version>
</properties>

<repositories>
    <repository>
        <id>nexus</id>
        <name>Internal Maven Repository</name>
        <url>http://ourinternalmavenrepository/nexus/content/groups/public</url>
    </repository>

...

</repositories>

<build>

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.source.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.source.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>${maven-resources-plugin.version}</version>
                <configuration>
                    <source>${java.source.version}</source>
                    <target>${java.source.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

...

<distributionManagement>
    <repository>
        <id>deployment</id>
        <name>Internal Releases</name>
        <url>http://ourinternalmavenrepository/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>deployment</id>
        <name>Internal Releases</name>
        <url>http://ourinternalmavenrepository/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>
<profiles>

...

    <profile>
        <id>metrics</id>
        <repositories>
            <repository>
                <id>central</id>
                <url>http://central</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <url>http://central</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
        <build>
            <plugins>
                <!-- CHECKSTYLE -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>${maven-checkstyle-plugin.version}</version>
                    <configuration>
                        <configLocation>our_checkstyle.xml</configLocation>
                        <failsOnError>false</failsOnError>
                        <consoleOutput>true</consoleOutput>
                        <source>${java.source.version}</source>
                        <target>${java.source.version}</target>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>com.ourcompany.ourproject</groupId>
                            <artifactId>init</artifactId>
                            <version>0.1-SNAPSHOT</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${maven-pmd-plugin.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>check</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <failsOnError>false</failsOnError>
                        <source>${java.source.version}</source>
                        <target>${java.source.version}</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        <reporting>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-checkstyle-plugin</artifactId>
                    <version>${maven-checkstyle-plugin.version}</version>
                    <configuration>
                        <configLocation>our_checkstyle.xml</configLocation>
                        <targetJdk>${java.source.version}</targetJdk>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${maven-pmd-plugin.version}</version>
                    <configuration>
                        <targetJdk>${java.source.version}</targetJdk>
                    </configuration>
                </plugin>
            </plugins>
        </reporting>
    </profile>
</profiles>

<!-- <dependencyManagement> -->
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

...

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${google.guava.version}</version>
    </dependency>

...

</dependencies>
<!-- </dependencyManagement> -->

我认为在 maven 项目中使用 maven checkstyle 插件以及使用 guava 框架非常常见。所以我真的想知道我们在这里做错了什么;)

4

1 回答 1

2

非常感谢@AndrewLogvinov。他在对这个问题的评论中提供了可行的解决方案。只需将 google-collections 依赖项添加到 maven-checkstyle-plugin 的依赖项中:

<dependency>
    <groupId>com.google.collections</groupId>
    <artifactId>google-collections</artifactId>
    <version>1.0</version>
</dependency>
于 2013-04-08T09:19:57.793 回答