java.lang.NoClassDefFoundError:com / google / gson / Gson

时间:2012-05-25 20:00:25

标签: java eclipse spring maven junit

我注意到一个奇怪的问题。我可以使用Junit运行我的测试用例,但是当我使用maven运行时,其中一个测试用例失败了。它抱怨没有找到Gson class def。

我能够在Maven依赖项中看到Gson jar。

所以我怀疑classpath不包括Gson。所以我用-X运行了maven并注意到了一些线索。

[DEBUG] Could not find metadata com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml in local (C:\Users\ra\.m2\repository)
[DEBUG] Skipped remote update check for com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml, locally cached metadata up-to-date.
[DEBUG] Could not find metadata com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml in local (C:\Users\ra\.m2\repository)
[DEBUG] Skipped remote update check for com.example.libraries:Symbology:1.0.0-SNAPSHOT/maven-metadata.xml, locally cached metadata up-to-date.
[WARNING] The POM for com.example.libraries:Symbology:jar:1.0.0-SNAPSHOT is invalid, transitive dependencies (if any) will not be available: 2 problems were encountered while building the effective model for com.example.libraries:Symbology:1.0.0-SNAPSHOT
[ERROR] 'dependencies.dependency.artifactId' for ::jar is missing. @ 
[ERROR] 'dependencies.dependency.groupId' for ::jar is missing. @ 

我有一个依赖于Symbology项目的项目,而这又使用了Gson。 但是现在从这个日志中我看到传递依赖性没有包括在内。所以没有找到Gson课程。

这是Symbology pom:

<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.example.libraries</groupId>
<artifactId>Symbology</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Symbology</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
</dependencies>

这是我的项目的Pom,它叫做符号系统:

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.example.libraries</groupId>
<artifactId>FGF</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<distributionManagement>
    <snapshotRepository>
        <id>example.com</id>
        <name>example.com-snapshots</name>
        <url>http://example/artifactory/libs-snapshots-local</url>
    </snapshotRepository>
</distributionManagement>

<name>FGF</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Category</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Time</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Display</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    **<dependency>
        <groupId>com.example.libraries</groupId>
        <artifactId>Symbology</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>**
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.0.BUILD-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.directory.studio</groupId>
        <artifactId>org.apache.commons.lang</artifactId>
        <version>2.6</version>
    </dependency>       

</dependencies>

1 个答案:

答案 0 :(得分:1)

在花了很多时间之后最终解决问题。

问题是当我将符号系统工件推送到存储库时,它并没有将pom推入存储库。因此,当我使用符号系统工件作为依赖项时,我的项目无法知道什么是传递依赖项。

因此,我将我的pom与jar一起推入存储库,现在它可以获得所有依赖项。

HURRAYYYYYYYYYYYYYYYYY !!!!!!!!!!!!!!!!!!