Skip to content

Commit

Permalink
Issue eclipse-tycho#611 Support setting CI-Friendly-Versions in
Browse files Browse the repository at this point in the history
tycho-build-extension

Add a first implementation and integration test
  • Loading branch information
laeubi committed Apr 24, 2022
1 parent 16cb813 commit dc53be8
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* 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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.TimeZone;

import javax.annotation.Priority;
import javax.inject.Named;

import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.interpolation.DefaultModelVersionProcessor;
import org.apache.maven.model.interpolation.ModelVersionProcessor;

@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";

@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 {
Date startTime = request.getBuildStartTime();
if (startTime != null) {
String formatString = request.getSystemProperties().getProperty("tycho.buildqualifier.format");
if (formatString != null) {
SimpleDateFormat format = new SimpleDateFormat(formatString);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
String qualifier = format.format(startTime);
modelProperties.put(BUILD_QUALIFIER, "." + qualifier);
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Christoph Läubrich and others.
* Copyright (c) 2021, 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
Expand Down Expand Up @@ -55,7 +55,6 @@
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.dag.CycleDetectedException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.internal.p2.director.DirectorActivator;
import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureParser;
import org.eclipse.equinox.p2.metadata.IArtifactKey;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
Expand All @@ -72,8 +71,10 @@
import org.eclipse.tycho.p2.util.resolution.ProjectorResolutionStrategy;
import org.eclipse.tycho.p2.util.resolution.ResolutionDataImpl;
import org.eclipse.tycho.p2.util.resolution.ResolverException;
import org.eclipse.tycho.pomless.AbstractTychoMapping;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.sonatype.maven.polyglot.mapping.Mapping;

@Component(role = GraphBuilder.class, hint = GraphBuilder.HINT)
public class TychoGraphBuilder extends DefaultGraphBuilder {
Expand All @@ -90,8 +91,22 @@ public class TychoGraphBuilder extends DefaultGraphBuilder {
@Requirement(hint = "plexus")
private BundleContext bundleContext;

@Requirement(role = Mapping.class)
private Map<String, Mapping> polyglotMappings;

@Override
public Result<ProjectDependencyGraph> build(MavenSession session) {
// Tell the polyglot mappings that we are in extension mode
for (Mapping mapping : polyglotMappings.values()) {
if (mapping instanceof AbstractTychoMapping) {
AbstractTychoMapping tychoMapping = (AbstractTychoMapping) mapping;
tychoMapping.setExtensionMode(true);
tychoMapping.setMultiModuleProjectDirectory(session.getRequest().getMultiModuleProjectDirectory());
if (session.getRequest().getSystemProperties().getProperty("tycho.buildqualifier.format") != null) {
tychoMapping.setSnapshotFormat("${" + TychoCiFriendlyVersions.BUILD_QUALIFIER + "}");
}
}
}
MavenExecutionRequest request = session.getRequest();
ProjectDependencyGraph dependencyGraph = session.getProjectDependencyGraph();
Result<ProjectDependencyGraph> graphResult = super.build(session);
Expand Down Expand Up @@ -201,9 +216,10 @@ public Result<ProjectDependencyGraph> build(MavenSession session) {
.distinct()//
.peek(project -> loggerAdapter.debug(" + add upstream project '" + project.getName()
+ "' of project '" + projectRequest.mavenProject.getName() + "'..."))//
// make behaviors are both false here as projectDependenciesMap includes transitive already
.forEach(
project -> queue.add(new ProjectRequest(project, false, false, projectRequest)));
// make behaviors are both false here as projectDependenciesMap includes
// transitive already
.forEach(project -> queue
.add(new ProjectRequest(project, false, false, projectRequest)));
}
if (projectRequest.requestDownstream) {
projectDependenciesMap.entrySet().stream()//
Expand All @@ -217,7 +233,8 @@ public Result<ProjectDependencyGraph> build(MavenSession session) {
.distinct()//
.peek(project -> loggerAdapter.debug(" + add downstream project '" + project.getName()
+ "' of project '" + projectRequest.mavenProject.getName() + "'..."))//
// request dependencies of dependants, otherwise, -amd would not be able to produce a satisfiable build graph
// request dependencies of dependants, otherwise, -amd would not be able to
// produce a satisfiable build graph
.forEach(
project -> queue.add(new ProjectRequest(project, false, true, projectRequest)));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Lablicate GmbH and others.
* Copyright (c) 2019, 2022 Lablicate GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -64,6 +64,9 @@ public abstract class AbstractTychoMapping implements Mapping, ModelReader {
protected Logger logger;

private ModelWriter writer;
private boolean extensionMode;
private File multiModuleProjectDirectory;
private String snapshotFormat;

@Override
public File locatePom(File dir) {
Expand Down Expand Up @@ -295,11 +298,32 @@ private static void setLocation(Model model, File modelSource) {
model.setLocation("", new InputLocation(0, 0, inputSource));
}

protected static String getPomVersion(String pdeVersion) {
protected String getPomVersion(String pdeVersion) {
String pomVersion = pdeVersion;
if (pdeVersion.endsWith(QUALIFIER_SUFFIX)) {
pomVersion = pdeVersion.substring(0, pdeVersion.length() - QUALIFIER_SUFFIX.length()) + "-SNAPSHOT";
String unqualifiedVersion = pdeVersion.substring(0, pdeVersion.length() - QUALIFIER_SUFFIX.length());
if (isExtensionMode() && snapshotFormat != null) {
return unqualifiedVersion + "." + snapshotFormat;
}
return unqualifiedVersion + "-SNAPSHOT";
}
return pomVersion;
}

public boolean isExtensionMode() {
return extensionMode;
}

public void setExtensionMode(boolean extensionMode) {
this.extensionMode = extensionMode;

}

public void setMultiModuleProjectDirectory(File multiModuleProjectDirectory) {
this.multiModuleProjectDirectory = multiModuleProjectDirectory;
}

public void setSnapshotFormat(String snapshotFormat) {
this.snapshotFormat = snapshotFormat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<extensions>
<extension>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-build</artifactId>
<version>${tycho-version}</version>
</extension>
</extensions>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-Dtycho-version=3.0.0-SNAPSHOT
-Dtycho.buildqualifier.format=yyyyMMddHHmm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: bundle Plug-in
Bundle-SymbolicName: bundle
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-11
Export-Package: bundle
Automatic-Module-Name: bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = .,\
META-INF/
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2021 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 bundle;

public class Something {

public static void sayHello() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: bundle2
Bundle-SymbolicName: bundle2;singleton:=true
Bundle-Version: 1.0.0.qualifier
Require-Bundle: bundle;bundle-version="1.0.0"
Bundle-RequiredExecutionEnvironment: JavaSE-11
Automatic-Module-Name: bundle2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = .,\
META-INF/
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* Copyright (c) 2021 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 bundle2;

import bundle.Something;

public class BundleClient {

public void doSomething() {
Something.sayHello();
}

}
30 changes: 30 additions & 0 deletions tycho-its/projects/ci-friendly/buildqualifier.default/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.eclipse.tycho.tycho-its</groupId>
<artifactId>ci-friendly-parent</artifactId>
<packaging>pom</packaging>
<version>${releaseVersion}${qualifier}</version>
<modules>
<module>bundle</module>
<module>bundle2</module>
</modules>

<properties>
<!-- Define the release version used for unversioned items like pom file -->
<releaseVersion>1.0.0</releaseVersion>
<!-- Defines the default Qualifier if no format is given-->
<qualifier>-SNAPSHOT</qualifier>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.eclipse.tycho.test.buildextension;

import java.util.List;

import org.apache.maven.it.Verifier;
import org.eclipse.tycho.test.AbstractTychoIntegrationTest;
import org.junit.Test;

public class CiFriendlyVersionsTest extends AbstractTychoIntegrationTest {

@Test
public void testDefaultBuildQualifier() throws Exception {
Verifier verifier = getVerifier("ci-friendly/buildqualifier.default", false, true);
verifier.executeGoals(List.of("clean", "package"));
verifier.verifyErrorFreeLog();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 Sonatype Inc. and others.
* Copyright (c) 2008, 2022 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* Sonatype Inc. - initial API and implementation
* Christoph Läubrich - Issue #611 - Support setting CI-Friendly-Versions in tycho-build-extension
*******************************************************************************/
package org.eclipse.tycho.buildversion;

Expand Down Expand Up @@ -93,7 +94,7 @@ public class BuildQualifierMojo extends AbstractVersionMojo {
* Specify a date format as specified by java.text.SimpleDateFormat. Timezone used is UTC.
* </p>
*/
@Parameter(defaultValue = "yyyyMMddHHmm")
@Parameter(defaultValue = "yyyyMMddHHmm", property = "tycho.buildqualifier.format")
protected SimpleDateFormat format;

@Parameter(property = "forceContextQualifier")
Expand Down Expand Up @@ -144,8 +145,21 @@ private TychoProjectVersion calculateQualifiedVersion() throws MojoFailureExcept
return new TychoProjectVersion(unqualifiedVersion, osgiVersion.getQualifier());
}
}
String qualifier = getDesiredQualifier();

String qualifier = forceContextQualifier;
validateQualifier(qualifier);

String pomOSGiVersion = getUnqualifiedVersion();
String suffix = "." + qualifier;
if (pomOSGiVersion.endsWith(suffix)) {
return new TychoProjectVersion(pomOSGiVersion.substring(0, pomOSGiVersion.length() - suffix.length()),
qualifier);
}
return new TychoProjectVersion(pomOSGiVersion, qualifier);
}

protected String getDesiredQualifier() throws MojoExecutionException {
String qualifier = forceContextQualifier;

if (qualifier == null) {
qualifier = DefaultReactorProject.adapt(project).getBuildProperties().getForceContextQualifier();
Expand All @@ -155,11 +169,8 @@ private TychoProjectVersion calculateQualifiedVersion() throws MojoFailureExcept
Date timestamp = getBuildTimestamp();
qualifier = getQualifier(timestamp);
}

validateQualifier(qualifier);

return new TychoProjectVersion(getUnqualifiedVersion(), qualifier);
}
return qualifier;
}

private Version getParsedOSGiVersion() throws MojoFailureException {
String osgiVersionString = getOSGiVersion();
Expand Down
Loading

0 comments on commit dc53be8

Please sign in to comment.