Remove RevisionSourceIdentifier
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / Util.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import static org.opendaylight.yangtools.yang2sources.plugin.YangToSourcesProcessor.LOG_PREFIX;
11
12 import java.io.File;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
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;
32
33 final class Util {
34
35     /**
36      * It isn't desirable to create instances of this class.
37      */
38     private Util() {
39     }
40
41     private static final Logger LOG = LoggerFactory.getLogger(Util.class);
42
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);
49             }
50         }
51         return dependencies;
52     }
53
54     /**
55      * Read current project dependencies and check if it don't grab incorrect
56      * artifacts versions which could be in conflict with plugin dependencies.
57      *
58      * @param project
59      *            current project
60      * @param repoSystem
61      *            repository system
62      * @param localRepo
63      *            local repository
64      * @param remoteRepos
65      *            remote repositories
66      */
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);
70         if (plugin == null) {
71             LOG.warn("{} {} not found, dependencies version check skipped", LOG_PREFIX, YangToSourcesMojo.PLUGIN_NAME);
72         } else {
73             Map<Artifact, Collection<Artifact>> pluginDependencies = new HashMap<>();
74             getPluginTransitiveDependencies(plugin, pluginDependencies, repoSystem, localRepo, remoteRepos);
75
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);
81                 }
82             }
83         }
84     }
85
86     /**
87      * Read transitive dependencies of given plugin and store them in map.
88      *
89      * @param plugin
90      *            plugin to read
91      * @param map
92      *            map, where founded transitive dependencies will be stored
93      * @param repoSystem
94      *            repository system
95      * @param localRepository
96      *            local repository
97      * @param remoteRepos
98      *            list of remote repositories
99      */
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) {
103
104         List<Dependency> pluginDependencies = plugin.getDependencies();
105         for (Dependency dep : pluginDependencies) {
106             Artifact artifact = repoSystem.createDependencyArtifact(dep);
107
108             ArtifactResolutionRequest request = new ArtifactResolutionRequest();
109             request.setArtifact(artifact);
110             request.setResolveTransitively(true);
111             request.setLocalRepository(localRepository);
112             request.setRemoteRepositories(remoteRepos);
113
114             ArtifactResolutionResult result = repoSystem.resolve(request);
115             Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
116             map.put(artifact, pluginDependencyDependencies);
117         }
118     }
119
120     /**
121      * Check artifact against collection of dependencies. If collection contains
122      * artifact with same groupId and artifactId, but different version, logs a
123      * warning.
124      *
125      * @param artifact
126      *            artifact to check
127      * @param dependencies
128      *            collection of dependencies
129      */
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);
138                 }
139             }
140         }
141     }
142
143     private static boolean isJar(final File element) {
144         return element.isFile() && element.getName().endsWith(".jar");
145     }
146
147     static SourceIdentifier moduleToIdentifier(final ModuleLike module) {
148         return new SourceIdentifier(Unqualified.of(module.getName()), module.getRevision().orElse(null));
149     }
150 }