All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openrewrite.maven.internal.RawPom Maven / Gradle / Ivy

There is a newer version: 8.40.3
Show newest version
/*
 * Copyright 2020 the original author or authors.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* https://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.openrewrite.maven.internal; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import lombok.*; import lombok.experimental.FieldDefaults; import lombok.experimental.NonFinal; import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; import org.openrewrite.internal.StringUtils; import org.openrewrite.maven.tree.*; import javax.xml.bind.annotation.XmlRootElement; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; /** * A value object deserialized directly from POM XML */ @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @ToString(onlyExplicitlyIncluded = true) @EqualsAndHashCode(onlyExplicitlyIncluded = true) @Data @XmlRootElement(name = "project") @SuppressWarnings("unused") public class RawPom { // Obsolete field supplanted by the "modelVersion" field in modern poms @Nullable String pomVersion; @Nullable Parent parent; @EqualsAndHashCode.Include @ToString.Include @Nullable String groupId; @EqualsAndHashCode.Include @ToString.Include String artifactId; @EqualsAndHashCode.Include @ToString.Include @Nullable String version; // Obsolete field supplanted by the "version" field in modern poms @Nullable String currentVersion; @EqualsAndHashCode.Include @ToString.Include @Nullable @NonFinal @Setter(AccessLevel.PACKAGE) String snapshotVersion; @Nullable String name; @Nullable String description; @Nullable Prerequisites prerequisites; @Nullable String packaging; @Nullable Dependencies dependencies; @Nullable DependencyManagement dependencyManagement; @Nullable Map properties; @Nullable Build build; @Nullable RawRepositories repositories; @Nullable Licenses licenses; @Nullable Profiles profiles; public static RawPom parse(InputStream inputStream, @Nullable String snapshotVersion) { try { RawPom pom = MavenXmlMapper.readMapper().readValue(inputStream, RawPom.class); if (snapshotVersion != null) { pom.setSnapshotVersion(snapshotVersion); } return pom; } catch (IOException e) { throw new UncheckedIOException("Failed to parse pom", e); } } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Data public static class Dependency { String groupId; String artifactId; @Nullable String version; @Nullable String scope; @Nullable String type; @Nullable String classifier; @Nullable String optional; @Nullable @JacksonXmlElementWrapper List exclusions; } @Getter public static class DependencyManagement { @Nullable private final Dependencies dependencies; public DependencyManagement() { this.dependencies = null; } public DependencyManagement(@JsonProperty("dependencies") @Nullable Dependencies dependencies) { this.dependencies = dependencies; } } @Getter public static class Dependencies { private final List dependencies; public Dependencies() { this.dependencies = emptyList(); } public Dependencies(@JacksonXmlProperty(localName = "dependency") List dependencies) { this.dependencies = dependencies; } } @Getter public static class Licenses { private final List licenses; public Licenses() { this.licenses = emptyList(); } public Licenses(@JacksonXmlProperty(localName = "license") List licenses) { this.licenses = licenses; } } @Getter public static class Prerequisites { @JacksonXmlProperty(localName = "maven") @Nullable public String maven; } @Getter public static class Profiles { private final List profiles; public Profiles() { this.profiles = emptyList(); } public Profiles(@JacksonXmlProperty(localName = "profile") List profiles) { this.profiles = profiles; } } @FieldDefaults(level = AccessLevel.PRIVATE) @Data @AllArgsConstructor public static class Build { @NonFinal @Nullable @JacksonXmlElementWrapper(localName = "plugins") @JacksonXmlProperty(localName = "plugin") List plugins; @Nullable @JacksonXmlProperty(localName = "pluginManagement") PluginManagement pluginManagement; public Build() { plugins = null; pluginManagement = null; } } @FieldDefaults(level = AccessLevel.PRIVATE) @Data public static class PluginManagement { @Nullable @JacksonXmlElementWrapper(localName = "plugins") @JacksonXmlProperty(localName = "plugin") List plugins; } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Data public static class Plugin { String groupId; String artifactId; @Nullable String version; @Nullable String extensions; @Nullable String inherited; @Nullable JsonNode configuration; @NonFinal @Nullable @JacksonXmlElementWrapper(localName = "dependencies") @JacksonXmlProperty(localName = "dependency") List dependencies; @NonFinal @Nullable @JacksonXmlElementWrapper(localName = "executions") @JacksonXmlProperty(localName = "execution") List executions; } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Data public static class Execution { String id; @NonFinal @JacksonXmlElementWrapper(localName = "goals") @JacksonXmlProperty(localName = "goal") List goals; String phase; @Nullable String inherited; @Nullable JsonNode configuration; } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Data public static class Parent { String groupId; String artifactId; String version; @Nullable String relativePath; } @ToString @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @EqualsAndHashCode @Getter public static class License { String name; public License() { this.name = ""; } public License(@JsonProperty("name") String name) { this.name = name; } } @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Data public static class Profile { @Nullable String id; @Nullable ProfileActivation activation; @Nullable Map properties; @Nullable Dependencies dependencies; @Nullable DependencyManagement dependencyManagement; @Nullable RawRepositories repositories; } public @Nullable String getGroupId() { return groupId == null && parent != null ? parent.getGroupId() : groupId; } public @Nullable String getVersion() { if(version == null) { if(currentVersion == null) { if(parent == null) { return null; } else { return parent.getVersion(); } } else { return currentVersion; } } return version; } public Pom toPom(@Nullable Path inputPath, @Nullable MavenRepository repo) { org.openrewrite.maven.tree.Parent parent = getParent() == null ? null : new org.openrewrite.maven.tree.Parent(new GroupArtifactVersion( getParent().getGroupId(), getParent().getArtifactId(), getParent().getVersion()), getParent().getRelativePath()); Pom.PomBuilder builder = Pom.builder() .sourcePath(inputPath) .repository(repo) .parent(parent) .gav(new ResolvedGroupArtifactVersion( repo == null ? null : repo.getUri(), Objects.requireNonNull(getGroupId()), artifactId, Objects.requireNonNull(getVersion()), null)) .name(name) .obsoletePomVersion(pomVersion) .prerequisites(prerequisites == null ? null : new org.openrewrite.maven.tree.Prerequisites(prerequisites.getMaven())) .packaging(packaging) .properties(getProperties() == null ? emptyMap() : getProperties()) .licenses(mapLicenses(getLicenses())) .profiles(mapProfiles(getProfiles())); if(StringUtils.isBlank(pomVersion)) { builder.dependencies(mapRequestedDependencies(getDependencies())) .dependencyManagement(mapDependencyManagement(getDependencyManagement())) .repositories(mapRepositories(getRepositories())) .plugins(mapPlugins((build != null) ? build.getPlugins() : null)) .pluginManagement(mapPlugins((build != null && build.getPluginManagement() != null) ? build.getPluginManagement().getPlugins() : null)); } return builder.build(); } private List mapLicenses(@Nullable Licenses rawLicenses) { List licenses = emptyList(); if (rawLicenses != null) { List unmappedLicenses = rawLicenses.getLicenses(); if (unmappedLicenses != null) { licenses = new ArrayList<>(unmappedLicenses.size()); for (License l : unmappedLicenses) { licenses.add(org.openrewrite.maven.tree.License.fromName(l.getName())); } } } return licenses; } private List mapProfiles(@Nullable Profiles rawProfiles) { List profiles = emptyList(); if (rawProfiles != null) { List unmappedProfiles = rawProfiles.getProfiles(); if (unmappedProfiles != null) { profiles = new ArrayList<>(unmappedProfiles.size()); // profiles are mapped in reverse order to put them in precedence order left to right for (int i = unmappedProfiles.size() - 1; i >= 0; i--) { Profile p = unmappedProfiles.get(i); profiles.add(new org.openrewrite.maven.tree.Profile( p.getId(), p.getActivation(), p.getProperties() == null ? emptyMap() : p.getProperties(), mapRequestedDependencies(p.getDependencies()), mapDependencyManagement(p.getDependencyManagement()), mapRepositories(p.getRepositories()), mapPlugins((build != null) ? build.getPlugins() : null), mapPlugins((build != null && build.getPluginManagement() != null) ? build.getPluginManagement().getPlugins() : null) )); } } } return profiles; } @NonNull private List mapRepositories(@Nullable RawRepositories rawRepositories) { List pomRepositories = emptyList(); if (rawRepositories != null) { List unmappedRepos = rawRepositories.getRepositories(); if (unmappedRepos != null) { pomRepositories = new ArrayList<>(unmappedRepos.size()); for (RawRepositories.Repository r : unmappedRepos) { pomRepositories.add(new MavenRepository(r.getId(), r.getUrl(), r.getReleases() == null ? null : r.getReleases().getEnabled(), r.getSnapshots() == null ? null : r.getSnapshots().getEnabled(), false, null, null, null, null)); } } } return pomRepositories; } private List mapDependencyManagement(@Nullable DependencyManagement rawDependencyManagement) { List dependencyManagementDependencies = emptyList(); if (rawDependencyManagement != null && rawDependencyManagement.getDependencies() != null) { List unmappedDependencies = rawDependencyManagement.getDependencies().getDependencies(); if (unmappedDependencies != null) { dependencyManagementDependencies = new ArrayList<>(unmappedDependencies.size()); for (Dependency d : unmappedDependencies) { GroupArtifactVersion dGav = new GroupArtifactVersion(d.getGroupId(), d.getArtifactId(), d.getVersion()); if ("import".equals(d.getScope())) { dependencyManagementDependencies.add(new ManagedDependency.Imported(dGav)); } else { dependencyManagementDependencies.add(new ManagedDependency.Defined(dGav, d.getScope(), d.getType(), d.getClassifier(), d.getExclusions())); } } } } return dependencyManagementDependencies; } private List mapRequestedDependencies(@Nullable Dependencies rawDependencies) { List dependencies = emptyList(); if (rawDependencies != null && rawDependencies.getDependencies() != null) { List unmappedDependencies = rawDependencies.getDependencies(); if (unmappedDependencies != null) { dependencies = new ArrayList<>(unmappedDependencies.size()); for (Dependency d : unmappedDependencies) { GroupArtifactVersion dGav = new GroupArtifactVersion(d.getGroupId(), d.getArtifactId(), d.getVersion()); dependencies.add(new org.openrewrite.maven.tree.Dependency(dGav, d.getClassifier(), d.getType(), d.getScope(), d.getExclusions(), d.getOptional())); } } } return dependencies; } private List mapRequestedDependencies(@Nullable List rawDependencies) { List dependencies = emptyList(); if (rawDependencies != null) { dependencies = new ArrayList<>(rawDependencies.size()); for (Dependency d : rawDependencies) { GroupArtifactVersion dGav = new GroupArtifactVersion(d.getGroupId(), d.getArtifactId(), d.getVersion()); dependencies.add(new org.openrewrite.maven.tree.Dependency(dGav, d.getClassifier(), d.getType(), d.getScope(), d.getExclusions(), d.getOptional())); } } return dependencies; } private List mapPlugins(@Nullable List rawPlugins) { List plugins = emptyList(); if (rawPlugins != null) { plugins = new ArrayList<>(rawPlugins.size()); for (Plugin rawPlugin : rawPlugins) { plugins.add(new org.openrewrite.maven.tree.Plugin( rawPlugin.getGroupId(), rawPlugin.getArtifactId(), rawPlugin.getVersion(), rawPlugin.getExtensions(), rawPlugin.getInherited(), rawPlugin.getConfiguration(), mapRequestedDependencies(rawPlugin.getDependencies()), mapPluginExecutions(rawPlugin.getExecutions()) )); } } return plugins; } private Map mapPluginConfiguration(@Nullable JsonNode configuration) { if (configuration == null || configuration.isEmpty()) { return emptyMap(); } return MavenXmlMapper.readMapper().convertValue(configuration, new TypeReference>() { }); } private List mapPluginExecutions(@Nullable List rawExecutions) { List executions = emptyList(); if (rawExecutions != null) { executions = new ArrayList<>(rawExecutions.size()); for (Execution rawExecution : rawExecutions) { executions.add(new org.openrewrite.maven.tree.Plugin.Execution( rawExecution.getId(), rawExecution.getGoals(), rawExecution.getPhase(), rawExecution.getInherited(), rawExecution.getConfiguration() )); } } return executions; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy