Skip to content

Commit

Permalink
Simplify build and consumer pom transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 7, 2023
1 parent a6360bd commit c5b4ace
Show file tree
Hide file tree
Showing 63 changed files with 712 additions and 3,876 deletions.
5 changes: 0 additions & 5 deletions maven-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,6 @@ under the License.
<artifactId>maven-model-builder</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-transform</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions maven-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ under the License.
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-builder</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model-transform</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-resolver-provider</artifactId>
Expand Down

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

import org.apache.maven.model.Model;
import org.apache.maven.model.building.DefaultModelBuilderFactory;
import org.apache.maven.model.building.ModelBuilder;
import org.apache.maven.model.building.TransformerContext;
import org.apache.maven.model.v4.MavenStaxReader;
import org.apache.maven.project.MavenProject;
Expand All @@ -40,32 +41,28 @@
import static org.mockito.Mockito.when;

class ConsumerPomArtifactTransformerTest {

ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();

@Test
void buildTransform() throws Exception {
void transform() 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/before.pom").toAbsolutePath();
Path afterPomFile =
Paths.get("src/test/resources/projects/transform/after.pom").toAbsolutePath();
Path temp = Files.createTempFile("consumer-", ".pom");

try (InputStream expected = Files.newInputStream(afterPomFile);
InputStream result = ConsumerPomArtifactTransformer.BuildPomArtifact.transform(
beforePomFile, new NoTransformerContext(), Collections.emptyMap())) {
XmlAssert.assertThat(result).and(expected).areIdentical();
}
}

@Test
void consumerTransform() throws Exception {
Path beforePomFile = Paths.get("src/test/resources/projects/transform/consumer-before.pom")
.toAbsolutePath();
Path afterPomFile = Paths.get("src/test/resources/projects/transform/consumer-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));
ConsumerPomArtifactTransformer.ConsumerPomArtifact.transform(model.getDelegate(), tempFile);
MavenProject project = new MavenProject(model);
ConsumerPomArtifactTransformer t = new ConsumerPomArtifactTransformer(modelBuilder);
t.createConsumerPomArtifact(project, tempFile, systemSessionMock)
.transform(beforePomFile, tempFile, model.getDelegate());
}
XmlAssert.assertThat(afterPomFile.toFile()).and(tempFile.toFile()).areIdentical();
}
Expand All @@ -79,8 +76,7 @@ void injectTransformedArtifactsWithoutPomShouldNotInjectAnyArtifacts() throws IO
when(systemSessionMock.getData()).thenReturn(sessionDataMock);
when(sessionDataMock.get(any())).thenReturn(new NoTransformerContext());

new ConsumerPomArtifactTransformer(Collections.emptyMap())
.injectTransformedArtifacts(emptyProject, systemSessionMock);
new ConsumerPomArtifactTransformer(modelBuilder).injectTransformedArtifacts(emptyProject, systemSessionMock);

assertThat(emptyProject.getAttachedArtifacts()).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.apache.maven.model;

import javax.inject.Inject;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.apache.maven.bridge.MavenRepositorySystem;
import org.apache.maven.execution.DefaultMavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingResult;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.Test;

@PlexusTest
public class ModelBuilderTest {

@Inject
ProjectBuilder projectBuilder;

@Inject
MavenRepositorySystem repositorySystem;

@Inject
DefaultRepositorySystemSessionFactory repositorySessionFactory;

@Test
void testModelBuilder() throws Exception {
MavenExecutionRequest mavenRequest = new DefaultMavenExecutionRequest();
mavenRequest.setLocalRepository(repositorySystem.createLocalRepository(new File("target/test-repo/")));

DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
request.setRepositorySession(repositorySessionFactory.newRepositorySession(mavenRequest));
List<ProjectBuildingResult> results = projectBuilder.build(
Collections.singletonList(new File("src/test/resources/projects/tree/pom.xml")), true, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void testReadInvalidPom() throws Exception {
File pomFile = new File("src/test/resources/projects/badPom.xml").getAbsoluteFile();
MavenSession mavenSession = createMavenSession(null);
ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
configuration.setRepositorySession(mavenSession.getRepositorySession());
org.apache.maven.project.ProjectBuilder projectBuilder =
getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
Expand All @@ -245,7 +245,8 @@ void testReadInvalidPom() throws Exception {
assertThat(pex.getResults().get(0).getProblems().size(), greaterThan(0));
assertThat(
pex.getResults(),
contains(projectBuildingResultWithProblemMessage("expected START_TAG or END_TAG not CHARACTERS")));
contains(projectBuildingResultWithProblemMessage(
"Received non-all-whitespace CHARACTERS or CDATA event in nextTag()")));
}

@Test
Expand Down
37 changes: 4 additions & 33 deletions maven-core/src/test/resources/projects/transform/after.pom
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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
http://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.
-->

<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">
<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>test</groupId>
<artifactId>test</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>



<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source> 1.5 </source>
<source>1.5</source>
<target xml:space="preserve"> 1.5 </target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
Expand All @@ -57,10 +29,9 @@ under the License.
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>default</id>
<id>default-active</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
Expand All @@ -80,4 +51,4 @@ under the License.
</properties>
</profile>
</profiles>
</project>
</project>
9 changes: 5 additions & 4 deletions maven-core/src/test/resources/projects/transform/before.pom
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.1.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>
xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 https://maven.apache.org/xsd/maven-4.1.0.xsd"
root="true">
<modelVersion>4.1.0</modelVersion>

<groupId>test</groupId>
<artifactId>test</artifactId>
Expand Down Expand Up @@ -63,7 +64,7 @@ under the License.

<profiles>
<profile>
<id>default</id>
<id>default-active</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
Expand Down

This file was deleted.

Loading

0 comments on commit c5b4ace

Please sign in to comment.