Gradle插件下载依赖程序

bwitn5fc  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(136)

目前我正在编写一个gradle插件,我需要在给定的任务中以编程方式添加和下载maven依赖项。
我评估了DependencyHandlerArtifactResolutionQuery,但我不知道在哪里以及如何添加Dependency并在mavenCentral存储库中解析它
类似的Maven编码看起来相当简单

Artifact artifact = artifactFactory.createArtifactWithClassifier(groupId, artifactId, version, type, classifier);
      
artifactResolver.resolve(artifact, remoteRepositories, localRepository);

所以我猜/希望在Gradle中有一个类似的简单方法,我只是没有看到它
关于Mathias
更新1:
这是我尝试的一些东西,疯狂地从不同的尝试中获得C&P。值得一提的是,我想下载的依赖项具有分类器ZIP,所以在我的build.gradle中我写的是正常的。

compile 'group:artifact:version@zip

得到所述文件

ComponentIdentifier componentIdentifier = new DefaultModuleComponentIdentifier("com.sap.cloud",
                "neo-java-web-sdk", "3.39.10");

System.out.println("CompIdentifier = " + componentIdentifier.getDisplayName());
//getProject().getDependencies().add("compile", componentIdentifier.getDisplayName());

Configuration configuration = getProject().getConfigurations().getByName("compile");
org.gradle.api.artifacts.Dependency dep2 = new DefaultExternalModuleDependency("com.sap.cloud", "neo-java-web-sdk", "3.39.10");

boolean depList = configuration.getDependencies().add(dep2);
//
configuration.forEach(file -> {
    getProject().getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
});
Set<File> files =  configuration.resolve();
for (File file2 : files) {
    System.out.println("Files: " + file2.getName());
}

DependencyHandler dep =  getProject().getDependencies();
ComponentModuleMetadataHandler modules = dep.getModules();

ArtifactResolutionQuery a = getProject().getDependencies().createArtifactResolutionQuery()
   
 .forComponents(componentIdentifier).withArtifacts(MavenModule.class, SourcesArtifact.class);
ArtifactResolutionResult r = a.execute();
Set<ComponentArtifactsResult> set = r.getResolvedComponents();
Set<ComponentResult> c = r.getComponents();
xcitsw88

xcitsw881#

我认为在Gradle插件中以编程方式下载依赖项的最简单方法与在构建脚本中执行相同。只需创建一个新的配置,添加依赖项并解析配置。观看下面的示例如何在Java(Gradle插件的首选语言)中工作:

Configuration config = project.getConfigurations().create("download");
config.setTransitive(false); // if required
project.getDependencies().add(config.getName(), "com.sap.cloud:neo-java-web-sdk:3.39.10@zip");
File file = config.getSingleFile();

在本例中,配置名称("download")可以是任何尚未用作配置名称的字符串(如compileruntime)。由于配置将在以后解析,因此无论何时重用此代码段(或者多次调用它),都必须使用另一个名称。

相关问题