Java 将编译时依赖项写入文件

Java 将编译时依赖项写入文件,java,groovy,gradle,Java,Groovy,Gradle,我正在尝试遍历项目的依赖项,并将它们写入自定义文件中 我熟悉使用“jar”插件创建胖jar的方法,但在自定义任务中使用类似的方法似乎不起作用 我试过的一些代码 // First way I tried to do it. task customTask(){ File manifest = new File('file.txt') manifest << 'Some Text\n' manifest << 'Dependencies:'

我正在尝试遍历项目的依赖项,并将它们写入自定义文件中

我熟悉使用“jar”插件创建胖jar的方法,但在自定义任务中使用类似的方法似乎不起作用

我试过的一些代码

// First way I tried to do it. 
task customTask(){
    File manifest = new File('file.txt')
    manifest << 'Some Text\n'
    manifest << 'Dependencies:'

    configurations.runtime.collect{ manifest << it.name}

}

// The second way I tried to do it. 
task manifest(){
    File manifest = new File('file.txt')
    manifest << 'Header\n'
    manifest << 'Dependencies:'
    FileTree tree = fileTree(dir: configurations.compile, include: '**/*.jar')
    tree.each {File file ->
        manifest << file
    }

}
//我尝试的第一种方法。
任务自定义任务(){
文件清单=新文件('File.txt')
manifest从configuration.runtime返回的对象已经是一个接口,因此您可以轻松地对其进行迭代。这就是为什么您的文件树(dir:xxx)无法工作的原因,因为它采用目录路径并创建文件列表,但配置已经是一个

以下几点对我很有用:

apply plugin: 'groovy'
dependencies {
    // compile gradleApi()
    compile 'junit:junit:4.11'
}

repositories {
    mavenCentral()
}

task t1 << {
    def manifest = project.file('file.txt')
    manifest.delete()
    manifest << 'Dependencies:\n'

    // you can use it.path here if you need full path to jar
    configurations.runtime.each { manifest << it.name + "\n"}

    // also configurations.compile.each works here
}
取消对依赖项中的行
compilegroovyapi()
的注释,会引入更多JAR

通常,我总是使用
project.file()
(您可以只使用
file()
,但我喜欢详细说明项目方法)来创建文件对象,而不是
newfile(foo)

正如Peter Ledbrook在下面的评论中所建议的那样,您可以对此任务做一些进一步的优化,特别是将您的文件作为任务的输出,这样,如果任务的输入没有更改,您的文件就不需要重新创建,作业将跳过。因此,上面的内容相当原始。

返回的对象from configuration.runtime已经是一个接口,因此您可以很容易地对其进行迭代。这就是为什么您的文件树(dir:xxx)无法工作的原因,因为它采用目录路径并创建文件列表,但配置已经是一个

以下几点对我很有用:

apply plugin: 'groovy'
dependencies {
    // compile gradleApi()
    compile 'junit:junit:4.11'
}

repositories {
    mavenCentral()
}

task t1 << {
    def manifest = project.file('file.txt')
    manifest.delete()
    manifest << 'Dependencies:\n'

    // you can use it.path here if you need full path to jar
    configurations.runtime.each { manifest << it.name + "\n"}

    // also configurations.compile.each works here
}
取消对依赖项中的行
compilegroovyapi()
的注释,会引入更多JAR

通常,我总是使用
project.file()
(您可以只使用
file()
,但我喜欢详细说明项目方法)来创建文件对象,而不是
newfile(foo)

正如Peter Ledbrook在下面的评论中所建议的那样,您可以对此任务做一些进一步的优化,特别是将您的文件作为任务的输出,这样,如果任务的输入没有更改,您的文件就不需要重新创建,作业将跳过。因此,上面的内容相当原始。

返回的对象from configuration.runtime已经是一个接口,因此您可以很容易地对其进行迭代。这就是为什么您的文件树(dir:xxx)无法工作的原因,因为它采用目录路径并创建文件列表,但配置已经是一个

以下几点对我很有用:

apply plugin: 'groovy'
dependencies {
    // compile gradleApi()
    compile 'junit:junit:4.11'
}

repositories {
    mavenCentral()
}

task t1 << {
    def manifest = project.file('file.txt')
    manifest.delete()
    manifest << 'Dependencies:\n'

    // you can use it.path here if you need full path to jar
    configurations.runtime.each { manifest << it.name + "\n"}

    // also configurations.compile.each works here
}
取消对依赖项中的行
compilegroovyapi()
的注释,会引入更多JAR

通常,我总是使用
project.file()
(您可以只使用
file()
,但我喜欢详细说明项目方法)来创建文件对象,而不是
newfile(foo)

正如Peter Ledbrook在下面的评论中所建议的那样,您可以对此任务做一些进一步的优化,特别是将您的文件作为任务的输出,这样,如果任务的输入没有更改,您的文件就不需要重新创建,作业将跳过。因此,上面的内容相当原始。

返回的对象from configuration.runtime已经是一个接口,因此您可以很容易地对其进行迭代。这就是为什么您的文件树(dir:xxx)无法工作的原因,因为它采用目录路径并创建文件列表,但配置已经是一个

以下几点对我很有用:

apply plugin: 'groovy'
dependencies {
    // compile gradleApi()
    compile 'junit:junit:4.11'
}

repositories {
    mavenCentral()
}

task t1 << {
    def manifest = project.file('file.txt')
    manifest.delete()
    manifest << 'Dependencies:\n'

    // you can use it.path here if you need full path to jar
    configurations.runtime.each { manifest << it.name + "\n"}

    // also configurations.compile.each works here
}
取消对依赖项中的行
compilegroovyapi()
的注释,会引入更多JAR

通常,我总是使用
project.file()
(您可以只使用
file()
,但我喜欢详细说明项目方法)来创建文件对象,而不是
newfile(foo)


正如Peter Ledbrook在下面的评论中所建议的那样,您可以对此任务做一些进一步的优化,特别是将您的文件作为任务的输出,这样,如果任务的输入没有更改,您的文件就不需要重新创建,作业也将跳过。因此,上面的内容相当原始。

您确定要这样做吗他在第一次尝试中使用了运行时配置。如果您像第二个示例一样更改为编译,那么您应该获得所有jar.configurations.compile.collect{manifest能否请您澄清您试图实现的目标?基本上,这些任务不会工作,因为没有操作。Opai,我想要一个文本文件,其中包含我编译和运行构建所需的所有JAR名称。您的任务仅在配置阶段设置,运行时它们不会做任何事情。需要de>tobad357这是我的想法,但当我按照您的建议执行时,我会遇到此错误。失败:生成失败,出现异常。*其中:生成文件“/Users/davidtaylor/Workspace/plugin/Build.gradle”行:87*出错原因:评估根项目“plugin”时出现问题。“>无法更改配置”:编译完成后已解决。*请尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。确实要在第一次尝试中使用运行时配置。如果您像第二个示例一样更改为编译,则应获取所有jar.configurations.compile.collect{manifest能否请您澄清您试图实现的目标?基本上,这些任务不会工作,因为没有操作。Opai,我想要一个文本文件,其中包含我编译和运行构建所需的所有JAR名称。您的任务仅在配置阶段设置,运行时它们不会做任何事情。需要de>tobad357这是我的想法,但当我按照您的建议执行时,我会出现此错误。失败:生成失败,出现异常。*其中:生成文件'/Users/davidtaylor/Workspace/plugin/Build.gradle'行:87*什么