2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang2sources.plugin;
10 import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.LOG_PREFIX;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.repository.ArtifactRepository;
21 import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
22 import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
23 import org.apache.maven.model.Dependency;
24 import org.apache.maven.model.Plugin;
25 import org.apache.maven.project.MavenProject;
26 import org.apache.maven.repository.RepositorySystem;
27 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
28 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
36 * It isn't desirable to create instances of this class.
41 private static final Logger LOG = LoggerFactory.getLogger(Util.class);
43 static List<File> getClassPath(final MavenProject project) {
44 final List<File> dependencies = new ArrayList<>();
45 for (Artifact element : project.getArtifacts()) {
46 File asFile = element.getFile();
47 if (isJar(asFile) || asFile.isDirectory()) {
48 dependencies.add(asFile);
55 * Read current project dependencies and check if it don't grab incorrect
56 * artifacts versions which could be in conflict with plugin dependencies.
67 static void checkClasspath(final MavenProject project, final RepositorySystem repoSystem,
68 final ArtifactRepository localRepo, final List<ArtifactRepository> remoteRepos) {
69 Plugin plugin = project.getPlugin(YangToSourcesMojo.PLUGIN_NAME);
71 LOG.warn("{} {} not found, dependencies version check skipped", LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME);
73 Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
74 getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
76 Set<Artifact> projectDependencies = project.getDependencyArtifacts();
77 for (Map.Entry<Artifact, Collection<Artifact>> entry : pluginDependencies.entrySet()) {
78 checkArtifact(entry.getKey(), projectDependencies);
79 for (Artifact dependency : entry.getValue()) {
80 checkArtifact(dependency, projectDependencies);
87 * Read transitive dependencies of given plugin and store them in map.
92 * map, where founded transitive dependencies will be stored
95 * @param localRepository
98 * list of remote repositories
100 private static void getPluginTransitiveDependencies(final Plugin plugin,
101 final Map<Artifact, Collection<Artifact>> map, final RepositorySystem repoSystem,
102 final ArtifactRepository localRepository, final List<ArtifactRepository> remoteRepos) {
104 List<Dependency> pluginDependencies = plugin.getDependencies();
105 for (Dependency dep : pluginDependencies) {
106 Artifact artifact = repoSystem.createDependencyArtifact(dep);
108 ArtifactResolutionRequest request = new ArtifactResolutionRequest();
109 request.setArtifact(artifact);
110 request.setResolveTransitively(true);
111 request.setLocalRepository(localRepository);
112 request.setRemoteRepositories(remoteRepos);
114 ArtifactResolutionResult result = repoSystem.resolve(request);
115 Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
116 map.put(artifact, pluginDependencyDependencies);
121 * Check artifact against collection of dependencies. If collection contains
122 * artifact with same groupId and artifactId, but different version, logs a
127 * @param dependencies
128 * collection of dependencies
130 private static void checkArtifact(final Artifact artifact, final Collection<Artifact> dependencies) {
131 for (org.apache.maven.artifact.Artifact d : dependencies) {
132 if (artifact.getGroupId().equals(d.getGroupId()) && artifact.getArtifactId().equals(d.getArtifactId())) {
133 if (!artifact.getVersion().equals(d.getVersion())) {
134 LOG.warn("{} Dependency resolution conflict:", LOG_PREFIX);
135 LOG.warn("{} '{}' dependency [{}] has different version than one declared in current project [{}]"
136 + ". It is recommended to fix this problem because it may cause compilation errors.",
137 LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME, artifact, d);
143 private static boolean isJar(final File element) {
144 return element.isFile() && element.getName().endsWith(".jar");
147 static SourceIdentifier moduleToIdentifier(final ModuleLike module) {
148 return new SourceIdentifier(Unqualified.of(module.getName()), module.getRevision().orElse(null));