-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #611 Support setting CI-Friendly-Versions in
tycho-build-extension Add a first implementation and integration test
- Loading branch information
Showing
24 changed files
with
666 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tycho-build/src/main/java/org/eclipse/tycho/build/DefaultBuildTimestampProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012, 2016 Sonatype Inc. and others. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Sonatype Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.tycho.build; | ||
|
||
import java.util.Date; | ||
|
||
import org.apache.maven.execution.MavenSession; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.plexus.component.annotations.Component; | ||
|
||
/** | ||
* Build timestamp provider that returns the same timestamp for all projects, the | ||
* ${maven.build.timestamp}. | ||
*/ | ||
@Component(role = BuildTimestampProvider.class, hint = DefaultBuildTimestampProvider.ROLE_HINT) | ||
public class DefaultBuildTimestampProvider implements BuildTimestampProvider { | ||
|
||
static final String ROLE_HINT = "default"; | ||
|
||
@Override | ||
public Date getTimestamp(MavenSession session, MavenProject project, MojoExecution execution) { | ||
return session.getStartTime(); | ||
} | ||
|
||
@Override | ||
public void setQuiet(boolean quiet) { | ||
|
||
} | ||
} |
200 changes: 200 additions & 0 deletions
200
tycho-build/src/main/java/org/eclipse/tycho/build/TychoCiFriendlyVersions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Christoph Läubrich and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Christoph Läubrich - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.tycho.build; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.TimeZone; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import javax.annotation.Priority; | ||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import org.apache.maven.execution.DefaultMavenExecutionRequest; | ||
import org.apache.maven.execution.DefaultMavenExecutionResult; | ||
import org.apache.maven.execution.MavenSession; | ||
import org.apache.maven.model.Model; | ||
import org.apache.maven.model.Parent; | ||
import org.apache.maven.model.Plugin; | ||
import org.apache.maven.model.building.ModelBuildingRequest; | ||
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor; | ||
import org.apache.maven.model.interpolation.ModelVersionProcessor; | ||
import org.apache.maven.model.io.DefaultModelReader; | ||
import org.apache.maven.plugin.LegacySupport; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.project.MavenProject; | ||
import org.codehaus.plexus.PlexusContainer; | ||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; | ||
import org.codehaus.plexus.logging.Logger; | ||
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; | ||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; | ||
|
||
@Priority(100) | ||
@Named | ||
public class TychoCiFriendlyVersions extends DefaultModelVersionProcessor implements ModelVersionProcessor { | ||
|
||
static final String BUILD_QUALIFIER = "qualifier"; | ||
static final String MICRO_VERSION = "micro"; | ||
static final String MINOR_VERSION = "minor"; | ||
static final String MAJOR_VERSION = "major"; | ||
static final String RELEASE_VERSION = "releaseVersion"; | ||
private PlexusContainer container; | ||
private Logger logger; | ||
private Map<File, MavenProject> rawProjectCache = new ConcurrentHashMap<>(); | ||
|
||
@Inject | ||
public TychoCiFriendlyVersions(PlexusContainer plexusContainer, Logger logger) { | ||
this.container = plexusContainer; | ||
this.logger = logger; | ||
|
||
} | ||
|
||
@Override | ||
public boolean isValidProperty(String property) { | ||
return super.isValidProperty(property) || MAJOR_VERSION.equals(property) || MINOR_VERSION.equals(property) | ||
|| MICRO_VERSION.equals(property) || BUILD_QUALIFIER.equals(property) | ||
|| RELEASE_VERSION.equals(property); | ||
} | ||
|
||
@Override | ||
public void overwriteModelProperties(Properties modelProperties, ModelBuildingRequest request) { | ||
super.overwriteModelProperties(modelProperties, request); | ||
if (request.getSystemProperties().containsKey(MAJOR_VERSION)) { | ||
modelProperties.put(MAJOR_VERSION, request.getSystemProperties().get(MAJOR_VERSION)); | ||
} | ||
if (request.getSystemProperties().containsKey(MINOR_VERSION)) { | ||
modelProperties.put(MINOR_VERSION, request.getSystemProperties().get(MINOR_VERSION)); | ||
} | ||
if (request.getSystemProperties().containsKey(MICRO_VERSION)) { | ||
modelProperties.put(MICRO_VERSION, request.getSystemProperties().get(MICRO_VERSION)); | ||
} | ||
if (request.getSystemProperties().containsKey(BUILD_QUALIFIER)) { | ||
modelProperties.put(BUILD_QUALIFIER, request.getSystemProperties().get(BUILD_QUALIFIER)); | ||
} else { | ||
String formatString = request.getSystemProperties().getProperty("tycho.buildqualifier.format"); | ||
if (formatString != null) { | ||
Date startTime = request.getBuildStartTime(); | ||
File pomFile = request.getPomFile(); | ||
if (startTime != null && pomFile != null) { | ||
String provider = request.getSystemProperties().getProperty("tycho.buildqualifier.provider", | ||
"default"); | ||
try { | ||
BuildTimestampProvider timestampProvider = container.lookup(BuildTimestampProvider.class, | ||
provider); | ||
SimpleDateFormat format = new SimpleDateFormat(formatString); | ||
format.setTimeZone(TimeZone.getTimeZone("UTC")); | ||
MavenProject mavenProject = getMavenProject(pomFile); | ||
timestampProvider.setQuiet(true); | ||
try { | ||
Date timestamp = timestampProvider.getTimestamp(getMavenSession(request), mavenProject, | ||
getExecution(mavenProject)); | ||
String qualifier = format.format(timestamp); | ||
modelProperties.put(BUILD_QUALIFIER, "." + qualifier); | ||
} finally { | ||
timestampProvider.setQuiet(false); | ||
} | ||
} catch (ComponentLookupException | MojoExecutionException e) { | ||
logger.warn("Can't use '" + provider | ||
+ "' as a timestamp provider for tycho-ci-friendly-versions (" + e + ")"); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private MojoExecution getExecution(MavenProject mavenProject) { | ||
|
||
Plugin projectPlugin = getPlugin("org.eclipse.tycho:tycho-packaging-plugin", mavenProject); | ||
if (projectPlugin == null) { | ||
// create a dummy | ||
projectPlugin = new Plugin(); | ||
projectPlugin.setGroupId("org.eclipse.tycho"); | ||
projectPlugin.setArtifactId("tycho-packaging-plugin"); | ||
try { | ||
projectPlugin.setConfiguration( | ||
Xpp3DomBuilder.build(new StringReader( | ||
"<configuration><jgit.dirtyWorkingTree>ignore</jgit.dirtyWorkingTree></configuration>"))); | ||
} catch (XmlPullParserException | IOException e) { | ||
projectPlugin.setConfiguration(null); | ||
} | ||
} | ||
return new MojoExecution(projectPlugin, "", ""); | ||
} | ||
|
||
private Plugin getPlugin(String id, MavenProject mavenProject) { | ||
if (mavenProject == null) { | ||
return null; | ||
} | ||
Plugin plugin = mavenProject.getPlugin(id); | ||
if (plugin == null) { | ||
return getPlugin(id, mavenProject.getParent()); | ||
} | ||
return plugin; | ||
} | ||
|
||
private MavenProject getMavenProject(File pom) { | ||
|
||
MavenProject project = rawProjectCache.computeIfAbsent(pom, file -> { | ||
// at this phase there are no projects, thats all we can offer for now... | ||
MavenProject mavenProject = new MavenProject(); | ||
DefaultModelReader modelReader = new DefaultModelReader(); | ||
try { | ||
mavenProject.setModel(modelReader.read(pom, Map.of())); | ||
} catch (IOException e) { | ||
// nothing to do here then... | ||
} | ||
mavenProject.setFile(pom); | ||
return mavenProject; | ||
}); | ||
Model model = project.getModel(); | ||
if (model != null) { | ||
Parent parent = model.getParent(); | ||
if (parent != null) { | ||
File parentPom = new File(pom.getParentFile(), parent.getRelativePath()); | ||
if (parentPom.isFile()) { | ||
project.setParent(getMavenProject(parentPom)); | ||
} | ||
} | ||
} | ||
return project; | ||
|
||
} | ||
|
||
private MavenSession getMavenSession(ModelBuildingRequest request) { | ||
try { | ||
LegacySupport legacySupport = container.lookup(LegacySupport.class); | ||
MavenSession session = legacySupport.getSession(); | ||
if (session != null) { | ||
return session; | ||
} | ||
} catch (ComponentLookupException e) { | ||
// fall through | ||
} | ||
// create a dummy session... actually time providers are not really interested | ||
// in *all* but very limited details | ||
DefaultMavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest(); | ||
executionRequest.setBaseDirectory(request.getPomFile().getParentFile()); | ||
executionRequest.setStartTime(request.getBuildStartTime()); | ||
return new MavenSession(container, executionRequest, new DefaultMavenExecutionResult(), | ||
Collections.emptyList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.