[MNG-7893] Allow versioning the superpom according to the model version (#1253)

This commit is contained in:
Guillaume Nodet 2023-09-26 07:17:52 +02:00 committed by GitHub
parent 876e8d2b7c
commit 10fa5c713a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 138 additions and 18 deletions

View File

@ -20,6 +20,7 @@
import org.apache.maven.api.Service;
import org.apache.maven.api.annotations.Experimental;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.model.Model;
/**
@ -33,7 +34,8 @@ public interface SuperPomProvider extends Service {
*
* @param version The model version to retrieve the super POM for (e.g. "4.0.0"), must not be {@code null}.
* @return The super POM, never {@code null}.
* @throws IllegalStateException if the super POM could not be retrieved
* @throws SuperPomProviderException if the super POM could not be retrieved
*/
Model getSuperPom(String version);
@Nonnull
Model getSuperPom(@Nonnull String version);
}

View File

@ -0,0 +1,43 @@
/*
* 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.api.services;
/**
* Exceptions thrown by the {@link SuperPomProvider} service.
*
* @since 4.0.0
*/
public class SuperPomProviderException extends MavenException {
public SuperPomProviderException() {
super();
}
public SuperPomProviderException(String message) {
super(message);
}
public SuperPomProviderException(String message, Throwable cause) {
super(message, cause);
}
public SuperPomProviderException(Throwable cause) {
super(cause);
}
}

View File

@ -29,6 +29,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -497,9 +498,12 @@ private void validatePrerequisitesForNonMavenPluginProjects(List<MavenProject> p
* @return A {@link Set} of profile identifiers, never {@code null}.
*/
private Set<String> getAllProfiles(MavenSession session) {
final Model superPomModel = superPomProvider.getSuperModel("4.0.0").getDelegate();
final Map<String, Model> superPomModels = new HashMap<>();
final Set<MavenProject> projectsIncludingParents = new HashSet<>();
for (MavenProject project : session.getProjects()) {
superPomModels.computeIfAbsent(
project.getModelVersion(),
v -> superPomProvider.getSuperModel(v).getDelegate());
boolean isAdded = projectsIncludingParents.add(project);
MavenProject parent = project.getParent();
while (isAdded && parent != null) {
@ -513,8 +517,9 @@ private Set<String> getAllProfiles(MavenSession session) {
.map(Profile::getId);
final Stream<String> settingsProfiles =
session.getSettings().getProfiles().stream().map(org.apache.maven.settings.Profile::getId);
final Stream<String> superPomProfiles =
superPomModel.getProfiles().stream().map(Profile::getId);
final Stream<String> superPomProfiles = superPomModels.values().stream()
.flatMap(p -> p.getProfiles().stream())
.map(Profile::getId);
return Stream.of(projectProfiles, settingsProfiles, superPomProfiles)
.flatMap(Function.identity())

View File

@ -24,6 +24,7 @@
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.SuperPomProvider;
import org.apache.maven.api.services.SuperPomProviderException;
@Named
@Singleton
@ -38,6 +39,10 @@ public DefaultSuperPomProvider(org.apache.maven.model.superpom.SuperPomProvider
@Override
public Model getSuperPom(String version) {
return provider.getSuperModel(version).getDelegate();
try {
return provider.getSuperModel(version).getDelegate();
} catch (IllegalStateException e) {
throw new SuperPomProviderException("Could not retrieve super pom " + version, e);
}
}
}

View File

@ -746,7 +746,8 @@ private Model readEffectiveModel(
problems.setRootModel(inputModel);
ModelData resultData = new ModelData(request.getModelSource(), inputModel);
ModelData superData = new ModelData(null, getSuperModel());
String superModelVersion = inputModel.getModelVersion() != null ? inputModel.getModelVersion() : "4.0.0";
ModelData superData = new ModelData(null, getSuperModel(superModelVersion));
// profile activation
DefaultProfileActivationContext profileActivationContext = getProfileActivationContext(request);
@ -1605,8 +1606,8 @@ private ModelData readParentExternally(
modelSource, parentModel, parent.getGroupId(), parent.getArtifactId(), parent.getVersion());
}
private Model getSuperModel() {
return superPomProvider.getSuperModel("4.0.0");
private Model getSuperModel(String modelVersion) {
return superPomProvider.getSuperModel(modelVersion);
}
private void importDependencyManagement(

View File

@ -26,6 +26,8 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.model.Model;
@ -44,7 +46,7 @@ public class DefaultSuperPomProvider implements SuperPomProvider {
/**
* The cached super POM, lazily created.
*/
private Model superModel;
private static final Map<String, Model> SUPER_MODELS = new ConcurrentHashMap<>();
@Inject
public DefaultSuperPomProvider(ModelProcessor modelProcessor) {
@ -53,8 +55,8 @@ public DefaultSuperPomProvider(ModelProcessor modelProcessor) {
@Override
public Model getSuperModel(String version) {
if (superModel == null) {
String resource = "/org/apache/maven/model/pom-" + version + ".xml";
return SUPER_MODELS.computeIfAbsent(Objects.requireNonNull(version), v -> {
String resource = "/org/apache/maven/model/pom-" + v + ".xml";
InputStream is = getClass().getResourceAsStream(resource);
@ -65,23 +67,21 @@ public Model getSuperModel(String version) {
try {
Map<String, Object> options = new HashMap<>(2);
options.put("xml:4.0.0", "xml:4.0.0");
options.put("xml:" + version, "xml:" + version);
String modelId = "org.apache.maven:maven-model-builder:"
String modelId = "org.apache.maven:maven-model-builder:" + version + "-"
+ this.getClass().getPackage().getImplementationVersion() + ":super-pom";
InputSource inputSource = new InputSource(
modelId, getClass().getResource(resource).toExternalForm());
options.put(ModelProcessor.INPUT_SOURCE, new org.apache.maven.model.InputSource(inputSource));
superModel = modelProcessor.read(is, options);
return modelProcessor.read(is, options);
} catch (IOException e) {
throw new IllegalStateException(
"The super POM " + resource + " is damaged"
+ ", please verify the integrity of your Maven installation",
e);
}
}
return superModel;
});
}
}

View File

@ -0,0 +1,64 @@
<?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.
-->
<!-- START SNIPPET: superpom -->
<project>
<modelVersion>4.0.0</modelVersion>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
<testResource>
<directory>${project.basedir}/src/test/resources-filtered</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>
</project>
<!-- END SNIPPET: superpom -->