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

Improve validation mojo #319

Merged
merged 6 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>version-number</artifactId>
<version>1.9</version>
<version>1.10-rc142.172b_43188179</version> <!-- TODO https://github.com/jenkinsci/lib-version-number/pull/41 -->
basil marked this conversation as resolved.
Show resolved Hide resolved
</dependency>
<dependency>
<groupId>org.kohsuke.stapler</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.jenkinsci.maven.plugins.hpi;

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.zip.ZipEntry;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.execution.MavenSession;
Expand All @@ -12,7 +19,9 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.shared.transfer.artifact.DefaultArtifactCoordinate;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolver;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;

/**
* Mojos that need to figure out the Jenkins version it's working with.
Expand Down Expand Up @@ -89,6 +98,48 @@ protected String findJenkinsVersion() throws MojoExecutionException {
throw new MojoExecutionException("Failed to determine Jenkins version this plugin depends on.");
}

protected JavaSpecificationVersion getMinimumJavaVersion() throws MojoExecutionException {
Artifact core = resolveJenkinsCore();
File jar = wrap(core).getFile();
try (JarFile jarFile = new JarFile(jar)) {
ZipEntry entry = jarFile.getEntry("jenkins/model/Jenkins.class");
if (entry == null) {
throw new MojoExecutionException("Failed to find Jenkins.class in " + jar);
}
try (InputStream is = jarFile.getInputStream(entry); DataInputStream dis = new DataInputStream(is)) {
int magic = dis.readInt();
if (magic != 0xcafebabe) {
throw new MojoExecutionException("Jenkins.class is not a valid class file in " + jar);
}
dis.readUnsignedShort(); // discard minor version
return JavaSpecificationVersion.fromClassVersion(dis.readUnsignedShort());
}
} catch (IOException e) {
throw new MojoExecutionException("Failed to read minimum Java version from " + jar, e);
}
}

private Artifact resolveJenkinsCore() throws MojoExecutionException {
DefaultArtifactCoordinate artifactCoordinate = new DefaultArtifactCoordinate();
if (jenkinsCoreId != null) {
String[] parts = jenkinsCoreId.split(":");
artifactCoordinate.setGroupId(parts[0]);
artifactCoordinate.setArtifactId(parts[1]);
} else {
artifactCoordinate.setGroupId("org.jenkins-ci.main");
artifactCoordinate.setArtifactId("jenkins-core");
}
artifactCoordinate.setVersion(findJenkinsVersion());

try {
return artifactResolver
.resolveArtifact(session.getProjectBuildingRequest(), artifactCoordinate)
.getArtifact();
} catch (ArtifactResolverException e) {
throw new MojoExecutionException("Couldn't download artifact: ", e);
basil marked this conversation as resolved.
Show resolved Hide resolved
}
}

protected MavenArtifact wrap(Artifact a) {
return new MavenArtifact(
a,
Expand Down
26 changes: 9 additions & 17 deletions src/main/java/org/jenkinsci/maven/plugins/hpi/ValidateMojo.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

Expand All @@ -13,24 +13,16 @@
*/
@Mojo(name = "validate", defaultPhase = LifecyclePhase.VALIDATE)
public class ValidateMojo extends AbstractJenkinsMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!isMustangOrAbove())
throw new MojoExecutionException("JDK6 or later is necessary to build a Jenkins plugin");

if (new VersionNumber(findJenkinsVersion()).compareTo(new VersionNumber("1.419.99"))<=0)
throw new MojoExecutionException("This version of maven-hpi-plugin requires Jenkins 1.420 or later");
}
@Override
public void execute() throws MojoExecutionException {
JavaSpecificationVersion javaVersion = getMinimumJavaVersion();
if (JavaSpecificationVersion.forCurrentJVM().isOlderThan(javaVersion)) {
throw new MojoExecutionException("Java " + javaVersion + " or later is necessary to build this plugin.");
}

/**
* Are we running on JDK6 or above?
*/
private static boolean isMustangOrAbove() {
try {
Class.forName("javax.annotation.processing.Processor");
return true;
} catch(ClassNotFoundException e) {
return false;
if (new VersionNumber(findJenkinsVersion()).compareTo(new VersionNumber("2.204")) < 0) {
throw new MojoExecutionException("This version of maven-hpi-plugin requires Jenkins 2.204 or later");
}
}
}