【问题标题】:Added Gson to the pom.xml but is not found将 Gson 添加到 pom.xml 但未找到
【发布时间】:2014-08-09 20:27:22
【问题描述】:

我已将 gson 添加到我的 pom.xml 中。就这个。但是当我调用Gson gson = new Gson() 并尝试在 Maven 存储库中进行搜索时,它没有找到任何元素。为什么?我哪里错了?

<?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>
    <parent>
    <artifactId>VolaConNoi_webapp</artifactId>
    <groupId>it.volaconnoi</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

    <groupId>it.volaconnoi</groupId>
    <artifactId>VolaConNoi_webapp-ear</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ear</packaging>

    <name>VolaConNoi_webapp-ear</name>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>it.volaconnoi</groupId>
            <artifactId>VolaConNoi_webapp-ejb</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>ejb</type>
        </dependency>
        <dependency>
            <groupId>it.volaconnoi</groupId>
            <artifactId>VolaConNoi_webapp-web</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
        <!--  Gson: Java to Json conversion -->
        <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.2.4</version>
          <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

编辑

【问题讨论】:

  • 您是否导入了 Gson 库?您使用的是哪个 ide?​​span>
  • 你的意思是在java文件中我必须写import com.google etc...?
  • “未找到”是什么意思?你收到一个 Maven 错误吗?在这种情况下,向我们展示这个错误。
  • 要在 maven 存储库中找到它,您应该在向 pom 文件添加新库时第一次运行 maven clean install
  • 我正在研究 netbeans。在我的 java 文件中,我写了 Gson gson = new Gson()。然后我在“搜索 Maven 存储库中的依赖项...”中单击右侧,但是当对话框窗口打开时,它显示“没有匹配的结果”

标签: java xml maven gson


【解决方案1】:

请将此添加到 project 下的 pom.xml 文件中,这将告诉 maven 从此处下载您包含在 pom 文件中的库:

<repositories>
    <repository>
      <id>central</id>
      <name>Maven repository</name>
      <url>http://repo1.maven.org/maven2</url>
    </repository>
  </repositories>

您的代码将是这样的:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

Gson gson = new GsonBuilder().create();

还有consult this guide on how to use Gson,以防您在使用它时也需要帮助。

【讨论】:

  • 我和 OP 有同样的问题,但在 Eclipse 中。这可行,但我无法获得任何自动完成功能。这可能与 Gson 本身有关吗?
  • 为此苦苦挣扎了几个小时,直到我找到了这篇文章。我知道它已经有几年的历史了,但它仍然适用,至少适用于某些 Eclipse 发行版。
猜你喜欢
  • 1970-01-01
  • 2016-10-28
  • 2013-07-12
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
相关资源
最近更新 更多