Skip to content

Commit 7b93661

Browse files
committed
Identify Checkstyle version if using a transitive one from the Maven plugin
1 parent 66ab913 commit 7b93661

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/main/java/org/infernus/idea/checkstyle/maven/MavenCheckstyleConfigurator.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package org.infernus.idea.checkstyle.maven;
22

33
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.vfs.VirtualFileManager;
45
import java.io.File;
6+
import java.io.FileReader;
57
import java.net.URI;
68
import java.net.URISyntaxException;
79
import java.nio.file.Files;
810
import java.nio.file.InvalidPathException;
911
import java.nio.file.Path;
1012
import java.util.Comparator;
13+
import java.util.Objects;
1114
import java.util.Spliterator;
1215
import java.util.Spliterators;
1316
import java.util.TreeSet;
1417
import java.util.stream.StreamSupport;
18+
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
1519
import org.infernus.idea.checkstyle.CheckstyleProjectService;
1620
import org.infernus.idea.checkstyle.config.PluginConfigurationBuilder;
1721
import org.infernus.idea.checkstyle.config.PluginConfigurationManager;
@@ -23,10 +27,13 @@
2327
import org.jdom.Element;
2428
import org.jetbrains.annotations.NotNull;
2529
import org.jetbrains.annotations.Nullable;
30+
import org.jetbrains.idea.maven.dom.MavenDomUtil;
31+
import org.jetbrains.idea.maven.dom.MavenPropertyResolver;
2632
import org.jetbrains.idea.maven.importing.MavenAfterImportConfigurator;
2733
import org.jetbrains.idea.maven.importing.MavenWorkspaceConfigurator.MavenProjectWithModules;
2834
import org.jetbrains.idea.maven.model.MavenId;
2935
import org.jetbrains.idea.maven.project.MavenProject;
36+
import org.jetbrains.idea.maven.utils.MavenArtifactUtil;
3037

3138
/**
3239
* Importer to automatically configure the Checkstyle IntelliJ plugin settings based on the
@@ -62,22 +69,39 @@ public void afterImport(@NotNull final MavenAfterImportConfigurator.Context cont
6269
final var checkstyleMavenPlugin = mavenProject.findPlugin(
6370
MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getGroupId(),
6471
MAVEN_CHECKSTYLE_PLUGIN_MAVEN_ID.getArtifactId());
65-
6672
if (checkstyleMavenPlugin == null) {
6773
return;
6874
}
6975

7076
final var checkstyleDependencyMavenId = checkstyleMavenPlugin.getDependencies().stream()
7177
.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+
});
73101

74102
final var pluginConfigurationBuilder = PluginConfigurationBuilder.from(
75103
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) {
81105
// Checkstyle Version
82106
pluginConfigurationBuilder.withCheckstyleVersion(
83107
checkstyleDependencyMavenId.getVersion());

0 commit comments

Comments
 (0)