|
1 | 1 | package org.infernus.idea.checkstyle.maven; |
2 | 2 |
|
3 | 3 | import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.openapi.vfs.VirtualFileManager; |
4 | 5 | import java.io.File; |
| 6 | +import java.io.FileReader; |
5 | 7 | import java.net.URI; |
6 | 8 | import java.net.URISyntaxException; |
7 | 9 | import java.nio.file.Files; |
8 | 10 | import java.nio.file.InvalidPathException; |
9 | 11 | import java.nio.file.Path; |
10 | 12 | import java.util.Comparator; |
| 13 | +import java.util.Objects; |
11 | 14 | import java.util.Spliterator; |
12 | 15 | import java.util.Spliterators; |
13 | 16 | import java.util.TreeSet; |
14 | 17 | import java.util.stream.StreamSupport; |
| 18 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
15 | 19 | import org.infernus.idea.checkstyle.CheckstyleProjectService; |
16 | 20 | import org.infernus.idea.checkstyle.config.PluginConfigurationBuilder; |
17 | 21 | import org.infernus.idea.checkstyle.config.PluginConfigurationManager; |
|
23 | 27 | import org.jdom.Element; |
24 | 28 | import org.jetbrains.annotations.NotNull; |
25 | 29 | import org.jetbrains.annotations.Nullable; |
| 30 | +import org.jetbrains.idea.maven.dom.MavenDomUtil; |
| 31 | +import org.jetbrains.idea.maven.dom.MavenPropertyResolver; |
26 | 32 | import org.jetbrains.idea.maven.importing.MavenAfterImportConfigurator; |
27 | 33 | import org.jetbrains.idea.maven.importing.MavenWorkspaceConfigurator.MavenProjectWithModules; |
28 | 34 | import org.jetbrains.idea.maven.model.MavenId; |
29 | 35 | import org.jetbrains.idea.maven.project.MavenProject; |
| 36 | +import org.jetbrains.idea.maven.utils.MavenArtifactUtil; |
30 | 37 |
|
31 | 38 | /** |
32 | 39 | * Importer to automatically configure the Checkstyle IntelliJ plugin settings based on the |
@@ -62,22 +69,39 @@ public void afterImport(@NotNull final MavenAfterImportConfigurator.Context cont |
62 | 69 | final var checkstyleMavenPlugin = mavenProject.findPlugin( |
63 | 70 | MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getGroupId(), |
64 | 71 | MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getArtifactId()); |
65 | | - |
66 | 72 | if (checkstyleMavenPlugin == null) { |
67 | 73 | return; |
68 | 74 | } |
69 | 75 |
|
70 | 76 | final var checkstyleDependencyMavenId = checkstyleMavenPlugin.getDependencies().stream() |
71 | 77 | .filter(dependency -> CHECKSTYLE_MAVEN_ID.equals(dependency.getGroupId(), |
72 | | - dependency.getArtifactId())).findFirst().orElse(null); |
| 78 | + dependency.getArtifactId())).findFirst().orElseGet(() -> { |
| 79 | + final var mavenPluginPath = MavenArtifactUtil.getArtifactFile( |
| 80 | + mavenProject.getLocalRepository(), checkstyleMavenPlugin.getMavenId(), "pom"); |
| 81 | + |
| 82 | + try { |
| 83 | + final var mavenReader = new MavenXpp3Reader(); |
| 84 | + final var model = mavenReader.read(new FileReader(mavenPluginPath.toFile())); |
| 85 | + final var checkstyleDependency = model.getDependencies().stream().filter( |
| 86 | + dependency -> CHECKSTYLE_MAVEN_ID.equals(dependency.getGroupId(), |
| 87 | + dependency.getArtifactId())).findFirst().orElseThrow(); |
| 88 | + final var mavenPluginVirtualFile = VirtualFileManager.getInstance() |
| 89 | + .findFileByNioPath(mavenPluginPath); |
| 90 | + final var mavenDomProjectModel = MavenDomUtil.getMavenDomProjectModel(project, |
| 91 | + Objects.requireNonNull(mavenPluginVirtualFile)); |
| 92 | + final var version = MavenPropertyResolver.resolve( |
| 93 | + checkstyleDependency.getVersion(), |
| 94 | + Objects.requireNonNull(mavenDomProjectModel)); |
| 95 | + return new MavenId(checkstyleDependency.getGroupId(), |
| 96 | + checkstyleDependency.getArtifactId(), version); |
| 97 | + } catch (final Exception exception) { |
| 98 | + throw new RuntimeException(exception); |
| 99 | + } |
| 100 | + }); |
73 | 101 |
|
74 | 102 | final var pluginConfigurationBuilder = PluginConfigurationBuilder.from( |
75 | 103 | currentPluginConfiguration); |
76 | | - // TODO: This will be null if checkstyle isn't declared explicitly. |
77 | | - // Meaning the transitive dependency version won't be found. |
78 | | - // Would be great to resolve that transitive version somehow. |
79 | | - if (checkstyleDependencyMavenId != null |
80 | | - && checkstyleDependencyMavenId.getVersion() != null) { |
| 104 | + if (checkstyleDependencyMavenId.getVersion() != null) { |
81 | 105 | // Checkstyle Version |
82 | 106 | pluginConfigurationBuilder.withCheckstyleVersion( |
83 | 107 | checkstyleDependencyMavenId.getVersion()); |
|
0 commit comments