简体   繁体   English

如何在Eclipse中解析导入?

[英]How to resolve import in Eclipse?

Im new in Java development and not familiar with various kinds of import (Maven, Git, etc), so I make it simple: 我是Java开发的新手,不熟悉各种导入(Maven,Git等),所以我简单一点:

import com.google.common.collect.*;
import com.google.gson.*;

These two is not resolved in code Im inspecting, and I have no idea what kind of actions I should take neither what I should import to resolve it but it is probably some popular library. 在我正在检查的代码中没有解决这两个问题,我不知道我应该采取什么样的行动既不应该导入以解决它,但它可能是一些流行的库。

Is there complete guide how developers import packages in eclipse (for example C# developers use Nuget, despite there is a ton of hand made ones), or they really use all this enormous import selector? 是否有完整的指南如何开发人员在eclipse中导入包(例如C#开发人员使用Nuget,尽管有大量的手工制作),或者他们真的使用了所有这些巨大的导入选择器?

First of all Mavenise your current project and add the following dependency to it: 首先Mavenise你当前的项目并添加以下依赖项:

Goto: POM.XML after converting your current project to Maven project. 转到:将当前项目转换为Maven项目后的POM.XML。

    <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.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>

      <properties>
       ------Properties Here----
      </properties>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>

        </dependency> 

          <dependency>
        <groupId>com.google.collections</groupId>
        <artifactId>google-collections</artifactId>
        <version>1.0-rc2</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
--------Add your Dependencies here, remember you have to add dependencies under <dependencies> here </dependencies> ----------
      </dependencies>
    </project>

Search for all dependencies here: https://mvnrepository.com/ , incase you need more dependencies to import. 在此处搜索所有依赖项: https ://mvnrepository.com/,如果您需要更多依赖项导入。

How to mavenise your current Java Project: 如何对当前的Java项目进行修改:

In your eclipse just right click on Java Project and click Configure and you should see “ Convert to Maven Project ” option. 在你的eclipse中右键单击Java Project并单击Configure,你会看到“转换为Maven项目”选项。

What is POM.XML 什么是POM.XML

A Project Object Model or POM is the fundamental unit of work in Maven. 项目对象模型或POM是Maven的基本工作单元。 It is an XML file that contains information about the project and configuration details used by Maven to build the project. 它是一个XML文件,其中包含有关Maven用于构建项目的项目和配置详细信息的信息。 It contains default values for most projects. 它包含大多数项目的默认值。 Now, you can add a dependency in pom.xml. 现在,您可以在pom.xml中添加依赖项。

If you use no dependency management tool like maven the simplest way is just download corresponding JARs and add them manually: http://www.oxfordmathcenter.com/drupal7/node/44 如果您不使用像maven这样的依赖管理工具,最简单的方法就是下载相应的JAR并手动添加它们: http//www.oxfordmathcenter.com/drupal7/node/44

For GSON use this: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1 对于GSON,请使用: https ://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

For the second dependency I suppose you should use GUAVA: https://github.com/google/guava/wiki/UseGuavaInYourBuild 对于第二个依赖项,我认为你应该使用GUAVA: https//github.com/google/guava/wiki/UseGuavaInYourBuild

or this: https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2 或者: https//mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2

But actually better to convert your project to Maven project (as described for example here: https://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/ ) so that you'll be able to use pom.xml for dependency management or Gradle (see configuring existing eclipse java project to build using gradle ) and avoid manual JAR download 但实际上更好的方法是将您的项目转换为Maven项目(例如,如下所述: https//crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/ )以便您' ll能够使用pom.xml进行依赖关系管理或Gradle(请参阅配置使用gradle构建的现有eclipse java项目 )并避免手动JAR下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM