Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -66,6 +69,7 @@ public void execute() throws MojoExecutionException {
List<Dependency> 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);
Expand Down Expand Up @@ -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<MavenPlugin> mavenPlugins) {
if (mavenPlugins == null || mavenPlugins.isEmpty()) {
return;
}

Build build = pomModel.getBuild();
if (build == null) {
build = new Build();
pomModel.setBuild(build);
}

List<Plugin> plugins = build.getPlugins();
if (plugins == null) {
plugins = new ArrayList<>();
build.setPlugins(plugins);
}

Map<String, Plugin> 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<DependencyNode> pluginDependencies = mavenPlugin.getDependencies();
if (pluginDependencies != null && !pluginDependencies.isEmpty()) {
List<Dependency> dependencies = new ArrayList<>();
Queue<DependencyNode> 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);
}
}
}
}
Loading