获取JavaDoc jar以获取Gradle中的依赖项 [英] Get JavaDoc jars for dependencies in Gradle

查看:335
本文介绍了获取JavaDoc jar以获取Gradle中的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行Gradle任务,为基于Eclipse的IDE创建项目.这需要同时生成.project.classpath文件.这部分绝对没有问题,我可以通过apply plugin: 'eclipse'使用Gradle Eclipse插件,让我的任务依赖于eclipseProjecteclipseClasspath并具有更改的配置.但是还有一个附加要求,那就是整个项目必须是可移植的(即使没有Gradle的环境也是如此),因此不可能仅定义外部依赖项,因为类路径将引用缓存.我通过使用定义依赖项的主项目解决了这个问题:

I am working on a Gradle task to create a project for an IDE based on Eclipse. This requires to generate both a .project and a .classpath file. This part is absolutely no problem and I can use the Gradle Eclipse Plugin via apply plugin: 'eclipse', to let my task depend on eclipseProject and eclipseClasspath with altered configurations. But an additional requirement says, that the whole project must be portable (even to environments without Gradle), so it is not possible to just define the external dependencies, because the classpath will reference to the cache. I solved this problem by using a main project defining the dependencies:

// Define configurations
configurations {
    libs
}

// Define repositories
repositories {                                                              
    mavenCentral()
}

// Include dependencies via 'groupID:artifactID:version[:classifier]'
dependencies {
     libs 'junit:junit:4.12'
}

这些依赖项将下载到缓存中,并且一个简单的复制任务会将它们复制到我的项目'lib'文件夹中:

These dependencies will be downloaded to the cache, and a simple Copy task copies them to my project 'lib' folder:

// Copies the dependencies to project 'lib' folder
task copyLibs(type: Copy) {
     from configurations.libs
     into "$projectDir/$projectName/lib"
}

一个子项目现在使用这些复制的库作为依赖项,只需使用:

A subproject now uses those copied libraries as dependencies, simply using:

dependencies {
    compile fileTree(dir: 'lib', include: ['*.jar'])
}

子项目中的另一个任务使路径相对化,因此我可以移动项目并在需要的任何地方调用gradle build.这是类路径的外观:

An additional task in the subproject relativizes the paths, so I can move my project and call gradle build anywhere I want. This is how the classpath looks:

<classpath>
     <classpathentry path="bin" kind="output"/>
     <classpathentry kind="src" path="src"/>
     <classpathentry kind="lib" path="lib\hamcrest-core-1.3.jar"/>
     <classpathentry kind="lib" path="lib\junit-4.12.jar"/>
</classpath>

现在,我想包含依赖项的javadoc -jars.在使用本地副本之前,我可以简单地使用此Eclipse插件功能:

Now I want to include the javadoc-jars of the dependencies. Before using the local copies, I could simply use this Eclipse plugin feature:

eclipse {
    classpath {
         downloadJavadoc = true
         downloadSources = true
    }
}

但是现在我的项目使用文件作为依赖项,因此Eclipse插件也不能只是简单地请求Javadoc文件.每次使用:javadoc分类器后缀添加第二个依赖项时,如何请求下载javadoc -jars 而没有?下载后,我可以自己在缓存中搜索它,也可以按预期方式将其添加到.classpath中:

But now my project uses files as dependencies, so the Eclipse plugin can't just simply request the Javadoc files, too. How can I request the download of the javadoc-jars without adding a second dependency everytime with the :javadoc classifier suffix? Once it gets downloaded, I can search it in the cache myself and also add it to the .classpath in the intended way:

<classpathentry kind="lib" path="lib\junit-4.12.jar">
  <attributes>
    <attribute name="javadoc_location" value="jar:platform:/path/to/javadoc/junit-4.12-javadoc.jar"/>
  </attributes>
</classpathentry>

但目前,仅在以下情况下下载javadoc -jars:a)与常规依赖项一样定义带有:javadoc分类符后缀的依赖项,或者b)依赖项未复制,但直接由带有Eclipse插件(带有downloadJavadoc = true)的项目.

But at the moment, the javadoc-jars are only downloaded, if a) the dependency with the :javadoc classifier suffix is defined just like the regular dependency or b) the dependencies are not copied, but directly referenced by a project with the Eclipse plugin (with downloadJavadoc = true).

推荐答案

即使我认为我需要更改构建文件的结构,我也找到了针对此特定情况的解决方案,因此我想为其提供解决方案其他可能会偶然发现相同问题的人.

Even if I think, that I need to change the structure of my build file, I found a solution for this specific case, so I want to provide it for others who might stumble upon the same problem.

我找到了

I found the ArtifactResolutionQuery, which can resolve additional required JARs (Javadoc and sources), so I wrote a task to copy those JARs, too.

// Copies the javadoc files to project 'lib/javadoc' folder
task copyJavadocs(type: Copy) {
    from {
        dependencies.createArtifactResolutionQuery()
            .forComponents(
                 configurations.compile.incoming.resolutionResult
                     .allDependencies.collect { it.selected.id }
            )
            .withArtifacts(JvmLibrary, JavadocArtifact)
            .execute()
            .resolvedComponents
            .collectMany {
                it.artifactResults
                    .collect { it.file.path }
            }
    }
    into "$projectDir/$sunriseProjectName/lib/javadoc"
}

这篇关于获取JavaDoc jar以获取Gradle中的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆