Reputation: 23
How can get dependencies of current project, using Java ? I try this code in Java class but result is empty:
class Example implements Plugin<Project> {
void apply(Project project) {
project.getConfigurations().getByName("runtime").getAllDependencies();
}
}
Thanks for your answer JBirdVegas . I try write your example on Java:
List<String> deps = new ArrayList<>();
Configuration configuration = project.getConfigurations().getByName("compile");
for (File file : configuration) {
deps.add(file.toString());
}
But have error:
Cannot change dependencies of configuration ':compile' after it has been resolved.
when run gradle build
Upvotes: 2
Views: 4486
Reputation: 11403
You're just missing a step to iterate over the found dependencies
Groovy:
class Example implements Plugin<Project> {
void apply(Project project) {
def configuration = project.configurations.getByName('compile')
configuration.each { File file ->
println "Found project dependency @ $file.absolutePath"
}
}
}
Java 8:
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
public class Example implements Plugin<Project> {
@Override
public void apply(Project project) {
Configuration configuration = project.getConfigurations().getByName("compile");
configuration.forEach(file -> {
project.getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
});
}
}
Java 7:
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import java.io.File;
public class Example implements Plugin<Project> {
@Override
public void apply(Project project) {
Configuration configuration = project.getConfigurations().getByName("compile");
for (File file : configuration) {
project.getLogger().lifecycle("Found project dependency @ " + file.getAbsolutePath());
}
}
}
Upvotes: 4