You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2025/08/24 20:17:57 UTC
(maven-archiver) 01/01: [MSHARED-1466] add Java-Version to MANIFEST.MF
This is an automated email from the ASF dual-hosted git repository.
hboutemy pushed a commit to branch MSHARED-1466
in repository https://gitbox.apache.org/repos/asf/maven-archiver.git
commit 32a3cf42532cbcfc9b01a395e7f5c623d9715387
Author: Hervé Boutemy <hb...@apache.org>
AuthorDate: Sun Aug 24 22:16:48 2025 +0200
[MSHARED-1466] add Java-Version to MANIFEST.MF
fixes #259
---
pom.xml | 5 +
.../org/apache/maven/archiver/BuildHelper.java | 113 +++++++++++++++++++++
.../org/apache/maven/archiver/MavenArchiver.java | 9 +-
3 files changed, 125 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 90ad6aa..418de9f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -83,6 +83,11 @@
<artifactId>plexus-interpolation</artifactId>
<version>1.28</version>
</dependency>
+ <dependency>
+ <groupId>org.codehaus.plexus</groupId>
+ <artifactId>plexus-xml</artifactId>
+ <version>3.0.2</version>
+ </dependency>
<!--
Test dependencies
-->
diff --git a/src/main/java/org/apache/maven/archiver/BuildHelper.java b/src/main/java/org/apache/maven/archiver/BuildHelper.java
new file mode 100644
index 0000000..7cf56cc
--- /dev/null
+++ b/src/main/java/org/apache/maven/archiver/BuildHelper.java
@@ -0,0 +1,113 @@
+/*
+ * 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.archiver;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginContainer;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+
+/**
+ * Helper to detect info about build info in a MavenProject, as configured in plugins.
+ *
+ * @since 3.6.5
+ */
+public class BuildHelper {
+ /**
+ * Tries to determine the target Java release from the following sources (until one is found)
+ * <ol>
+ * <li>use {@code release} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
+ * <li>use {@code maven.compiler.release<} property</li>
+ * <li>use {@code target} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
+ * <li>use {@code maven.compiler.target} property</li>
+ * </ol>
+ *
+ * @param project not null
+ * @return the Java release version configured in the project, or null if not configured
+ */
+ public static String discoverJavaRelease(MavenProject project) {
+ Plugin compiler = getCompilerPlugin(project);
+
+ String jdk = getPluginParameter(project, compiler, "release", "maven.compiler.release");
+
+ if (jdk == null) {
+ jdk = getPluginParameter(project, compiler, "target", "maven.compiler.target");
+ }
+
+ if (jdk != null) {
+ jdk = normalizeJavaVersion(jdk);
+ }
+
+ return jdk;
+ }
+
+ public static String normalizeJavaVersion(String jdk) {
+ if (jdk.length() == 3 && Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) {
+ jdk = jdk.substring(2);
+ }
+ return jdk;
+ }
+
+ public static Plugin getCompilerPlugin(MavenProject project) {
+ return getPlugin(project, "org.apache.maven.plugins:maven-compiler-plugin");
+ }
+
+ public static Plugin getPlugin(MavenProject project, String pluginGa) {
+ Plugin plugin = getPlugin(project.getBuild(), pluginGa);
+ if (plugin == null) {
+ plugin = getPlugin(project.getPluginManagement(), pluginGa);
+ }
+ return plugin;
+ }
+
+ public static String getPluginParameter(
+ MavenProject project, Plugin plugin, String parameter, String defaultValueProperty) {
+ String value = getPluginParameter(plugin, parameter);
+ if (value == null) {
+ value = project.getProperties().getProperty(defaultValueProperty);
+ }
+ return value;
+ }
+
+ private static Plugin getPlugin(PluginContainer container, String pluginGa) {
+ if (container == null) {
+ return null;
+ }
+ Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap();
+ return pluginsAsMap.get(pluginGa);
+ }
+
+ private static String getPluginParameter(Plugin plugin, String parameter) {
+ if (plugin != null) {
+ Xpp3Dom pluginConf = (Xpp3Dom) plugin.getConfiguration();
+
+ if (pluginConf != null) {
+ Xpp3Dom target = pluginConf.getChild(parameter);
+
+ if (target != null) {
+ return target.getValue();
+ }
+ }
+ }
+ return null;
+ }
+}
diff --git a/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/src/main/java/org/apache/maven/archiver/MavenArchiver.java
index be0b6a5..cbe0183 100644
--- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java
+++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java
@@ -252,7 +252,7 @@ public class MavenArchiver {
Manifest m = new Manifest();
if (config.isAddDefaultEntries()) {
- handleDefaultEntries(m, entries);
+ handleDefaultEntries(project, m, entries);
}
if (config.isAddBuildEnvironmentEntries()) {
@@ -607,12 +607,17 @@ public class MavenArchiver {
archiver.createArchive();
}
- private void handleDefaultEntries(Manifest m, Map<String, String> entries) throws ManifestException {
+ private void handleDefaultEntries(MavenProject project, Manifest m, Map<String, String> entries)
+ throws ManifestException {
String createdBy = this.createdBy;
if (createdBy == null) {
createdBy = createdBy(CREATED_BY, "org.apache.maven", "maven-archiver");
}
addManifestAttribute(m, entries, "Created-By", createdBy);
+ String javaVersion = BuildHelper.discoverJavaRelease(project);
+ if (javaVersion != null) {
+ addManifestAttribute(m, entries, "Java-Version", javaVersion);
+ }
if (buildJdkSpecDefaultEntry) {
addManifestAttribute(m, entries, "Build-Jdk-Spec", System.getProperty("java.specification.version"));
}