From d296927de27c0118e1ca05c47f85f4e12132f373 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 13 May 2024 14:04:39 +0200 Subject: [PATCH] [MNG-8119] Remove build section from profiles in the consumer pom (#1503) --- .../impl/DefaultConsumerPomBuilder.java | 34 +++++++-- .../ConsumerPomArtifactTransformerTest.java | 28 +++++++ .../projects/transform/jar/after.pom | 35 +++++++++ .../projects/transform/jar/before.pom | 74 +++++++++++++++++++ 4 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 maven-core/src/test/resources/projects/transform/jar/after.pom create mode 100644 maven-core/src/test/resources/projects/transform/jar/before.pom diff --git a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java index c86f395f3390..9579697fc021 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/internal/transformation/impl/DefaultConsumerPomBuilder.java @@ -258,9 +258,7 @@ static Model transform(Model model, MavenProject project) { .build(null), model); builder.packaging(POM_PACKAGING); - builder.profiles(model.getProfiles().stream() - .map(p -> prune(Profile.newBuilder(p, true), p).build()) - .collect(Collectors.toList())); + builder.profiles(prune(model.getProfiles())); model = builder.build(); String modelVersion = new MavenModelVersion().getModelVersion(model); @@ -273,9 +271,8 @@ static Model transform(Model model, MavenProject project) { .parent(null) .build(null), model); - builder.profiles(model.getProfiles().stream() - .map(p -> prune(Profile.newBuilder(p, true), p).build()) - .collect(Collectors.toList())); + builder.profiles(prune(model.getProfiles())); + model = builder.build(); String modelVersion = new MavenModelVersion().getModelVersion(model); model = model.withModelVersion(modelVersion); @@ -283,6 +280,31 @@ static Model transform(Model model, MavenProject project) { return model; } + private static List prune(List profiles) { + return profiles.stream() + .map(p -> { + Profile.Builder builder = Profile.newBuilder(p, true); + prune((ModelBase.Builder) builder, p); + return builder.build(null).build(); + }) + .filter(p -> !isEmpty(p)) + .collect(Collectors.toList()); + } + + private static boolean isEmpty(Profile profile) { + return profile.getActivation() == null + && profile.getBuild() == null + && profile.getDependencies().isEmpty() + && (profile.getDependencyManagement() == null + || profile.getDependencyManagement().getDependencies().isEmpty()) + && profile.getDistributionManagement() == null + && profile.getModules().isEmpty() + && profile.getProperties().isEmpty() + && profile.getRepositories().isEmpty() + && profile.getPluginRepositories().isEmpty() + && profile.getReporting() == null; + } + private static T prune(T builder, ModelBase model) { builder.properties(null).reporting(null); if (model.getDistributionManagement() != null diff --git a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomArtifactTransformerTest.java b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomArtifactTransformerTest.java index 970881aa4d78..b8f621f1dcb8 100644 --- a/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomArtifactTransformerTest.java +++ b/maven-core/src/test/java/org/apache/maven/internal/transformation/impl/ConsumerPomArtifactTransformerTest.java @@ -68,6 +68,34 @@ void transform() throws Exception { XmlAssert.assertThat(afterPomFile.toFile()).and(tempFile.toFile()).areIdentical(); } + @Test + void transformJarConsumerPom() throws Exception { + RepositorySystemSession systemSessionMock = Mockito.mock(RepositorySystemSession.class); + SessionData sessionDataMock = Mockito.mock(SessionData.class); + when(systemSessionMock.getData()).thenReturn(sessionDataMock); + when(sessionDataMock.get(any())).thenReturn(new NoTransformerContext()); + + Path beforePomFile = Paths.get("src/test/resources/projects/transform/jar/before.pom") + .toAbsolutePath(); + Path afterPomFile = + Paths.get("src/test/resources/projects/transform/jar/after.pom").toAbsolutePath(); + Path tempFile = Files.createTempFile("", ".pom"); + Files.delete(tempFile); + try (InputStream expected = Files.newInputStream(beforePomFile)) { + Model model = new Model(new MavenStaxReader().read(expected)); + MavenProject project = new MavenProject(model); + project.setOriginalModel(model); + DefaultConsumerPomArtifactTransformer t = new DefaultConsumerPomArtifactTransformer((s, p, f) -> { + try (InputStream is = Files.newInputStream(f)) { + return DefaultConsumerPomBuilder.transform(new MavenStaxReader().read(is), project); + } + }); + + t.transform(project, systemSessionMock, beforePomFile, tempFile); + } + XmlAssert.assertThat(afterPomFile.toFile()).and(tempFile.toFile()).areIdentical(); + } + @Test void injectTransformedArtifactsWithoutPomShouldNotInjectAnyArtifacts() throws IOException { MavenProject emptyProject = new MavenProject(); diff --git a/maven-core/src/test/resources/projects/transform/jar/after.pom b/maven-core/src/test/resources/projects/transform/jar/after.pom new file mode 100644 index 000000000000..90810bb2ef1a --- /dev/null +++ b/maven-core/src/test/resources/projects/transform/jar/after.pom @@ -0,0 +1,35 @@ + + + 4.0.0 + io.github.helpermethod + zip-forge + 1.0.1 + zip-forge + A tiny, formatter-friendly Java DSL for creating ZIP files. + https://github.com/helpermethod/zip-forge + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + scm:git:git@github.com:helpermethod/zip-forge.git + scm:git:git@github.com:helpermethod/zip-forge.git + git@github.com:helpermethod/zip-forge.git + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + org.assertj + assertj-core + 3.25.3 + test + + + \ No newline at end of file diff --git a/maven-core/src/test/resources/projects/transform/jar/before.pom b/maven-core/src/test/resources/projects/transform/jar/before.pom new file mode 100644 index 000000000000..cfe2e04cc13f --- /dev/null +++ b/maven-core/src/test/resources/projects/transform/jar/before.pom @@ -0,0 +1,74 @@ + + + 4.0.0 + io.github.helpermethod + zip-forge + 1.0.1 + zip-forge + A tiny, formatter-friendly Java DSL for creating ZIP files. + https://github.com/helpermethod/zip-forge + + + The Apache License, Version 2.0 + https://www.apache.org/licenses/LICENSE-2.0.txt + + + + scm:git:git@github.com:helpermethod/zip-forge.git + scm:git:git@github.com:helpermethod/zip-forge.git + git@github.com:helpermethod/zip-forge.git + + + + org.junit.jupiter + junit-jupiter + 5.10.2 + test + + + org.assertj + assertj-core + 3.25.3 + test + + + + + release + + + + maven-javadoc-plugin + 3.6.3 + + + attach-javadocs + + jar + + + true + + + + + + maven-source-plugin + 3.3.1 + + + attach-sources + + jar + + + true + + + + + + + + + \ No newline at end of file