Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jenkinsci.maven.plugins.hpi;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.util.VersionNumber;
import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
Expand Down Expand Up @@ -140,6 +142,37 @@ private Artifact resolveJenkinsCore() throws MojoExecutionException {
}
}

@CheckForNull
protected String getAddOpens() throws MojoExecutionException {
Artifact artifact = resolveJenkinsWar();
File war = wrap(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);
}
}

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);
}
}

protected MavenArtifact wrap(Artifact a) {
return new MavenArtifact(
a,
Expand Down
44 changes: 44 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,44 @@
package org.jenkinsci.maven.plugins.hpi;

import io.jenkins.lib.versionnumber.JavaSpecificationVersion;
import java.util.ArrayList;
import java.util.List;
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 {
String addOpens = getAddOpens();
if (addOpens != null
&& JavaSpecificationVersion.forCurrentJVM().isNewerThanOrEqualTo(new JavaSpecificationVersion("9"))) {
String argLine = project.getProperties().getProperty("argLine");
if (argLine != null) {
argLine += " " + buildArgLine(addOpens);
} else {
argLine = buildArgLine(addOpens);
}
getLog().info("Setting argLine to " + argLine);
project.getProperties().setProperty("argLine", argLine);
}
}

private static String buildArgLine(String addOpens) {
List<String> arguments = new ArrayList<>();
for (String module : addOpens.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