diff --git a/src/it/ensure-java-level-marker-11-migration/invoker.properties b/src/it/ensure-java-level-marker-11-migration/invoker.properties
new file mode 100644
index 0000000000..ea9aa6b6b3
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11-migration/invoker.properties
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+invoker.goals=-ntp validate
diff --git a/src/it/ensure-java-level-marker-11-migration/pom.xml b/src/it/ensure-java-level-marker-11-migration/pom.xml
new file mode 100644
index 0000000000..f61baeab60
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11-migration/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ org.jenkins-ci.plugins
+ plugin
+ 4.77
+
+
+ org.jenkins-ci.tools.hpi.its
+ ensure-java-level-marker-11-migration
+ 1.0-SNAPSHOT
+ hpi
+ MyNewPlugin
+
+ 2.361.4
+ @project.version@
+ false
+
+
diff --git a/src/it/ensure-java-level-marker-11-migration/target/java-level/8 b/src/it/ensure-java-level-marker-11-migration/target/java-level/8
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/src/it/ensure-java-level-marker-11-migration/verify.groovy b/src/it/ensure-java-level-marker-11-migration/verify.groovy
new file mode 100644
index 0000000000..2f2575e7af
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11-migration/verify.groovy
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+assert new File(basedir, 'target/java-level').list().length == 1
+assert new File(basedir, 'target/java-level/11').exists()
+assert new File(basedir, 'build.log').getText('UTF-8').contains("Removing old marker file")
+
+return true
diff --git a/src/it/ensure-java-level-marker-11/invoker.properties b/src/it/ensure-java-level-marker-11/invoker.properties
new file mode 100644
index 0000000000..ea9aa6b6b3
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11/invoker.properties
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+invoker.goals=-ntp validate
diff --git a/src/it/ensure-java-level-marker-11/pom.xml b/src/it/ensure-java-level-marker-11/pom.xml
new file mode 100644
index 0000000000..b457eed0bf
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ org.jenkins-ci.plugins
+ plugin
+ 4.77
+
+
+ org.jenkins-ci.tools.hpi.its
+ ensure-java-level-marker-11
+ 1.0-SNAPSHOT
+ hpi
+ MyNewPlugin
+
+ 2.361.4
+ @project.version@
+ false
+
+
diff --git a/src/it/ensure-java-level-marker-11/verify.groovy b/src/it/ensure-java-level-marker-11/verify.groovy
new file mode 100644
index 0000000000..742429f744
--- /dev/null
+++ b/src/it/ensure-java-level-marker-11/verify.groovy
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+
+assert new File(basedir, 'target/java-level').list().length == 1
+assert new File(basedir, 'target/java-level/11').exists()
+
+return true
diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java
new file mode 100644
index 0000000000..bd452ef18d
--- /dev/null
+++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java
@@ -0,0 +1,73 @@
+package org.jenkinsci.maven.plugins.hpi;
+
+import hudson.util.VersionNumber;
+import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+
+/**
+ * Configure Maven for the desired version of Java.
+ *
+ * @author Basil Crow
+ */
+@Mojo(name = "initialize", defaultPhase = LifecyclePhase.INITIALIZE)
+public class InitializeMojo extends AbstractJenkinsMojo {
+
+ @Override
+ public void execute() throws MojoExecutionException {
+ setCompilerProperties();
+ }
+
+ private void setCompilerProperties() throws MojoExecutionException {
+ if (!project.getProperties().containsKey("maven.compiler.source")
+ && !project.getProperties().containsKey("maven.compiler.release")) {
+ // On an older plugin parent POM that predates the setting of these values as Maven properties.
+ return;
+ }
+
+ JavaSpecificationVersion javaVersion = getMinimumJavaVersion();
+ if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(new VersionNumber("9"))) {
+ // Should always be set already, but just in case...
+ setProperty("maven.compiler.source", javaVersion.toString());
+ setProperty("maven.compiler.target", javaVersion.toString());
+ setProperty("maven.compiler.testSource", javaVersion.toString());
+ setProperty("maven.compiler.testTarget", javaVersion.toString());
+ // Should never be set already, but just in case...
+ unsetProperty("maven.compiler.release");
+ unsetProperty("maven.compiler.testRelease");
+ } else {
+ /*
+ * When compiling with a Java 9+ compiler, we always rely on "release" in favor of "source" and "target",
+ * even when compiling to Java 8 bytecode.
+ */
+ setProperty("maven.compiler.release", Integer.toString(javaVersion.toReleaseVersion()));
+ setProperty("maven.compiler.testRelease", Integer.toString(javaVersion.toReleaseVersion()));
+
+ /*
+ * While it does not hurt to have these set to the Java specification version, it is also not needed when
+ * "release" is in use.
+ */
+ unsetProperty("maven.compiler.source");
+ unsetProperty("maven.compiler.target");
+ unsetProperty("maven.compiler.testSource");
+ unsetProperty("maven.compiler.testTarget");
+ }
+ }
+
+ private void setProperty(String key, String value) {
+ String currentValue = project.getProperties().getProperty(key);
+ if (currentValue == null || !currentValue.equals(value)) {
+ getLog().info("Setting " + key + " to " + value);
+ project.getProperties().setProperty(key, value);
+ }
+ }
+
+ private void unsetProperty(String key) {
+ String currentValue = project.getProperties().getProperty(key);
+ if (currentValue != null && !currentValue.isEmpty()) {
+ getLog().info("Unsetting " + key);
+ project.getProperties().remove(key);
+ }
+ }
+}
diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java
index 2ca13a8a02..639846707e 100644
--- a/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java
+++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java
@@ -2,12 +2,16 @@
import hudson.util.VersionNumber;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Properties;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.util.FileUtils;
/**
* Make sure that we are running in the right environment.
@@ -37,6 +41,7 @@ public void execute() throws MojoExecutionException {
if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(javaVersion)) {
throw new MojoExecutionException("Java " + javaVersion + " or later is necessary to build this plugin.");
}
+ writeProfileMarker(javaVersion);
if (new VersionNumber(findJenkinsVersion()).compareTo(new VersionNumber("2.361")) < 0) {
throw new MojoExecutionException("This version of maven-hpi-plugin requires Jenkins 2.361 or later");
@@ -94,6 +99,56 @@ public void execute() throws MojoExecutionException {
}
}
+ private void writeProfileMarker(JavaSpecificationVersion javaVersion) throws MojoExecutionException {
+ var path = Path.of(project.getBuild().getDirectory(), "java-level");
+ if (!Files.exists(path)) {
+ try {
+ Files.createDirectories(path);
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to create " + path, e);
+ }
+ }
+ var versionPath = path.resolve(Integer.toString(javaVersion.toReleaseVersion()));
+ boolean found = false;
+ try (var walk = Files.walk(path)) {
+ for (var it = walk.iterator(); it.hasNext(); ) {
+ var p = it.next();
+ if (p.equals(path)) {
+ continue;
+ }
+ if (!p.equals(versionPath)) {
+ getLog().info("Removing old marker file " + p);
+ delete(p);
+ } else {
+ found = true;
+ }
+ }
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to walk " + path, e);
+ }
+ if (!found) {
+ try {
+ Files.createFile(versionPath);
+ getLog().info("Created marker file " + versionPath);
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to create file " + versionPath, e);
+ }
+ }
+ }
+
+ private static void delete(Path p) throws MojoExecutionException {
+ try {
+ if (Files.isRegularFile(p)) {
+ Files.delete(p);
+ } else if (Files.isDirectory(p)) {
+ // In case someone manually created a subdirectory, delete it recursively
+ FileUtils.deleteDirectory(p.toFile());
+ }
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to delete " + p, e);
+ }
+ }
+
private void check(String tag, String value, String badStart, String goodStart, String reason) {
if (value.startsWith(badStart)) {
String goodValue = goodStart + value.substring(badStart.length());
diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java
index 1d88bf172b..541dc663b0 100644
--- a/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java
+++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/extensions/HpiLifecycleMappingProvider.java
@@ -26,6 +26,7 @@ static Map getBindings() {
"validate",
new LifecyclePhase(
"org.jenkins-ci.tools:maven-hpi-plugin:validate,org.jenkins-ci.tools:maven-hpi-plugin:validate-hpi"));
+ bindings.put("initialize", new LifecyclePhase("org.jenkins-ci.tools:maven-hpi-plugin:initialize"));
bindings.put(
"process-resources",
new LifecyclePhase("org.apache.maven.plugins:maven-resources-plugin:2.6:resources"));