diff --git a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/FreezeDependencyMojo.java b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/FreezeDependencyMojo.java index 024f0607b..e65f4e1e8 100644 --- a/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/FreezeDependencyMojo.java +++ b/maven_plugin/src/main/java/io/github/chains_project/maven_lockfile/FreezeDependencyMojo.java @@ -1,6 +1,7 @@ package io.github.chains_project.maven_lockfile; import io.github.chains_project.maven_lockfile.data.LockFile; +import io.github.chains_project.maven_lockfile.data.MavenPlugin; import io.github.chains_project.maven_lockfile.graph.DependencyNode; import io.github.chains_project.maven_lockfile.reporting.PluginLogManager; import java.io.File; @@ -9,9 +10,11 @@ import java.io.IOException; import java.util.*; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Build; import org.apache.maven.model.Dependency; import org.apache.maven.model.DependencyManagement; import org.apache.maven.model.Model; +import org.apache.maven.model.Plugin; import org.apache.maven.model.io.xpp3.MavenXpp3Reader; import org.apache.maven.model.io.xpp3.MavenXpp3Writer; import org.apache.maven.plugin.AbstractMojo; @@ -66,6 +69,7 @@ public void execute() throws MojoExecutionException { List filteredDependencies = getNearestVersionDependency(lockFile); Model pomModel = readPomFile(pomFile); updateDependencies(pomModel, filteredDependencies); + updatePlugins(pomModel, lockFile.getMavenPlugins()); writePomLockFile(pomModel, pomLockFile); } catch (IOException | XmlPullParserException e) { throw new MojoExecutionException("Could not freeze versions", e); @@ -170,4 +174,72 @@ private String convertSoftToExactVersionString(String version) { } return "[" + version + "]"; } + + /** + * Updates the plugins in the POM model with plugin dependencies from the lock file. + * + * @param pomModel the POM model to update + * @param mavenPlugins the set of Maven plugins from the lock file + */ + private void updatePlugins(Model pomModel, Set mavenPlugins) { + if (mavenPlugins == null || mavenPlugins.isEmpty()) { + return; + } + + Build build = pomModel.getBuild(); + if (build == null) { + build = new Build(); + pomModel.setBuild(build); + } + + List plugins = build.getPlugins(); + if (plugins == null) { + plugins = new ArrayList<>(); + build.setPlugins(plugins); + } + + Map existingPluginsMap = new HashMap<>(); + for (Plugin plugin : plugins) { + String key = plugin.getGroupId() + ":" + plugin.getArtifactId(); + existingPluginsMap.put(key, plugin); + } + + // Process each plugin from the lock file + for (MavenPlugin mavenPlugin : mavenPlugins) { + String key = mavenPlugin.getGroupId().getValue() + ":" + + mavenPlugin.getArtifactId().getValue(); + Plugin plugin = existingPluginsMap.get(key); + + if (plugin == null) { + // Plugin doesn't exist in the POM, create it + plugin = new Plugin(); + plugin.setGroupId(mavenPlugin.getGroupId().getValue()); + plugin.setArtifactId(mavenPlugin.getArtifactId().getValue()); + plugins.add(plugin); + } + + // Add plugin dependencies if they exist + Set pluginDependencies = mavenPlugin.getDependencies(); + if (pluginDependencies != null && !pluginDependencies.isEmpty()) { + List dependencies = new ArrayList<>(); + Queue depQueue = new ArrayDeque<>(pluginDependencies); + + while (!depQueue.isEmpty()) { + DependencyNode depNode = depQueue.poll(); + if (depNode.isIncluded()) { + Dependency dep = toMavenDependency(depNode); + String scope = dep.getScope() != null ? dep.getScope() : "compile"; + + // Plugin dependencies can only have scope: compile, runtime, or system + if (scope.equals("compile") || scope.equals("runtime") || scope.equals("system")) { + dependencies.add(dep); + } + } + depQueue.addAll(depNode.getChildren()); + } + + plugin.setDependencies(dependencies); + } + } + } }