diff --git a/src/it/verify-it/verify.groovy b/src/it/verify-it/verify.groovy index 39f8ef927b..bc2fb39382 100644 --- a/src/it/verify-it/verify.groovy +++ b/src/it/verify-it/verify.groovy @@ -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') @@ -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 @@ -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; \ No newline at end of file +return true; diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java index c4ae8ba7d5..c126ac5a59 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractHpiMojo.java @@ -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()) { @@ -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; diff --git a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsManifestMojo.java b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsManifestMojo.java index c86a9717da..63fbd8c5cd 100644 --- a/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsManifestMojo.java +++ b/src/main/java/org/jenkinsci/maven/plugins/hpi/AbstractJenkinsManifestMojo.java @@ -15,6 +15,8 @@ */ 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; @@ -22,9 +24,9 @@ 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; @@ -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; @@ -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(); @@ -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) @@ -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()); } /** @@ -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; }