如何使用 IntelliJ 添加外部库的源和 javadoc 到 gradle?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24765193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:44:46  来源:igfitidea点击:

How to add external library's sources and javadoc to gradle with IntelliJ?

javaintellij-ideagradlejavadoc

提问by kruze

I've set up a Java project with IntelliJ and Gradle. I have a build.gradle file in my root project and I can compile and run my app.

我已经用 IntelliJ 和 Gradle 建立了一个 Java 项目。我的根项目中有一个 build.gradle 文件,我可以编译和运行我的应用程序。

However... I'm using a Java library which comes with a sources and javadoc zip file. If I'm in my source code and want to go to the declaration of a class or method from this library, IntelliJ brings up the .classfile instead of the source .javafile provided in the zip.

但是...我正在使用一个带有源代码和 javadoc zip 文件的 Java 库。如果我在我的源代码中并想从这个库中转到类或方法的声明,IntelliJ 会调出该.class文件而不是.javazip 中提供的源文件。

How can I tell gradle to use the sources and javadoc zips provided with the external library?

如何告诉 gradle 使用外部库提供的源代码和 javadoc zip?

回答by Michael

It isn't Gradle you need to tell where the sources are. It is IntelliJ.

您不需要告诉 Gradle 来源在哪里。它是 IntelliJ。

File -> Project Structure -> Modules -> Select correct module if you have more than one -> Dependencies Tab -> Click on the + sign near the bottom -> Select Jars or Directories -> Select the directory that holds your sources/javadocs jars.

文件 -> 项目结构 -> 模块 -> 如果您有多个模块,请选择正确的模块 -> 依赖项选项卡 -> 单击底部附近的 + 号 -> 选择 Jars 或目录 -> 选择保存源/javadocs 的目录罐子。

IntelliJ will see that they are javadoc and sources and index them appropriately.

IntelliJ 将看到它们是 javadoc 和源代码并适当地索引它们。

enter image description here

在此处输入图片说明

回答by Robert Kühne

I'm not sure if your library is stored in a maven repository or not. I assume it is.

我不确定您的库是否存储在 Maven 存储库中。我认为是。

I know of two ways of importing a gradle project into IntelliJ. The first being the "Import Project..." wizard from IntelliJ which works nicely. But it does not import the javadoc jars if they exist. At least I never managed it.

我知道将 gradle 项目导入 IntelliJ 的两种方法。第一个是来自 IntelliJ 的“导入项目...”向导,它运行良好。但它不会导入 javadoc jar(如果它们存在)。至少我从来没有成功过。

The second method uses the idea plugin in gradle 2.1. The plugin generates the project for you. This way I got the javadoc inside. A fully working example for a build.gradle:

第二种方法使用gradle 2.1中的idea插件。该插件为您生成项目。这样我就得到了里面的javadoc。build.gradle 的完整示例:

apply plugin: 'java'
apply plugin: 'idea'

sourceCompatibility = '1.7'
targetCompatibility = '1.7'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.8.1'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'

    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'org.mockito', name: 'mockito-all', version: '1.8.5'
}

idea{
    project {
        languageLevel = '1.7'
    }
    module {
        downloadJavadoc = true // defaults to false
        downloadSources = true
    }
}

The you can call gradle cleanIdea ideawhich creates your project for IntelliJ.

您可以调用gradle cleanIdea idea它为 IntelliJ 创建您的项目。

If your library is not stored in a maven repository you can simply put up a nexus where you upload it. This would allow you to use the method described above.

如果您的库未存储在 Maven 存储库中,您可以简单地在上传它的地方放置一个连接。这将允许您使用上述方法。

回答by Keet Sugathadasa

Libraries cannot be directly used in any program if not properly added to the project gradle files.

如果未正确添加到项目 gradle 文件中,则库不能直接在任何程序中使用。

This can easily be done in smart IDEs like inteli J.

这可以在像 inteli J 这样的智能 IDE 中轻松完成。

1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)

1) 首先作为惯例,在您的项目 src 文件下添加一个名为“libs”的文件夹。(这可以使用 IDE 本身轻松完成)

2) then copy or add your library file (eg: .jar file) to the folder named 'libs'

2) 然后将您的库文件(例如:.jar 文件)复制或添加到名为“libs”的文件夹中

3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.

3) 现在您可以在 libs 文件夹中看到库文件。现在右键单击该文件并选择“添加为库”。这将修复您程序中的所有相关文件,库将直接可供您使用。

Please note:

请注意:

Whenever you are adding libraries to a project, make sure that the project supports the library

每当您向项目添加库时,请确保该项目支持该库

回答by Behrouz

I use intellij and gradle to build my java projects. This will be very easy by following these simple steps:

我使用 intellij 和 gradle 来构建我的 java 项目。通过以下简单步骤,这将非常容易:

  • First at the same level as your src directory add a "directory" with the name of "libs". enter image description here

  • Add your jar files to this directory "libs", (this makes your code portable).

  • Follow the steps:
  • Click Project Structure from File menu.

  • Select Modules that you want to add the new jars at the left panel.

  • Dependencies tab. enter image description here
  • '+' → JARs or directories and add the jar files in the \libs.
  • 首先在与 src 目录相同的级别添加一个名为“libs”的“目录”。 在此处输入图片说明

  • 将您的 jar 文件添加到此目录“libs”,(这使您的代码可移植)。

  • 按照步骤:
  • 从文件菜单中单击项目结构。

  • 在左侧面板中选择要添加新 jar 的模块。

  • 依赖项选项卡。 在此处输入图片说明
  • '+' → JARs 或目录并将 jar 文件添加到 \libs 中。

enter image description here

在此处输入图片说明

  • In the gradle.build file in the dependencies section add:

    dependencies { compile files('libs/"nameOfJarFile".jar') } enter image description here

  • Although not necessary, it is a good practice to invalidate cache and restart at some point. enter image description here

  • 在依赖项部分的 gradle.build 文件中添加:

    依赖{编译文件('libs/“nameOfJarFile”.jar')} 在此处输入图片说明

  • 尽管不是必需的,但在某些时候使缓存无效并重新启动是一个很好的做法。 在此处输入图片说明

回答by Asad Abdin

go to any IntelliJ Decompiled .class file of any library. as shown in image attached here. you will notice Intellij is giving you 2 options

转到任何库的任何 IntelliJ 反编译 .class 文件。如此处所附图片所示。您会注意到 Intellij 为您提供了 2 个选项

  1. Choose source - From Directory
  2. Download - from Maven
  1. 选择来源 - 从目录
  2. 下载 - 从 Maven

enter image description here

在此处输入图片说明