gradle如何在Java上获取有关项目的信息(dependecies artifactID)

gradle如何在Java上获取有关项目的信息(dependecies artifactID),java,gradle,groovy,Java,Gradle,Groovy,在Gradle中,我可以获得Groovy上的项目信息(依赖项、工件和组id),如下所示: class TestPlugin implements Plugin<Project> { @Override void apply(Project project) { def example = project.tasks.create("example") << { def dep = pr

在Gradle中,我可以获得Groovy上的项目信息(依赖项、工件和组id),如下所示:

class TestPlugin implements Plugin<Project> {
        @Override
        void apply(Project project) {
            def example = project.tasks.create("example") << {
                def dep = project.configurations.runtime.allDependencies
                def info = project.configurations.runtime.getName()
                def g = project.configurations.runtime.getAllArtifacts()

            }
class TestPlugin实现插件{
@凌驾
无效申请(项目){

def example=project.tasks.create(“example”)您可以添加一个任务,将您喜欢的任何值写入java
属性文件,如下所示:

apply plugin: 'java'
apply plugin: 'application'

def generatedResourcesDir = new File(project.buildDir, 'generated-resources')

tasks.withType(Jar).all { Jar jar -> 
    jar.doFirst {
        def props = new Properties()
        props.foobar = 'baz'
        generatedResourcesDir.mkdirs()
        def writer = new FileWriter(new File(generatedResourcesDir, 'build.properties'))
        try {
            props.store(writer, 'build properties')
            writer.flush()
        } finally {
            writer.close()
        }    
    }
} 

sourceSets {
    main {
        resources {
            srcDir generatedResourcesDir
        }
    }
}

mainClassName = 'BuildProps'
请注意,在根项目的build-output目录中创建了一个目录(称为generated resources,尽管您可以在合理的范围内随意调用它)。在任何
jar
任务之前运行自定义任务后,属性文件将写入此目录。最后,生成的资源目录将添加到
resources
源集中。这意味着它将成为生成的jar文件中的一个资源,因此可以像任何其他资源一样访问;例如充足的:

import java.util.Properties;
import java.io.InputStream;
import java.io.IOException;

class BuildProps {
    public static void main(String[] args) {
        try (InputStream inputStream = 
                BuildProps.class.getClassLoader().getResourceAsStream("build.properties")) {
            Properties props = new Properties();
            props.load(inputStream);
            System.out.println("Build properties:");
            System.out.println("foobar=" + props.getProperty("foobar", ""));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
将打印:

Build properties:
foobar=baz
对于所需的特定属性,可以如下设置:将行
props.foobar='baz'
替换为以下内容

def dependenciesProp = ''
for (def dependency : project.configurations.runtime.allDependencies) {
    dependenciesProp += dependency.toString() + ','
}
props.dependencies = dependenciesProp

props.runtimename = project.configurations.runtime.name

def artifactsProp = ''
for (def artifact : project.configurations.runtime.allArtifacts) {
    artifactsProp += artifact.toString() + ','
}
props.artifacts = artifactsProp

你的意思是“如何用Java编写这个插件”?是的。我发现gradle Java API有方法配置,但不知道如何在这个方法中使用闭包谢谢你的回答我尝试了你的解决方案,它很好,但只需要使用Java(没有Groovy任务).任务和代码需要放在Java类中,而不是build.gradle文件中。我认为Java有gradle API来获取这些信息。