Skip to content

Commit

Permalink
Dynamically apply Add-Opens directives from MANIFEST.MF (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Mar 31, 2022
1 parent 5943af8 commit f4e6886
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/InitializeMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.jenkinsci.maven.plugins.hpi;

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.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;

/**
* 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 {
setSurefireProperties();
}

private void setSurefireProperties() 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
return;
}

String argLine = buildArgLine(manifestEntry);
getLog().info("Setting jenkins.addOpens to " + argLine);
project.getProperties().setProperty("jenkins.addOpens", argLine);
}

@NonNull
private Artifact resolveJenkinsWar() throws MojoExecutionException {
DefaultArtifactCoordinate artifactCoordinate = new DefaultArtifactCoordinate();
artifactCoordinate.setGroupId("org.jenkins-ci.main");
artifactCoordinate.setArtifactId("jenkins-war");
artifactCoordinate.setVersion(findJenkinsVersion());
artifactCoordinate.setExtension("war");

try {
return artifactResolver
.resolveArtifact(session.getProjectBuildingRequest(), artifactCoordinate)
.getArtifact();
} catch (ArtifactResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: ", e);
}
}

@CheckForNull
private static String getManifestEntry(MavenArtifact artifact) throws MojoExecutionException {
File war = artifact.getFile();
try (JarFile jarFile = new JarFile(war)) {
Manifest manifest = jarFile.getManifest();
if (manifest == null) {
throw new MojoExecutionException("No manifest found in " + war);
}
return manifest.getMainAttributes().getValue("Add-Opens");
} catch (IOException e) {
throw new MojoExecutionException("Failed to read MANIFEST.MF from " + war, e);
}
}

@NonNull
private static String buildArgLine(String manifestEntry) {
List<String> arguments = new ArrayList<>();
for (String module : manifestEntry.split("\\s+")) {
if (!module.isEmpty()) {
arguments.add("--add-opens");
arguments.add(module + "=ALL-UNNAMED");
}
}
return String.join(" ", arguments);
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<id>default</id>
<phases>
<validate>org.jenkins-ci.tools:maven-hpi-plugin:validate,org.jenkins-ci.tools:maven-hpi-plugin:validate-hpi</validate>
<initialize>org.jenkins-ci.tools:maven-hpi-plugin:initialize</initialize>
<process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
<compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
<process-classes>org.kohsuke:access-modifier-checker:enforce</process-classes>
Expand Down

0 comments on commit f4e6886

Please sign in to comment.