Skip to content

Commit

Permalink
Clean up TestRuntimeMojo (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Dec 1, 2022
1 parent 2a3bf4b commit 90c7124
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
40 changes: 18 additions & 22 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/TestRuntimeMojo.java
@@ -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
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>
<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

0 comments on commit 90c7124

Please sign in to comment.