Skip to content

Commit

Permalink
Unify access to the Tycho cache directory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laeubi committed Dec 26, 2024
1 parent 0188d5b commit 75838f3
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,8 @@ public File downloadToFile(URI uri) throws IOException {
}
}

TransportCacheConfig getCacheConfig() {
return cacheConfig;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,13 @@

public class TychoRepositoryTransportCacheManager extends CacheManager {

public static final String CACHE_RELPATH = ".cache/tycho/p2-repository-metadata";

private static final List<String> 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
Expand Down Expand Up @@ -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");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,22 @@
*******************************************************************************/
package org.eclipse.tycho.p2maven.transport;

import java.io.File;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.LegacySupport;
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;
import org.eclipse.tycho.TychoConstants;

@Component(role = IAgentServiceFactory.class, hint = "org.eclipse.equinox.internal.p2.repository.CacheManager")
public class TychoRepositoryTransportCacheManagerAgentFactory implements IAgentServiceFactory, Initializable {

@Requirement
private LegacySupport legacySupport;
private File repoDir;
public class TychoRepositoryTransportCacheManagerAgentFactory implements IAgentServiceFactory {

@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 = TychoConstants.DEFAULT_USER_LOCALREPOSITORY;
} else {
repoDir = new File(session.getLocalRepository().getBasedir());
}
}

}
12 changes: 11 additions & 1 deletion src/site/markdown/SystemProperties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 6 additions & 5 deletions tycho-core/src/main/java/org/eclipse/tycho/core/PGPService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}";
Expand All @@ -61,6 +60,9 @@ public class PGPService {
@Requirement
RepositorySystem repositorySystem;

@Requirement
TransportCacheConfig transportCacheConfig;

/**
* Get the attached PGP signature for the given MavenProject
*
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,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;

Expand Down Expand Up @@ -103,6 +104,9 @@ public class TychoMavenLifecycleParticipant extends AbstractMavenLifecyclePartic
@Requirement
TychoProjectManager projectManager;

@Requirement
TransportCacheConfig transportCacheConfig;

public TychoMavenLifecycleParticipant() {
// needed for plexus
}
Expand Down Expand Up @@ -374,8 +378,7 @@ private boolean isM2E(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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, OsgiManifest> manifestCache = new HashMap<>();

private File cacheDir;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
List<Exception> 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);
Expand Down

0 comments on commit 75838f3

Please sign in to comment.