Skip to content

Commit d7f176b

Browse files
committed
[MSHARED-1466] add Java-Version entry to default MANIFEST.MF
cherry-picked from 3.x branch #298
1 parent 09b213f commit d7f176b

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
<artifactId>maven-api-model</artifactId>
8282
<version>${mavenVersion}</version>
8383
</dependency>
84+
<dependency>
85+
<groupId>org.apache.maven</groupId>
86+
<artifactId>maven-api-xml</artifactId>
87+
<version>${mavenVersion}</version>
88+
</dependency>
8489

8590
<!--
8691
Plexus dependencies
@@ -100,7 +105,6 @@
100105
<artifactId>plexus-utils</artifactId>
101106
<version>${plexusUtilsVersion}</version>
102107
</dependency>
103-
104108
<!--
105109
Test dependencies
106110
-->
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.shared.archiver;
20+
21+
import java.util.Arrays;
22+
import java.util.Map;
23+
24+
import org.apache.maven.api.model.Model;
25+
import org.apache.maven.api.model.Plugin;
26+
import org.apache.maven.api.model.PluginContainer;
27+
import org.apache.maven.api.xml.XmlNode;
28+
29+
/**
30+
* Helper to detect info about build info in a Maven model, as configured in plugins.
31+
*
32+
* @since 4.0.0-beta-5
33+
*/
34+
public class BuildHelper {
35+
/**
36+
* Tries to determine the target Java release from the following sources (until one is found)
37+
* <ol>
38+
* <li>use {@code release} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
39+
* <li>use {@code maven.compiler.release<} property</li>
40+
* <li>use {@code target} configuration of {@code org.apache.maven.plugins:maven-compiler-plugin}</li>
41+
* <li>use {@code maven.compiler.target} property</li>
42+
* </ol>
43+
*
44+
* @param model not null
45+
* @return the Java release version configured in the model, or null if not configured
46+
*/
47+
public static String discoverJavaRelease(Model model) {
48+
Plugin compiler = getCompilerPlugin(model);
49+
50+
String jdk = getPluginParameter(model, compiler, "release", "maven.compiler.release");
51+
52+
if (jdk == null) {
53+
jdk = getPluginParameter(model, compiler, "target", "maven.compiler.target");
54+
}
55+
56+
return normalizeJavaVersion(jdk);
57+
}
58+
59+
/**
60+
* Normalize Java version, for versions 5 to 8 where there is a 1.x alias.
61+
*
62+
* @param jdk can be null
63+
* @return normalized version if an alias was used
64+
*/
65+
public static String normalizeJavaVersion(String jdk) {
66+
if (jdk != null
67+
&& jdk.length() == 3
68+
&& Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) {
69+
jdk = jdk.substring(2);
70+
}
71+
return jdk;
72+
}
73+
74+
public static Plugin getCompilerPlugin(Model model) {
75+
return getPlugin(model, "org.apache.maven.plugins:maven-compiler-plugin");
76+
}
77+
78+
/**
79+
* Get plugin from model based on coordinates {@code groupId:artifactId}.
80+
*
81+
* @param model not null
82+
* @param pluginGa {@code groupId:artifactId}
83+
* @return the plugin from build or pluginManagement, if available in project
84+
*/
85+
public static Plugin getPlugin(Model model, String pluginGa) {
86+
Plugin plugin = getPlugin(model.getBuild(), pluginGa);
87+
if (model.getBuild() != null && plugin == null) {
88+
plugin = getPlugin(model.getBuild().getPluginManagement(), pluginGa);
89+
}
90+
return plugin;
91+
}
92+
93+
/**
94+
* Get plugin parameter value if configured in current model.
95+
*
96+
* @param model not null
97+
* @param plugin can be null
98+
* @param parameter the parameter name when configured in plugin's configuration
99+
* @param defaultValueProperty the property name when default value is used for the plugin parameter
100+
* @return the value, or null if not configured at all, but using internal default from plugin.
101+
*/
102+
public static String getPluginParameter(Model model, Plugin plugin, String parameter, String defaultValueProperty) {
103+
String value = getPluginParameter(plugin, parameter);
104+
if (value == null) {
105+
value = model.getProperties().get(defaultValueProperty);
106+
}
107+
return value;
108+
}
109+
110+
private static Plugin getPlugin(PluginContainer container, String pluginGa) {
111+
if (container == null) {
112+
return null;
113+
}
114+
Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap();
115+
return pluginsAsMap.get(pluginGa);
116+
}
117+
118+
private static String getPluginParameter(Plugin plugin, String parameter) {
119+
if (plugin != null) {
120+
XmlNode pluginConf = plugin.getConfiguration();
121+
122+
if (pluginConf != null) {
123+
XmlNode target = pluginConf.getChild(parameter);
124+
125+
if (target != null) {
126+
return target.getValue();
127+
}
128+
}
129+
}
130+
return null;
131+
}
132+
}

src/main/java/org/apache/maven/shared/archiver/MavenArchiver.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ protected Manifest doGetManifest(
249249
Manifest m = new Manifest();
250250

251251
if (config.isAddDefaultEntries()) {
252-
handleDefaultEntries(m, entries);
252+
handleDefaultEntries(project, m, entries);
253253
}
254254

255255
if (config.isAddBuildEnvironmentEntries()) {
@@ -577,12 +577,17 @@ public void doCreateArchive(Session session, Project project, MavenArchiveConfig
577577
archiver.createArchive();
578578
}
579579

580-
private void handleDefaultEntries(Manifest m, Map<String, String> entries) throws ManifestException {
580+
private void handleDefaultEntries(Project project, Manifest m, Map<String, String> entries)
581+
throws ManifestException {
581582
String createdBy = this.createdBy;
582583
if (createdBy == null) {
583584
createdBy = createdBy(CREATED_BY, "org.apache.maven", "maven-archiver");
584585
}
585586
addManifestAttribute(m, entries, "Created-By", createdBy);
587+
String javaVersion = BuildHelper.discoverJavaRelease(project.getModel());
588+
if (javaVersion != null) {
589+
addManifestAttribute(m, entries, "Java-Version", javaVersion);
590+
}
586591
if (buildJdkSpecDefaultEntry) {
587592
addManifestAttribute(m, entries, "Build-Jdk-Spec", System.getProperty("java.specification.version"));
588593
}

0 commit comments

Comments
 (0)