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

Add build timestamp to the manifest and enable deep aggregate inspection #3364

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions tycho-api/src/main/java/org/eclipse/tycho/TychoConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public interface TychoConstants {

public static final String BUILD_TIMESTAMP = CTX_BASENAME + "/buildTimestamp";

String HEADER_TYCHO_BUILD_TIMESTAMP = "Tycho-Build-Timestamp";

String HEADER_BND_LAST_MODIFIED = "Bnd-LastModified";

public String JAR_EXTENSION = "jar";

String PROP_GROUP_ID = "maven-groupId";
Expand Down
5 changes: 5 additions & 0 deletions tycho-artifactcomparator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<artifactId>jfiveparse</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.jar.Manifest;

import org.codehaus.plexus.component.annotations.Component;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.artifactcomparator.ArtifactComparator.ComparisonData;
import org.eclipse.tycho.artifactcomparator.ArtifactDelta;
import org.eclipse.tycho.artifactcomparator.ComparatorInputStream;
Expand All @@ -51,13 +52,14 @@ private static enum Change {
new Name("Build-Jdk"), //
new Name("Built-By"), //
new Name("Build-Jdk-Spec"),
//added by Tycho itself
new Name(TychoConstants.HEADER_TYCHO_BUILD_TIMESTAMP), //
// lets be friendly to bnd/maven-bundle-plugin
new Name("Bnd-LastModified"), //
new Name(TychoConstants.HEADER_BND_LAST_MODIFIED), //
new Name("Bundle-Developers"), //
new Name("Tool"),
// this is common attribute not supported by Tycho yet
new Name("Eclipse-SourceReferences"));
// TODO make it possible to disable default ignores and add custom ignore

@Override
public ArtifactDelta getDelta(ComparatorInputStream baseline, ComparatorInputStream reactor, ComparisonData data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,25 @@
*******************************************************************************/
package org.eclipse.tycho.buildversion;

import java.io.File;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.OptionalLong;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.tycho.ArtifactDescriptor;
import org.eclipse.tycho.ReactorProject;
import org.eclipse.tycho.TargetPlatformService;
import org.eclipse.tycho.TychoConstants;
import org.eclipse.tycho.core.ArtifactDependencyVisitor;
import org.eclipse.tycho.core.FeatureDescription;
import org.eclipse.tycho.core.PluginDescription;
Expand Down Expand Up @@ -60,6 +68,15 @@ public class BuildQualifierAggregatorMojo extends BuildQualifierMojo {
@Component
private TargetPlatformService platformService;

/**
* Controls if when a aggregation happens the mojo should use the embedded
* timestamps in the jar manifest or finally fall back to the last modified
* timestamps of the jar entries itself if it can't parse the qualifier from the
* file name.
*/
@Parameter(defaultValue = "true")
private boolean useArtifactTimestamps = true;

@Override
protected Date getBuildTimestamp() throws MojoExecutionException {
Date timestamp = super.getBuildTimestamp();
Expand Down Expand Up @@ -103,49 +120,77 @@ public void visitPlugin(PluginDescription plugin) {
}

private void visitArtifact(ArtifactDescriptor artifact) {
ReactorProject otherProject = artifact.getMavenProject();
String otherVersion = (otherProject != null) ? otherProject.getExpandedVersion()
: artifact.getKey().getVersion();
Version v = Version.parseVersion(otherVersion);
String otherQualifier = v.getQualifier();
if (otherQualifier != null) {
Date timestamp = parseQualifier(otherQualifier);
if (timestamp != null) {
if (latestTimestamp[0].compareTo(timestamp) < 0) {
if (getLog().isDebugEnabled()) {
getLog().debug("Found '" + format.format(timestamp) + "' from qualifier '"
+ otherQualifier + "' for artifact " + artifact);
}
latestTimestamp[0] = timestamp;
}
} else {
getLog().debug("Could not parse qualifier timestamp " + otherQualifier);
}
}
}

private Date parseQualifier(String qualifier) {
Date timestamp = parseQualifier(qualifier, format);
if (timestamp != null) {
return timestamp;
}
return discoverTimestamp(qualifier);
Date timestamp = getTimestamp(artifact);
if (timestamp != null && latestTimestamp[0].compareTo(timestamp) < 0) {
latestTimestamp[0] = timestamp;
}
}

private Date parseQualifier(String qualifier, SimpleDateFormat format) {
ParsePosition pos = new ParsePosition(0);
Date timestamp = format.parse(qualifier, pos);
if (timestamp != null && pos.getIndex() == qualifier.length()) {
return timestamp;
}
return null;
}

private Date discoverTimestamp(String qualifier) {
return timestampFinder.findInString(qualifier);
}
});

return latestTimestamp[0];
}

private Date getTimestamp(ArtifactDescriptor artifact) {
ReactorProject otherProject = artifact.getMavenProject();
if (otherProject != null) {
Object contextValue = otherProject.getContextValue(TychoConstants.BUILD_TIMESTAMP);
if (contextValue instanceof Date date) {
return date;
}
}
String otherVersion = (otherProject != null) ? otherProject.getExpandedVersion()
: artifact.getKey().getVersion();
Version v = Version.parseVersion(otherVersion);
String otherQualifier = v.getQualifier();
if (otherQualifier != null) {
Date parseQualifier = parseQualifier(otherQualifier);
if (parseQualifier != null) {
return parseQualifier;
}
}
if (useArtifactTimestamps) {
try {
File file = artifact.fetchArtifact().get();
try (JarFile jarFile = new JarFile(file)) {
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
Attributes attributes = manifest.getMainAttributes();
String tychoTs = attributes.getValue(TychoConstants.HEADER_TYCHO_BUILD_TIMESTAMP);
if (tychoTs != null) {
return new Date(Long.parseLong(tychoTs));
}
String bndTs = attributes.getValue(TychoConstants.HEADER_BND_LAST_MODIFIED);
if (bndTs != null) {
return new Date(Long.parseLong(bndTs));
}
}
OptionalLong max = jarFile.stream().mapToLong(JarEntry::getTime).filter(l -> l > 0).max();
if (max.isPresent()) {
return new Date(max.getAsLong());
}
}
} catch (Exception e) {
// can't use it then...
}
}
return null;
}

private Date parseQualifier(String qualifier) {
Date timestamp = parseQualifier(qualifier, format);
if (timestamp != null) {
return timestamp;
}
return timestampFinder.findInString(qualifier);
}

private Date parseQualifier(String qualifier, SimpleDateFormat format) {
ParsePosition pos = new ParsePosition(0);
Date timestamp = format.parse(qualifier, pos);
if (timestamp != null && pos.getIndex() == qualifier.length()) {
return timestamp;
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
project.getProperties().put(UNQUALIFIED_VERSION, projectVersion.unqualifiedVersion);
project.getProperties().put(QUALIFIED_VERSION, projectVersion.getOSGiVersion());
getLog().info("The project's OSGi version is " + projectVersion.getOSGiVersion());
DefaultReactorProject.adapt(project).setContextValue(TychoConstants.BUILD_TIMESTAMP, projectVersion);
DefaultReactorProject.adapt(project).setContextValue(TychoConstants.BUILD_TIMESTAMP, timestamp);
}

private TychoProjectVersion calculateQualifiedVersion(Date timestamp)
Expand Down
Loading
Loading