-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Allow a Maven plugin to require a Java version #11479
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
Merged
slawekjaranowski
merged 1 commit into
apache:maven-3.9.x
from
slawekjaranowski:java-prerequisite
Nov 25, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
maven-core/src/main/java/org/apache/maven/plugin/MavenPluginPrerequisitesChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * 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.plugin; | ||
|
|
||
| import java.util.function.Consumer; | ||
|
|
||
| import org.apache.maven.plugin.descriptor.PluginDescriptor; | ||
|
|
||
| /** | ||
| * Service responsible for checking if plugin's prerequisites are met. | ||
| * | ||
| * @since 3.9.12 | ||
| */ | ||
| @FunctionalInterface | ||
| public interface MavenPluginPrerequisitesChecker extends Consumer<PluginDescriptor> { | ||
| /** | ||
| * | ||
| * @param pluginDescriptor | ||
| * @throws IllegalStateException in case the checked prerequisites are not met | ||
| */ | ||
| @Override | ||
| void accept(PluginDescriptor pluginDescriptor); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...re/src/main/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.plugin.internal; | ||
|
|
||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; | ||
| import org.apache.maven.plugin.descriptor.PluginDescriptor; | ||
| import org.eclipse.aether.util.version.GenericVersionScheme; | ||
| import org.eclipse.aether.version.InvalidVersionSpecificationException; | ||
| import org.eclipse.aether.version.Version; | ||
| import org.eclipse.aether.version.VersionConstraint; | ||
| import org.eclipse.aether.version.VersionScheme; | ||
|
|
||
| @Named | ||
| @Singleton | ||
| public class MavenPluginJavaPrerequisiteChecker implements MavenPluginPrerequisitesChecker { | ||
| private final VersionScheme versionScheme = new GenericVersionScheme(); | ||
|
|
||
| @Override | ||
| public void accept(PluginDescriptor pluginDescriptor) { | ||
| String requiredJavaVersion = pluginDescriptor.getRequiredJavaVersion(); | ||
| if (requiredJavaVersion != null && !requiredJavaVersion.isEmpty()) { | ||
| String currentJavaVersion = System.getProperty("java.version"); | ||
| if (!matchesVersion(requiredJavaVersion, currentJavaVersion)) { | ||
| throw new IllegalStateException("Required Java version " + requiredJavaVersion | ||
| + " is not met by current version: " + currentJavaVersion); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| boolean matchesVersion(String requiredVersion, String currentVersion) { | ||
| VersionConstraint constraint; | ||
| try { | ||
| constraint = versionScheme.parseVersionConstraint(requiredVersion.equals("8") ? "1.8" : requiredVersion); | ||
| } catch (InvalidVersionSpecificationException e) { | ||
| throw new IllegalArgumentException("Invalid 'requiredJavaVersion' given in plugin descriptor", e); | ||
| } | ||
| Version current; | ||
| try { | ||
| current = versionScheme.parseVersion(currentVersion); | ||
| } catch (InvalidVersionSpecificationException e) { | ||
| throw new IllegalStateException("Could not parse current Java version", e); | ||
| } | ||
| if (constraint.getRange() == null) { | ||
| return constraint.getVersion().compareTo(current) <= 0; | ||
| } | ||
| return constraint.containsVersion(current); | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
...e/src/main/java/org/apache/maven/plugin/internal/MavenPluginMavenPrerequisiteChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.plugin.internal; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Named; | ||
| import javax.inject.Singleton; | ||
|
|
||
| import org.apache.maven.plugin.MavenPluginPrerequisitesChecker; | ||
| import org.apache.maven.plugin.descriptor.PluginDescriptor; | ||
| import org.apache.maven.rtinfo.RuntimeInformation; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| @Named | ||
| @Singleton | ||
| public class MavenPluginMavenPrerequisiteChecker implements MavenPluginPrerequisitesChecker { | ||
| private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
| private final RuntimeInformation runtimeInformation; | ||
|
|
||
| @Inject | ||
| public MavenPluginMavenPrerequisiteChecker(RuntimeInformation runtimeInformation) { | ||
| super(); | ||
| this.runtimeInformation = runtimeInformation; | ||
| } | ||
|
|
||
| @Override | ||
| public void accept(PluginDescriptor pluginDescriptor) { | ||
| String requiredMavenVersion = pluginDescriptor.getRequiredMavenVersion(); | ||
|
|
||
| boolean isBlankVersion = | ||
| requiredMavenVersion == null || requiredMavenVersion.trim().isEmpty(); | ||
|
|
||
| if (!isBlankVersion) { | ||
| boolean isRequirementMet = false; | ||
| try { | ||
| isRequirementMet = runtimeInformation.isMavenVersion(requiredMavenVersion); | ||
| } catch (IllegalArgumentException e) { | ||
| logger.warn( | ||
| "Could not verify plugin's Maven prerequisite as an invalid version is given in " | ||
| + requiredMavenVersion, | ||
| e); | ||
| return; | ||
| } | ||
| if (!isRequirementMet) { | ||
| throw new IllegalStateException("Required Maven version " + requiredMavenVersion | ||
| + " is not met by current version " + runtimeInformation.getMavenVersion()); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...rc/test/java/org/apache/maven/plugin/internal/MavenPluginJavaPrerequisiteCheckerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * 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.plugin.internal; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertThrows; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class MavenPluginJavaPrerequisiteCheckerTest { | ||
|
|
||
| @Test | ||
| public void testMatchesVersion() { | ||
| MavenPluginJavaPrerequisiteChecker checker = new MavenPluginJavaPrerequisiteChecker(); | ||
| assertTrue(checker.matchesVersion("8", "1.8")); | ||
| assertTrue(checker.matchesVersion("8", "9.0.1+11")); | ||
| assertTrue(checker.matchesVersion("1.0", "1.8")); | ||
| assertTrue(checker.matchesVersion("1.8", "9.0.1+11")); | ||
| assertFalse(checker.matchesVersion("[1.0,2],[3,4]", "2.1")); | ||
| assertTrue(checker.matchesVersion("[1.0,2],[3,4]", "3.1")); | ||
| assertThrows(IllegalArgumentException.class, () -> checker.matchesVersion("(1.0,0)", "11")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.