Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ private void validatePrerequisitesForNonMavenPluginProjects(List<MavenProject> p
* @return A {@link Set} of profile identifiers, never {@code null}.
*/
private Set<String> getAllProfiles(MavenSession session) {
// TODO: which version of super pom should we use here ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the same version that project uses? But: what happens if reactor has multiple versions (not same)? -- IMHO should fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've update this part of the computation to be coherent with the way superPoms are used during model building. The superPom of a given project is determined by the modelVersion of that project and not the modelVersion of its upper parent. What this means is that if you write a 4.1.0 project which inherit a 4.0.0 parent, the superPom will be 4.1.0.

final Model superPomModel = superPomProvider.getSuperModel("4.0.0").getDelegate();
final Set<MavenProject> projectsIncludingParents = new HashSet<>();
for (MavenProject project : session.getProjects()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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;
});
}
}
Original file line number Diff line number Diff line change
@@ -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 -->