1

I am running into test failures with a maven project that I do not understand.

The maven project is set up as a multi-module project, and it shaded one of its dependencies (Google Guava, because Spark itself has a conflicting dependency for Google Guava).

Below is a simplified version of the parent 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/xsd/maven-4.0.0.xsd">
    ... other stuff...

    <modules>
        <module>module A</module>
        <module>module B</module>
        <module>module C</module>
        ...
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0-jre</version>
        </dependency>
        ...other dependencies

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <id>relocateGuava</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>com.google</pattern>
                                    <shadedPattern>shaded.com.google</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                    <execution>
                        <id>buildUberJar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                    <resource>reference.conf</resource>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
        </plugins>
    </build>
</project>

The failure includes this:

Caused by: java.lang.ClassNotFoundException: shaded.com.google.common.collect.Multimap
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 22 more

and the offending line is a simple import statement in module A: import com.google.common.collect.ListMultimap;.

The dependency structure is like this: - module B is the one whose tests are failing - module B names module A as a dependency - module A includes that import statement

Here is a simplified version of module B's 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/xsd/maven-4.0.0.xsd">
    <dependencies>
        <dependency>
            module A
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>none</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

I think what might be happening is that module B's tests are run before its dependencies are shaded. So basically, the module B code that's being tested doesn't include the shaded dependencies in the classpath. However, B depends on A, and A's code has been rewritten to point at the shaded dependencies rather than the original dependencies. So when B includes A's code, it's including the code that is pointing at the shaded dependencies, and those shaded dependencies are not available to B.

Questions: - Is that is what is happening? - How can I fix it?

(If you can't tell, I'm very inexperienced with both Java and Maven)

EDIT: Will also add that mvn package succeeds -- only mvn test fails.

Josh Reback
  • 529
  • 5
  • 16

1 Answers1

0

I think you may got wrong verion of google-guava, there are three ways you can try:

  1. Delete your local directory of google-guave which is keeping in your local maven repository and reimport the whole depedencies of your project, i used to use this way to resolve my problem years ago.

  2. Execute maven install for module A, so you can find the module A in your local maven repository, if maven install get error for module A, it must something wrong in module A, so we can exclude module B's problem.

  3. Explicitly define google-guava dependency in Module B, so when you run test case, maven will get google-guava dependency from module B, project's dependency of google-guava will be covered.

if three way above not work well for your probelm, i hope you can execute mvn dependency:list and which dependencies you actual get, when you run test case, especially attention on google-guava's verion, check its version is just right define in project's pom.xml

shengbang he
  • 128
  • 9