|
43 | 43 | import java.io.ByteArrayOutputStream; |
44 | 44 | import java.io.IOException; |
45 | 45 | import java.io.InputStream; |
| 46 | +import java.io.StringReader; |
46 | 47 | import java.nio.charset.StandardCharsets; |
47 | 48 | import java.nio.file.Files; |
48 | 49 | import java.nio.file.Path; |
|
68 | 69 | import java.util.function.BiFunction; |
69 | 70 | import java.util.function.Predicate; |
70 | 71 | import java.util.stream.Collectors; |
| 72 | +import org.apache.maven.model.DependencyManagement; |
71 | 73 | import org.apache.maven.model.Model; |
| 74 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
72 | 75 | import org.apache.maven.model.io.xpp3.MavenXpp3Writer; |
73 | 76 | import org.apache.maven.search.api.MAVEN; |
74 | 77 | import org.apache.maven.search.api.SearchBackend; |
@@ -963,31 +966,60 @@ public Result<CollectResult> projectDependencyTree(ReactorLocator reactorLocator |
963 | 966 | return Result.success(collectResult); |
964 | 967 | } |
965 | 968 |
|
966 | | - @Override |
967 | | - public Result<Model> effectiveModel(ResolutionRoot resolutionRoot) throws Exception { |
| 969 | + protected void tellModel(String title, Model model) throws IOException { |
| 970 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 971 | + MavenXpp3Writer mavenXpp3Writer = new MavenXpp3Writer(); |
| 972 | + mavenXpp3Writer.write(baos, model); |
| 973 | + output.doTell( |
| 974 | + title != null && !title.trim().isEmpty() ? title + "\n{}" : "{}", |
| 975 | + baos.toString(StandardCharsets.UTF_8)); |
| 976 | + } |
| 977 | + |
| 978 | + protected Result<Model> doEffectiveModel(boolean tell, ResolutionRoot resolutionRoot) throws Exception { |
968 | 979 | if (!resolutionRoot.isLoad()) { |
969 | 980 | throw new IllegalArgumentException("only loaded roots can be shown as effective model"); |
970 | 981 | } |
971 | 982 | ModelResponse response = toolboxResolver.readModel(resolutionRoot.getArtifact()); |
972 | | - |
973 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
974 | | - MavenXpp3Writer mavenXpp3Writer = new MavenXpp3Writer(); |
975 | | - mavenXpp3Writer.write(baos, response.getEffectiveModel()); |
976 | | - output.doTell("Effective model:\n{}", baos.toString(StandardCharsets.UTF_8)); |
977 | | - |
| 983 | + if (tell) { |
| 984 | + tellModel("Effective model:", response.getEffectiveModel()); |
| 985 | + } |
978 | 986 | return Result.success(response.getEffectiveModel()); |
979 | 987 | } |
980 | 988 |
|
| 989 | + @Override |
| 990 | + public Result<Model> effectiveModel(ResolutionRoot resolutionRoot) throws Exception { |
| 991 | + return doEffectiveModel(true, resolutionRoot); |
| 992 | + } |
| 993 | + |
981 | 994 | @Override |
982 | 995 | public Result<Model> effectiveModel(ReactorLocator reactorLocator) throws Exception { |
983 | 996 | requireNonNull(reactorLocator); |
984 | 997 | Result<Model> result = Result.success(reactorLocator.getCurrentProject().effectiveModel()); |
| 998 | + tellModel("Effective model:", result.getData().orElseThrow()); |
| 999 | + return result; |
| 1000 | + } |
985 | 1001 |
|
986 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
987 | | - MavenXpp3Writer mavenXpp3Writer = new MavenXpp3Writer(); |
988 | | - mavenXpp3Writer.write(baos, result.getData().orElseThrow()); |
989 | | - output.doTell("Effective model:\n{}", baos.toString(StandardCharsets.UTF_8)); |
990 | | - |
| 1002 | + @Override |
| 1003 | + public Result<Model> flattenBOM(Artifact artifact, ResolutionRoot resolutionRoot) throws Exception { |
| 1004 | + requireNonNull(artifact); |
| 1005 | + requireNonNull(resolutionRoot); |
| 1006 | + Result<Model> result = doEffectiveModel(false, resolutionRoot); |
| 1007 | + if (result.isSuccess()) { |
| 1008 | + Model resultModel = new MavenXpp3Reader() |
| 1009 | + .read(new StringReader(PomSuppliers.empty400( |
| 1010 | + artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()))); |
| 1011 | + resultModel.setDependencyManagement(new DependencyManagement()); |
| 1012 | + |
| 1013 | + Model bomModel = result.getData().orElseThrow(); |
| 1014 | + if (bomModel.getDependencyManagement() != null) { |
| 1015 | + for (org.apache.maven.model.Dependency dependency : |
| 1016 | + bomModel.getDependencyManagement().getDependencies()) { |
| 1017 | + resultModel.getDependencyManagement().addDependency(dependency); |
| 1018 | + } |
| 1019 | + } |
| 1020 | + tellModel("Flattened BOM:", resultModel); |
| 1021 | + return Result.success(resultModel); |
| 1022 | + } |
991 | 1023 | return result; |
992 | 1024 | } |
993 | 1025 |
|
|
0 commit comments