From 9cbac6f5d9da365827392d9b3650da0972eef101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Sun, 21 Apr 2024 18:44:57 +0200 Subject: [PATCH] Implement the new agent properties to be backed by maven IProvisioningAgent now supports new methods to gather so called "agent properties", this implements the new methods in terms of maven so they can be supplied by user or system properties as well as profiles. --- .../p2maven/DefaultProvisioningAgent.java | 14 +++ ...ArtifactRepositoryManagerAgentFactory.java | 41 +------- .../tycho/helper/MavenPropertyHelper.java | 96 +++++++++++++++++++ 3 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 tycho-spi/src/main/java/org/eclipse/tycho/helper/MavenPropertyHelper.java diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/DefaultProvisioningAgent.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/DefaultProvisioningAgent.java index 19e420c5be..92c709a71f 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/DefaultProvisioningAgent.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/DefaultProvisioningAgent.java @@ -24,6 +24,7 @@ import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory; import org.eclipse.sisu.equinox.EquinoxServiceFactory; +import org.eclipse.tycho.helper.MavenPropertyHelper; @Component(role = IProvisioningAgent.class) public class DefaultProvisioningAgent implements IProvisioningAgent { @@ -40,6 +41,9 @@ public class DefaultProvisioningAgent implements IProvisioningAgent { @Requirement Map agentFactories; + @Requirement + MavenPropertyHelper propertyHelper; + private Map> agentServices = new ConcurrentHashMap<>(); @Override @@ -107,6 +111,16 @@ public void unregisterService(String serviceName, Object service) { } } + @Override + public String getProperty(String key, String defaultValue) { + return propertyHelper.getGlobalProperty(key, defaultValue); + } + + @Override + public String getProperty(String key) { + return propertyHelper.getGlobalProperty(key); + } + private static final class LazyAgentServiceFactory implements Supplier { private IAgentServiceFactory factory; diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RemoteArtifactRepositoryManagerAgentFactory.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RemoteArtifactRepositoryManagerAgentFactory.java index b2941a552d..74868895de 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RemoteArtifactRepositoryManagerAgentFactory.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/RemoteArtifactRepositoryManagerAgentFactory.java @@ -12,12 +12,6 @@ *******************************************************************************/ package org.eclipse.tycho.p2maven.transport; -import java.util.List; -import java.util.Properties; - -import org.apache.maven.execution.MavenSession; -import org.apache.maven.settings.Profile; -import org.apache.maven.settings.Settings; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; import org.codehaus.plexus.logging.Logger; @@ -26,6 +20,7 @@ import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory; import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; import org.eclipse.tycho.IRepositoryIdManager; +import org.eclipse.tycho.helper.MavenPropertyHelper; import org.eclipse.tycho.version.TychoVersion; @Component(role = IAgentServiceFactory.class, hint = "org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager") @@ -41,7 +36,7 @@ public class RemoteArtifactRepositoryManagerAgentFactory implements IAgentServic MavenAuthenticator authenticator; @Requirement - MavenSession mavenSession; + MavenPropertyHelper propertyHelper; @Override public Object createService(IProvisioningAgent agent) { @@ -55,7 +50,7 @@ public Object createService(IProvisioningAgent agent) { private boolean getDisableP2MirrorsConfiguration() { String deprecatedKey = "tycho.disableP2Mirrors"; - String deprecatedValue = getMirrorProperty(deprecatedKey); + String deprecatedValue = propertyHelper.getGlobalProperty(deprecatedKey); if (deprecatedValue != null) { logger.info("Using " + deprecatedKey @@ -64,7 +59,7 @@ private boolean getDisableP2MirrorsConfiguration() { return getBooleanValue(deprecatedValue); } - String value = getMirrorProperty("eclipse.p2.mirrors"); + String value = propertyHelper.getGlobalProperty("eclipse.p2.mirrors"); if (value != null) { // eclipse.p2.mirrors false -> disable mirrors @@ -85,32 +80,4 @@ private boolean getBooleanValue(String value) { return Boolean.parseBoolean(value); } - private String getMirrorProperty(String key) { - // Check user properties first ... - Properties userProperties = mavenSession.getUserProperties(); - String userProperty = userProperties.getProperty(key); - if (userProperty != null) { - return userProperty; - } - // check if there are any active profile properties ... - Settings settings = mavenSession.getSettings(); - List profiles = settings.getProfiles(); - List activeProfiles = settings.getActiveProfiles(); - for (Profile profile : profiles) { - if (activeProfiles.contains(profile.getId())) { - String profileProperty = profile.getProperties().getProperty(key); - if (profileProperty != null) { - return profileProperty; - } - } - } - // now maven system properties - Properties systemProperties = mavenSession.getSystemProperties(); - String systemProperty = systemProperties.getProperty(key); - if (systemProperty != null) { - return systemProperty; - } - // java sysem properties last - return System.getProperty(key); - } } diff --git a/tycho-spi/src/main/java/org/eclipse/tycho/helper/MavenPropertyHelper.java b/tycho-spi/src/main/java/org/eclipse/tycho/helper/MavenPropertyHelper.java new file mode 100644 index 0000000000..dbd6db4847 --- /dev/null +++ b/tycho-spi/src/main/java/org/eclipse/tycho/helper/MavenPropertyHelper.java @@ -0,0 +1,96 @@ +/******************************************************************************* + * Copyright (c) 2024 Christoph Läubrich 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: + * Christoph Läubrich - initial API and implementation + *******************************************************************************/ +package org.eclipse.tycho.helper; + +import java.util.List; +import java.util.Properties; + +import org.apache.maven.execution.MavenSession; +import org.apache.maven.plugin.LegacySupport; +import org.apache.maven.settings.Profile; +import org.apache.maven.settings.Settings; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.component.annotations.Requirement; + +@Component(role = MavenPropertyHelper.class) +public class MavenPropertyHelper { + + @Requirement + LegacySupport legacySupport; + + /** + * Returns a global (user) property of the given key, the search order is + *
    + *
  1. Maven session user properties
  2. + *
  3. Active profile(s) property
  4. + *
  5. Maven session system properties
  6. + *
  7. Java System Properties
  8. + *
+ * + * @param key + * the key to search + * @return the value according to the described search order or null if nothing can + * be found. + */ + public String getGlobalProperty(String key) { + return getGlobalProperty(key, null); + } + + /** + * Returns a global (user) property of the given key, the search order is + *
    + *
  1. Maven session user properties
  2. + *
  3. Active profile(s) property
  4. + *
  5. Maven session system properties
  6. + *
  7. Java System Properties
  8. + *
  9. default value
  10. + *
+ * + * @param key + * the key to search + * @param defaultValue + * the default value to use as a last resort + * @return the value according to the described search order + */ + public String getGlobalProperty(String key, String defaultValue) { + MavenSession mavenSession = legacySupport.getSession(); + if (mavenSession != null) { + // Check user properties first ... + Properties userProperties = mavenSession.getUserProperties(); + String userProperty = userProperties.getProperty(key); + if (userProperty != null) { + return userProperty; + } + // check if there are any active profile properties ... + Settings settings = mavenSession.getSettings(); + List profiles = settings.getProfiles(); + List activeProfiles = settings.getActiveProfiles(); + for (Profile profile : profiles) { + if (activeProfiles.contains(profile.getId())) { + String profileProperty = profile.getProperties().getProperty(key); + if (profileProperty != null) { + return profileProperty; + } + } + } + // now maven system properties + Properties systemProperties = mavenSession.getSystemProperties(); + String systemProperty = systemProperties.getProperty(key); + if (systemProperty != null) { + return systemProperty; + } + } + // java sysem properties last + return System.getProperty(key, defaultValue); + } +}