From 75eddfd6240e95dd3178f030a330909c3699618f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Thu, 26 Dec 2024 08:39:29 +0100 Subject: [PATCH] Backport Unify access to the Tycho cache directory Currently Tycho caches data in the local maven repository but the access to that directory is spread over several places. This unifies the way to get the base directory and adds a new property 'tycho.p2.transport.cache' to allow specify an alternative location. --- .../DefaultTransportCacheConfig.java | 8 +++++-- .../transport/TychoRepositoryTransport.java | 4 ++++ .../TychoRepositoryTransportCacheManager.java | 11 ++++------ ...toryTransportCacheManagerAgentFactory.java | 21 ++----------------- src/site/markdown/SystemProperties.md | 12 ++++++++++- .../org/eclipse/tycho/core/PGPService.java | 11 +++++----- .../maven/TychoMavenLifecycleParticipant.java | 7 +++++-- .../core/osgitools/DefaultBundleReader.java | 5 ++--- .../core/test/DefaultBundleReaderTest.java | 4 ++-- .../p2/repository/MavenP2SiteMojo.java | 2 +- 10 files changed, 43 insertions(+), 42 deletions(-) diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java index 256fe867b6..f3ab8562e0 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java @@ -49,8 +49,12 @@ public void initialize() throws InitializationException { update = session.getRequest().isUpdateSnapshots(); interactive = session.getRequest().isInteractiveMode() && showTransferProgress(session); } - - cacheLocation = new File(repoDir, ".cache/tycho"); + String property = System.getProperty("tycho.p2.transport.cache"); + if (property == null || property.isBlank()) { + cacheLocation = new File(repoDir, ".cache/tycho"); + } else { + cacheLocation = new File(property); + } cacheLocation.mkdirs(); } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java index 0b9a96101c..648cb661ca 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java @@ -231,4 +231,8 @@ public File downloadToFile(URI uri) throws IOException { } } + TransportCacheConfig getCacheConfig() { + return cacheConfig; + } + } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManager.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManager.java index a36cb8b40f..c3c8654fe5 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManager.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManager.java @@ -25,18 +25,13 @@ public class TychoRepositoryTransportCacheManager extends CacheManager { - public static final String CACHE_RELPATH = ".cache/tycho/p2-repository-metadata"; - private static final List EXTENSIONS = List.of(".jar", ".xml"); private TychoRepositoryTransport transport; - private File localRepositoryRoot; - - public TychoRepositoryTransportCacheManager(TychoRepositoryTransport transport, File localRepositoryRoot) { + public TychoRepositoryTransportCacheManager(TychoRepositoryTransport transport) { super(null, transport); this.transport = transport; - this.localRepositoryRoot = localRepositoryRoot; } @Override @@ -77,7 +72,9 @@ public File createCacheFromFile(URI remoteFile, IProgressMonitor monitor) throws @Override protected File getCacheDirectory() { - return new File(localRepositoryRoot, CACHE_RELPATH); + + TransportCacheConfig config = transport.getCacheConfig(); + return new File(config.getCacheLocation(), "p2-repository-metadata"); } } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManagerAgentFactory.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManagerAgentFactory.java index d78b2c4ac5..28c3041758 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManagerAgentFactory.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransportCacheManagerAgentFactory.java @@ -12,44 +12,27 @@ *******************************************************************************/ package org.eclipse.tycho.p2maven.transport; -import java.io.File; - -import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.LegacySupport; -import org.apache.maven.repository.RepositorySystem; import org.codehaus.plexus.component.annotations.Component; import org.codehaus.plexus.component.annotations.Requirement; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; -import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException; import org.eclipse.equinox.internal.p2.repository.CacheManagerComponent; import org.eclipse.equinox.internal.p2.repository.Transport; import org.eclipse.equinox.p2.core.IProvisioningAgent; import org.eclipse.equinox.p2.core.spi.IAgentServiceFactory; @Component(role = IAgentServiceFactory.class, hint = "org.eclipse.equinox.internal.p2.repository.CacheManager") -public class TychoRepositoryTransportCacheManagerAgentFactory implements IAgentServiceFactory, Initializable { +public class TychoRepositoryTransportCacheManagerAgentFactory implements IAgentServiceFactory { @Requirement private LegacySupport legacySupport; - private File repoDir; @Override public Object createService(IProvisioningAgent agent) { Object transport = agent.getService(Transport.SERVICE_NAME); if (transport instanceof TychoRepositoryTransport tychoRepositoryTransport) { - return new TychoRepositoryTransportCacheManager(tychoRepositoryTransport, repoDir); + return new TychoRepositoryTransportCacheManager(tychoRepositoryTransport); } return new CacheManagerComponent().createService(agent); } - @Override - public void initialize() throws InitializationException { - MavenSession session = legacySupport.getSession(); - if (session == null) { - repoDir = RepositorySystem.defaultUserLocalRepository; - } else { - repoDir = new File(session.getLocalRepository().getBasedir()); - } - } - } diff --git a/src/site/markdown/SystemProperties.md b/src/site/markdown/SystemProperties.md index 6e8ed20a8b..a739b6b8a3 100644 --- a/src/site/markdown/SystemProperties.md +++ b/src/site/markdown/SystemProperties.md @@ -29,10 +29,20 @@ tycho.comparator.threshold | bytes | 5242880 (~5MB) | gives the number of bytes ## P2 -These properties control the behaviour of P2 used by Tycho +These properties control the behavior of P2 used by Tycho Name | Value | Default | Documentation --- | --- | --- | --- eclipse.p2.mirrors | true / false | true | Each p2 site can define a list of artifact repository mirrors, this controls if P2 mirrors should be used. This is independent from configuring mirrors in the maven configuration to be used by Tycho! eclipse.p2.maxDownloadAttempts | _any positive integer_ | 3 | Describes how often Tycho attempts to re-download an artifact from a p2 repository in case e.g. a bad mirror was used. One can think of this value as the maximum number of mirrors Tycho/p2 will check. +### Tycho P2 Transport + +These properties control how Tycho downloads artifacts from P2 servers + +Name | Value | Default | Documentation +--- | --- | --- | --- +tycho.p2.transport.cache | file path | local maven repository | Specify the location where Tycho stores certain cache files to speed up successive builds +tycho.p2.transport.debug | true/false | false | enable debugging of the Tycho Transport +tycho.p2.transport.max-download-threads | number | 4 | maximum number of threads that should be used to download artifacts in parallel +tycho.p2.transport.min-cache-minutes | number | 60 | Number of minutes that a cache entry is assumed to be fresh and is not fetched again from the server diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/PGPService.java b/tycho-core/src/main/java/org/eclipse/tycho/core/PGPService.java index d157918781..b32745915c 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/PGPService.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/PGPService.java @@ -26,7 +26,6 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; -import org.apache.maven.execution.MavenSession; import org.apache.maven.project.MavenProject; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -44,13 +43,13 @@ import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.tycho.p2maven.transport.TransportCacheConfig; @Component(role = PGPService.class) public class PGPService { //See GpgSigner.SIGNATURE_EXTENSION private static final String SIGNATURE_EXTENSION = ".asc"; - private static final String CACHE_RELPATH = ".cache/tycho/pgpkeys"; public static final String MAVEN_CENTRAL_KEY_SERVER = "http://pgp.mit.edu/pks/lookup?op=get&search={0}"; public static final String UBUNTU_KEY_SERVER = "https://keyserver.ubuntu.com/pks/lookup?op=get&search={0}"; @@ -61,6 +60,9 @@ public class PGPService { @Requirement RepositorySystem repositorySystem; + @Requirement + TransportCacheConfig transportCacheConfig; + /** * Get the attached PGP signature for the given MavenProject * @@ -97,12 +99,11 @@ public File getAttachedSignature(MavenProject mavenProject) { * @throws IOException * @throws PGPException */ - public PGPPublicKeyRing getPublicKey(long keyID, String keyServerUrl, MavenSession session, int keyServerRetry) + public PGPPublicKeyRing getPublicKey(long keyID, String keyServerUrl, int keyServerRetry) throws IOException, PGPException { String hexKey = "0x" + Long.toHexString(keyID).toUpperCase(); logger.info("Fetching PGP key with id " + hexKey); - File localRepoRoot = new File(session.getLocalRepository().getBasedir()); - File keyCacheFile = new File(new File(localRepoRoot, CACHE_RELPATH), hexKey + ".pub"); + File keyCacheFile = new File(new File(transportCacheConfig.getCacheLocation(), "pgpkeys"), hexKey + ".pub"); InputStream keyStream; if (keyCacheFile.isFile()) { logger.debug("Fetching key from cache: " + keyCacheFile.getAbsolutePath()); diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java index f044041a33..9dfafce4e8 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/maven/TychoMavenLifecycleParticipant.java @@ -64,6 +64,7 @@ import org.eclipse.tycho.core.osgitools.DefaultBundleReader; import org.eclipse.tycho.p2maven.MavenProjectDependencyProcessor; import org.eclipse.tycho.p2maven.MavenProjectDependencyProcessor.ProjectDependencyClosure; +import org.eclipse.tycho.p2maven.transport.TransportCacheConfig; import org.eclipse.tycho.resolver.TychoResolver; import org.eclipse.tycho.version.TychoVersion; @@ -104,6 +105,9 @@ public class TychoMavenLifecycleParticipant extends AbstractMavenLifecyclePartic @Requirement TychoProjectManager projectManager; + @Requirement + TransportCacheConfig transportCacheConfig; + private boolean warnedAboutTychoMode; public TychoMavenLifecycleParticipant() { @@ -399,8 +403,7 @@ private boolean isMavenMode(MavenSession session) { private void configureComponents(MavenSession session) { // TODO why does the bundle reader need to cache stuff in the local maven repository? - File localRepository = new File(session.getLocalRepository().getBasedir()); - ((DefaultBundleReader) bundleReader).setLocationRepository(localRepository); + ((DefaultBundleReader) bundleReader).setCacheLocation(transportCacheConfig.getCacheLocation()); } } diff --git a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultBundleReader.java b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultBundleReader.java index 7542ceb8ef..a78d995541 100644 --- a/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultBundleReader.java +++ b/tycho-core/src/main/java/org/eclipse/tycho/core/osgitools/DefaultBundleReader.java @@ -42,7 +42,6 @@ public class DefaultBundleReader extends AbstractLogEnabled implements BundleReader { private static final long LOCK_TIMEOUT = Long.getLong("tycho.bundlereader.lock.timeout", 5 * 60 * 1000L); - public static final String CACHE_PATH = ".cache/tycho"; private final Map manifestCache = new HashMap<>(); private File cacheDir; @@ -160,8 +159,8 @@ private OsgiManifest loadManifestFile(File manifestFile) throws IOException, Osg return OsgiManifest.parse(new FileInputStream(manifestFile), manifestFile.getAbsolutePath()); } - public void setLocationRepository(File basedir) { - this.cacheDir = new File(basedir, CACHE_PATH); + public void setCacheLocation(File basedir) { + this.cacheDir = basedir; } @Override diff --git a/tycho-core/src/test/java/org/eclipse/tycho/core/test/DefaultBundleReaderTest.java b/tycho-core/src/test/java/org/eclipse/tycho/core/test/DefaultBundleReaderTest.java index 3a347271cb..1c66569e89 100644 --- a/tycho-core/src/test/java/org/eclipse/tycho/core/test/DefaultBundleReaderTest.java +++ b/tycho-core/src/test/java/org/eclipse/tycho/core/test/DefaultBundleReaderTest.java @@ -42,7 +42,7 @@ public void setUp() throws Exception { cacheDir.delete(); cacheDir.mkdirs(); bundleReader = (DefaultBundleReader) lookup(BundleReader.class); - bundleReader.setLocationRepository(cacheDir); + bundleReader.setCacheLocation(cacheDir); } @After @@ -106,7 +106,7 @@ public void testGetEntryExtractionCache() throws Exception { public void testGetEntryExternalJar() throws Exception { File bundleJar = getTestJar(); // 370958 IOException will only occur if extraction dir exists already - new File(new File(cacheDir, DefaultBundleReader.CACHE_PATH), bundleJar.getName()).mkdirs(); + new File(cacheDir, bundleJar.getName()).mkdirs(); File externalLib = bundleReader.getEntry(bundleJar, "external:$user.home$/external-lib.jar"); assertNull(externalLib); } diff --git a/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/MavenP2SiteMojo.java b/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/MavenP2SiteMojo.java index 43f51872b7..25054268b7 100644 --- a/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/MavenP2SiteMojo.java +++ b/tycho-p2-repository-plugin/src/main/java/org/eclipse/tycho/plugins/p2/repository/MavenP2SiteMojo.java @@ -310,7 +310,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { List errors = new ArrayList<>(); for (String keyServer : keyServers) { try { - PGPPublicKeyRing publicKey = pgpService.getPublicKey(keyID, keyServer, session, + PGPPublicKeyRing publicKey = pgpService.getPublicKey(keyID, keyServer, keyServerRetry); if (publicKey != null) { publicKeys.put(keyID, publicKey);