Skip to content

Commit

Permalink
Add additional plugin manifest entries (#436)
Browse files Browse the repository at this point in the history
Co-authored-by: Basil Crow <me@basilcrow.com>
  • Loading branch information
jtnord and basil authored Apr 12, 2023
1 parent f7d8470 commit 94061cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/it/verify-it/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Files.newInputStream(new File(basedir, 'target/verify-it/META-INF/MANIFEST.MF').
assert manifest.getMainAttributes().getValue('Created-By').startsWith('Maven Archiver')
assert manifest.getMainAttributes().getValue('Extension-Name') == null // was provided by Maven 2, but core prefers Short-Name
assert manifest.getMainAttributes().getValue('Group-Id').equals('org.jenkins-ci.tools.hpi.its')
assert manifest.getMainAttributes().getValue('Artifact-Id').equals('verify-it')
assert manifest.getMainAttributes().getValue('Hudson-Version').equals('2.361.4')
assert manifest.getMainAttributes().getValue('Implementation-Title').equals('MyNewPlugin') // was project.artifactId in previous versions, now project.name
assert manifest.getMainAttributes().getValue('Implementation-Version').equals('1.0-SNAPSHOT')
Expand All @@ -51,7 +52,11 @@ Files.newInputStream(new File(basedir, 'target/verify-it/META-INF/MANIFEST.MF').
assert manifest.getMainAttributes().getValue('Plugin-Developers').equals('Noam Chomsky:nchomsky:nchomsky@example.com')
assert manifest.getMainAttributes().getValue('Plugin-License-Name').equals('MIT License')
assert manifest.getMainAttributes().getValue('Plugin-License-Url').equals('https://opensource.org/licenses/MIT')
assert manifest.getMainAttributes().getValue('Plugin-ScmConnection').equals('scm:git:https://github.com/jenkinsci/verify-it-plugin.git')
assert manifest.getMainAttributes().getValue('Plugin-ScmTag').equals('HEAD')
assert manifest.getMainAttributes().getValue('Plugin-ScmUrl').equals('https://github.com/jenkinsci/verify-it-plugin')
assert manifest.getMainAttributes().getValue('Plugin-GitHash').length() == 40
assert manifest.getMainAttributes().getValue('Plugin-Module') == null
assert manifest.getMainAttributes().getValue('Plugin-Version').startsWith('1.0-SNAPSHOT')
assert manifest.getMainAttributes().getValue('Short-Name').equals('verify-it')
assert manifest.getMainAttributes().getValue('Specification-Title').equals('MyNewPlugin') // was project.description in previous versions, now project.name
Expand All @@ -60,4 +65,4 @@ Files.newInputStream(new File(basedir, 'target/verify-it/META-INF/MANIFEST.MF').

// TODO add some test on hpi file content

return true;
return true;
Original file line number Diff line number Diff line change
Expand Up @@ -710,17 +710,13 @@ private String[] getWarFiles(File sourceDir) {
* @return null if no git repository is found
*/
public String getGitHeadSha1() {
// we want to allow the plugin that's not sitting at the root (such as findbugs plugin),
// but we don't want to go up too far and pick up unrelated repository.
File git = new File(project.getBasedir(), ".git");
if (!git.exists()) {
git = new File(project.getBasedir(),"../.git");
if (!git.exists())
return null;
if (!project.getScm().getConnection().startsWith("scm:git")) {
// Project is not using Git
return null;
}

try {
Process p = new ProcessBuilder("git", "-C", git.getAbsolutePath(), "rev-parse", "HEAD").redirectErrorStream(true).start();
Process p = new ProcessBuilder("git", "rev-parse", "HEAD").directory(project.getBasedir()).redirectErrorStream(true).start();
p.getOutputStream().close();
String v;
try (InputStream is = p.getInputStream()) {
Expand All @@ -732,7 +728,7 @@ public String getGitHeadSha1() {
if (v.length()<8)
return null; // git repository present, but without commits

return v.substring(0,8);
return v;
} catch (IOException | InterruptedException e) {
getLog().debug("Failed to run git rev-parse HEAD", e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
*/
package org.jenkinsci.maven.plugins.hpi;

import edu.umd.cs.findbugs.annotations.CheckForNull;

import org.apache.maven.archiver.ManifestConfiguration;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.model.Developer;
import org.apache.maven.model.License;
import org.apache.maven.model.Scm;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.archiver.jar.Manifest;
import org.codehaus.plexus.archiver.jar.ManifestException;

Expand All @@ -39,6 +41,8 @@
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -119,6 +123,7 @@ protected void setAttributes(Manifest.ExistingSection mainSection) throws MojoEx
}

mainSection.addAttributeAndCheck(new Manifest.Attribute("Group-Id",project.getGroupId()));
mainSection.addAttributeAndCheck(new Manifest.Attribute("Artifact-Id",project.getArtifactId()));
mainSection.addAttributeAndCheck(new Manifest.Attribute("Short-Name",project.getArtifactId()));
mainSection.addAttributeAndCheck(new Manifest.Attribute("Long-Name",pluginName));
String url = project.getUrl();
Expand Down Expand Up @@ -164,8 +169,12 @@ protected void setAttributes(Manifest.ExistingSection mainSection) throws MojoEx
}
if (v.endsWith("-SNAPSHOT") && pluginVersionDescription==null) {
String dt = getGitHeadSha1();
if (dt==null) // if SHA1 isn't available, fall back to timestamp
if (dt == null) {
// if SHA1 isn't available, fall back to timestamp
dt = new SimpleDateFormat("MM/dd/yyyy HH:mm").format(new Date());
} else {
dt = dt.substring(0, 8);
}
pluginVersionDescription = "private-"+dt+"-"+System.getProperty("user.name");
}
if (pluginVersionDescription!=null)
Expand Down Expand Up @@ -208,7 +217,11 @@ protected void setAttributes(Manifest.ExistingSection mainSection) throws MojoEx
addLicenseAttributesForManifest(mainSection);
addPropertyAttributeIfNotNull(mainSection, "Plugin-ChangelogUrl", "hpi.pluginChangelogUrl");
addPropertyAttributeIfNotNull(mainSection, "Plugin-LogoUrl", "hpi.pluginLogoUrl");
addAttributeIfNotNull(mainSection, "Plugin-ScmUrl", getScmUrl());
addAttributeIfNotNull(mainSection, "Plugin-ScmConnection", project.getScm().getConnection());
addAttributeIfNotNull(mainSection, "Plugin-ScmTag", project.getScm().getTag());
addAttributeIfNotNull(mainSection, "Plugin-ScmUrl", project.getScm().getUrl());
addAttributeIfNotNull(mainSection, "Plugin-GitHash", getGitHeadSha1());
addAttributeIfNotNull(mainSection, "Plugin-Module", getModule());
}

/**
Expand Down Expand Up @@ -279,10 +292,23 @@ private void addLicenseAttributesForManifest(Manifest.ExistingSection target) th
}
}

private String getScmUrl() {
Scm scm = project.getScm();
if (scm != null) {
return scm.getUrl();
@CheckForNull
private String getModule() {
MavenProject root = session.getTopLevelProject();
if (!project.equals(root)) {
try {
Path rootPath = root.getBasedir().toPath();
Path modulePath = project.getBasedir().toPath();
String module = rootPath.relativize(modulePath).toString();
// Normalize to Unix style
module = module.replace(File.separatorChar, '/');
// Normalize empty to null
if (!module.isBlank()) {
return module;
}
} catch (InvalidPathException e) {
getLog().warn("Failed to obtain module name", e);
}
}
return null;
}
Expand Down

0 comments on commit 94061cd

Please sign in to comment.