Skip to content

Commit 5708e39

Browse files
authored
[MSHARED-1466] add Java-Version entry to default MANIFEST.MF (#298)
1 parent 136f03f commit 5708e39

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
<artifactId>plexus-interpolation</artifactId>
8484
<version>1.28</version>
8585
</dependency>
86+
<dependency>
87+
<groupId>org.codehaus.plexus</groupId>
88+
<artifactId>plexus-xml</artifactId>
89+
<version>3.0.2</version>
90+
</dependency>
8691
<!--
8792
Test dependencies
8893
-->
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.archiver;
20+
21+
import java.util.Arrays;
22+
import java.util.Map;
23+
24+
import org.apache.maven.model.Plugin;
25+
import org.apache.maven.model.PluginContainer;
26+
import org.apache.maven.project.MavenProject;
27+
import org.codehaus.plexus.util.xml.Xpp3Dom;
28+
29+
/**
30+
* Helper to detect info about build info in a MavenProject, as configured in plugins.
31+
*
32+
* @since 3.6.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 project not null
45+
* @return the Java release version configured in the project, or null if not configured
46+
*/
47+
public static String discoverJavaRelease(MavenProject project) {
48+
Plugin compiler = getCompilerPlugin(project);
49+
50+
String jdk = getPluginParameter(project, compiler, "release", "maven.compiler.release");
51+
52+
if (jdk == null) {
53+
jdk = getPluginParameter(project, 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 && jdk.length() == 3 && Arrays.asList("1.5", "1.6", "1.7", "1.8").contains(jdk)) {
67+
jdk = jdk.substring(2);
68+
}
69+
return jdk;
70+
}
71+
72+
public static Plugin getCompilerPlugin(MavenProject project) {
73+
return getPlugin(project, "org.apache.maven.plugins:maven-compiler-plugin");
74+
}
75+
76+
/**
77+
* Get plugin from project based on coordinates {@code groupId:artifactId}.
78+
*
79+
* @param project not null
80+
* @param pluginGa {@code groupId:artifactId}
81+
* @return the plugin from build or pluginManagement, if available in project
82+
*/
83+
public static Plugin getPlugin(MavenProject project, String pluginGa) {
84+
Plugin plugin = getPlugin(project.getBuild(), pluginGa);
85+
if (plugin == null) {
86+
plugin = getPlugin(project.getPluginManagement(), pluginGa);
87+
}
88+
return plugin;
89+
}
90+
91+
/**
92+
* Get plugin parameter value if configured in current project.
93+
*
94+
* @param project not null
95+
* @param plugin can be null
96+
* @param parameter the parameter name when configured in plugin's configuration
97+
* @param defaultValueProperty the property name when default value is used for the plugin parameter
98+
* @return the value, or null if not configured at all, but using internal default from plugin.
99+
*/
100+
public static String getPluginParameter(
101+
MavenProject project, Plugin plugin, String parameter, String defaultValueProperty) {
102+
String value = getPluginParameter(plugin, parameter);
103+
if (value == null) {
104+
value = project.getProperties().getProperty(defaultValueProperty);
105+
}
106+
return value;
107+
}
108+
109+
private static Plugin getPlugin(PluginContainer container, String pluginGa) {
110+
if (container == null) {
111+
return null;
112+
}
113+
Map<String, Plugin> pluginsAsMap = container.getPluginsAsMap();
114+
return pluginsAsMap.get(pluginGa);
115+
}
116+
117+
private static String getPluginParameter(Plugin plugin, String parameter) {
118+
if (plugin != null) {
119+
Xpp3Dom pluginConf = (Xpp3Dom) plugin.getConfiguration();
120+
121+
if (pluginConf != null) {
122+
Xpp3Dom target = pluginConf.getChild(parameter);
123+
124+
if (target != null) {
125+
return target.getValue();
126+
}
127+
}
128+
}
129+
return null;
130+
}
131+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ protected Manifest getManifest(
252252
Manifest m = new Manifest();
253253

254254
if (config.isAddDefaultEntries()) {
255-
handleDefaultEntries(m, entries);
255+
handleDefaultEntries(project, m, entries);
256256
}
257257

258258
if (config.isAddBuildEnvironmentEntries()) {
@@ -607,12 +607,17 @@ public void createArchive(
607607
archiver.createArchive();
608608
}
609609

610-
private void handleDefaultEntries(Manifest m, Map<String, String> entries) throws ManifestException {
610+
private void handleDefaultEntries(MavenProject project, Manifest m, Map<String, String> entries)
611+
throws ManifestException {
611612
String createdBy = this.createdBy;
612613
if (createdBy == null) {
613614
createdBy = createdBy(CREATED_BY, "org.apache.maven", "maven-archiver");
614615
}
615616
addManifestAttribute(m, entries, "Created-By", createdBy);
617+
String javaVersion = BuildHelper.discoverJavaRelease(project);
618+
if (javaVersion != null) {
619+
addManifestAttribute(m, entries, "Java-Version", javaVersion);
620+
}
616621
if (buildJdkSpecDefaultEntry) {
617622
addManifestAttribute(m, entries, "Build-Jdk-Spec", System.getProperty("java.specification.version"));
618623
}

0 commit comments

Comments
 (0)