Skip to content
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

Clean up TestRuntimeMojo #404

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 18 additions & 22 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.jenkinsci.maven.plugins.hpi;

import com.google.common.io.ByteStreams;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -45,6 +43,14 @@ public class TestRuntimeMojo extends AbstractJenkinsMojo {
@Parameter(property = "maven.test.skip", defaultValue = "false")
private boolean skip;

/**
* Directory where unpacked patch modules should be cached.
*
* @see #getInsaneHook
*/
@Parameter(defaultValue = "${project.build.directory}/patch-modules")
private File patchModuleDir;

@Override
public void execute() throws MojoExecutionException {
if (skipTests || skip) {
Expand All @@ -56,14 +62,9 @@ public void execute() throws MojoExecutionException {
}

private void setAddOpensProperty() throws MojoExecutionException {
if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(new JavaSpecificationVersion("9"))) {
// nothing to do prior to JEP 261
return;
}

String manifestEntry = getManifestEntry(wrap(resolveJenkinsWar()));
if (manifestEntry == null) {
// core older than 2.339, ignore
getLog().warn("Add-Opens missing from MANIFEST.MF");
return;
}

Expand Down Expand Up @@ -130,32 +131,27 @@ private void setInsaneHookProperty() throws MojoExecutionException {
return;
}

Path insaneHook = getInsaneHook(wrap(jth));
Path insaneHook = getInsaneHook(wrap(jth), patchModuleDir.toPath());

String argLine;
if (JavaSpecificationVersion.forCurrentJVM().isNewerThanOrEqualTo(new JavaSpecificationVersion("9"))) {
argLine = String.format("--patch-module=java.base=%s --add-exports=java.base/org.netbeans.insane.hook=ALL-UNNAMED", insaneHook);
} else {
argLine = String.format("-Xbootclasspath/p:%s", insaneHook);
}
String argLine = String.format("--patch-module=java.base=%s --add-exports=java.base/org.netbeans.insane.hook=ALL-UNNAMED", insaneHook);
getLog().info("Setting jenkins.insaneHook to " + argLine);
project.getProperties().setProperty("jenkins.insaneHook", argLine);
}

@NonNull
private static Path getInsaneHook(MavenArtifact artifact) throws MojoExecutionException {
private static Path getInsaneHook(MavenArtifact artifact, Path patchModuleDir) throws MojoExecutionException {
File jar = artifact.getFile();
try (JarFile jarFile = new JarFile(jar)) {
ZipEntry entry = jarFile.getEntry("netbeans/harness/modules/ext/org-netbeans-insane-hook.jar");
if (entry == null) {
throw new MojoExecutionException("Failed to find org-netbeans-insane-hook.jar in " + jar);
}
Path tempFile = Files.createTempFile("org-netbeans-insane-hook", ".jar");
tempFile.toFile().deleteOnExit();
try (InputStream is = jarFile.getInputStream(entry); OutputStream os = Files.newOutputStream(tempFile)) {
ByteStreams.copy(is, os);
Files.createDirectories(patchModuleDir);
Path insaneHook = patchModuleDir.resolve("org-netbeans-insane-hook.jar");
try (InputStream is = jarFile.getInputStream(entry)) {
Files.copy(is, insaneHook, StandardCopyOption.REPLACE_EXISTING);
}
return tempFile.toAbsolutePath();
return insaneHook.toAbsolutePath();
} catch (IOException e) {
throw new MojoExecutionException("Failed to read org-netbeans-insane-hook.jar from " + jar, e);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources>
<generate-test-sources>org.jenkins-ci.tools:maven-hpi-plugin:insert-test</generate-test-sources>
<test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile,org.jenkins-ci.tools:maven-hpi-plugin:test-hpl,org.jenkins-ci.tools:maven-hpi-plugin:resolve-test-dependencies</test-compile>
<test>org.jenkins-ci.tools:maven-hpi-plugin:test-runtime,org.apache.maven.plugins:maven-surefire-plugin:test</test>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing that maybe the order of execution is undefined when these are in the same lifecycle phase?

<process-test-classes>org.jenkins-ci.tools:maven-hpi-plugin:test-runtime</process-test-classes>
<test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
<package>org.jenkins-ci.tools:maven-hpi-plugin:hpi</package>
<install>org.apache.maven.plugins:maven-install-plugin:install</install>
<deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
Expand Down