Skip to content

Commit

Permalink
Remove build section from profiles in the consumer pom
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 13, 2024
1 parent 9442164 commit e2da371
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -273,16 +271,40 @@ 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);
}
return model;
}

private static List<Profile> prune(List<Profile> 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 extends ModelBase.Builder> T prune(T builder, ModelBase model) {
builder.properties(null).reporting(null);
if (model.getDistributionManagement() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 35 additions & 0 deletions maven-core/src/test/resources/projects/transform/jar/after.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.helpermethod</groupId>
<artifactId>zip-forge</artifactId>
<version>1.0.1</version>
<name>zip-forge</name>
<description>A tiny, formatter-friendly Java DSL for creating ZIP files.</description>
<url>https://github.com/helpermethod/zip-forge</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<scm>
<connection>scm:git:git@github.com:helpermethod/zip-forge.git</connection>
<developerConnection>scm:git:git@github.com:helpermethod/zip-forge.git</developerConnection>
<url>git@github.com:helpermethod/zip-forge.git</url>
</scm>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
74 changes: 74 additions & 0 deletions maven-core/src/test/resources/projects/transform/jar/before.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.helpermethod</groupId>
<artifactId>zip-forge</artifactId>
<version>1.0.1</version>
<name>zip-forge</name>
<description>A tiny, formatter-friendly Java DSL for creating ZIP files.</description>
<url>https://github.com/helpermethod/zip-forge</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<scm>
<connection>scm:git:git@github.com:helpermethod/zip-forge.git</connection>
<developerConnection>scm:git:git@github.com:helpermethod/zip-forge.git</developerConnection>
<url>git@github.com:helpermethod/zip-forge.git</url>
</scm>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>release</id>
<build>
<plugins>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

0 comments on commit e2da371

Please sign in to comment.