Skip to content

Commit

Permalink
Add build timestamp to the manifest and enable deep aggregate inspection
Browse files Browse the repository at this point in the history
Currently the build timestamp is only encoded in the file-name. This is
then parsed back from the string into a date.

This has several drawbacks:
- it is sometimes unreliable and usually the user won't notice that e.g.
when the dependency and the build are using different formating rules
for qualifiers
- it does not work well for "standard" artifacts not build by Tycho that
are probably not using qualifiers in the name

Because of this, the following changes are applied:
- the build timestamp is read from the reactor if possible
- the build timestamp is written to the manifest as part of the
packaging
- if parsing is not successful the manifest is inspected if it contains
some well known build time stamps from either Tycho or BND
- if thats not available the jar entry time is used as a last resort

both things can be configured but are enabled by default just in case it
causes issues for the users and they rely on the old behavior.
  • Loading branch information
laeubi committed Jan 15, 2024
1 parent 3e94902 commit f7ef039
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 354 deletions.
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

0 comments on commit f7ef039

Please sign in to comment.