diff --git a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java index ed9739ed..9e12ab2a 100644 --- a/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java +++ b/plugin-modernizer-core/src/main/java/io/jenkins/tools/pluginmodernizer/core/impl/MavenInvoker.java @@ -8,15 +8,15 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.jenkins.tools.pluginmodernizer.core.config.Config; import io.jenkins.tools.pluginmodernizer.core.config.Settings; +import io.jenkins.tools.pluginmodernizer.core.model.RecipeDescriptor; import org.apache.maven.artifact.versioning.ComparableVersion; import org.apache.maven.shared.invoker.DefaultInvocationRequest; import org.apache.maven.shared.invoker.DefaultInvoker; @@ -32,7 +32,6 @@ public class MavenInvoker { private static final Logger LOG = LoggerFactory.getLogger(MavenInvoker.class); - private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); private final Config config; @@ -84,11 +83,11 @@ private List createGoalsList() throws IOException { goals.add("org.openrewrite.maven:rewrite-maven-plugin:" + Settings.MAVEN_REWRITE_PLUGIN_VERSION + ":" + mode); try (InputStream inputStream = getClass().getResourceAsStream("/" + Settings.RECIPE_DATA_YAML_PATH)) { - ArrayNode recipesNode = (ArrayNode) objectMapper.readTree(inputStream); + List recipeDescriptors = new YAMLMapper().readValue(inputStream, new TypeReference>() {}); List recipes = config.getRecipes(); - List activeRecipes = getActiveRecipes(recipes, recipesNode); - List recipeArtifactCoordinates = getRecipeArtifactCoordinates(recipes, recipesNode); + List activeRecipes = getActiveRecipes(recipes, recipeDescriptors); + List recipeArtifactCoordinates = getRecipeArtifactCoordinates(recipes, recipeDescriptors); if (activeRecipes.isEmpty()) { return null; @@ -101,30 +100,20 @@ private List createGoalsList() throws IOException { return goals; } - private List getActiveRecipes(List recipes, ArrayNode recipesNode) { - List activeRecipes = new ArrayList<>(); - for (String recipe : recipes) { - for (JsonNode recipeNode : recipesNode) { - if (recipeNode.get("name").asText().equals(recipe)) { - activeRecipes.add(recipeNode.get("fqcn").asText()); - break; - } - } - } - return activeRecipes; + private List getActiveRecipes(List recipes, List recipeDescriptors) { + return recipes.stream() + .flatMap(recipe -> recipeDescriptors.stream() + .filter(descriptor -> descriptor.name().equals(recipe)) + .map(RecipeDescriptor::fqcn)) + .collect(Collectors.toList()); } - private List getRecipeArtifactCoordinates(List recipes, ArrayNode recipesNode) { - List recipeArtifactCoordinates = new ArrayList<>(); - for (String recipe : recipes) { - for (JsonNode recipeNode : recipesNode) { - if (recipeNode.get("name").asText().equals(recipe)) { - recipeArtifactCoordinates.add(recipeNode.get("artifactCoordinates").asText()); - break; - } - } - } - return recipeArtifactCoordinates; + private List getRecipeArtifactCoordinates(List recipes, List recipeDescriptors) { + return recipes.stream() + .flatMap(recipe -> recipeDescriptors.stream() + .filter(descriptor -> descriptor.name().equals(recipe)) + .map(RecipeDescriptor::artifactCoordinates)) + .collect(Collectors.toList()); } private void invokeGoals(String plugin, String pluginPath, List goals) {