From 217c82d26d843a465d9cad75e6f2c0dec61439b5 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Tue, 11 Aug 2009 12:48:14 +0000 Subject: [PATCH] o Re-added support for offline mode git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@803093 13f79535-47bb-0310-9956-ffa450edef68 --- .../repository/DefaultRepositoryRequest.java | 14 ++++ .../repository/RepositoryRequest.java | 48 ++++++++++++ .../DefaultRepositoryMetadataManager.java | 78 ++++++++++--------- .../resolver/ArtifactResolutionRequest.java | 15 ++++ .../resolver/DefaultArtifactResolver.java | 22 ++++-- .../DefaultMetadataResolutionRequest.java | 26 ++++++- .../metadata/MetadataResolutionRequest.java | 37 ++++++++- .../DefaultMavenExecutionRequest.java | 1 + .../lifecycle/DefaultLifecycleExecutor.java | 10 ++- .../maven/plugin/DefaultPluginManager.java | 1 + .../maven/project/DefaultProjectBuilder.java | 2 + .../DefaultProjectBuildingRequest.java | 14 ++++ .../maven/project/ProjectBuildingRequest.java | 4 + .../project/artifact/MavenMetadataSource.java | 1 + 14 files changed, 222 insertions(+), 51 deletions(-) diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/repository/DefaultRepositoryRequest.java b/maven-compat/src/main/java/org/apache/maven/artifact/repository/DefaultRepositoryRequest.java index 2269846642..e52ea0fad9 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/repository/DefaultRepositoryRequest.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/repository/DefaultRepositoryRequest.java @@ -31,12 +31,26 @@ public class DefaultRepositoryRequest implements RepositoryRequest { + private boolean offline; + private ArtifactRepository localRepository; private List remoteRepositories; private RepositoryCache cache; + public boolean isOffline() + { + return offline; + } + + public DefaultRepositoryRequest setOffline( boolean offline ) + { + this.offline = offline; + + return this; + } + public ArtifactRepository getLocalRepository() { return localRepository; diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/repository/RepositoryRequest.java b/maven-compat/src/main/java/org/apache/maven/artifact/repository/RepositoryRequest.java index 6642a47779..b395c60444 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/repository/RepositoryRequest.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/repository/RepositoryRequest.java @@ -29,16 +29,64 @@ import java.util.List; public interface RepositoryRequest { + /** + * Indicates whether network access to remote repositories has been disabled. + * + * @return {@code true} if remote access has been disabled, {@code false} otherwise. + */ + boolean isOffline(); + + /** + * Enables/disables network access to remote repositories. + * + * @param offline {@code true} to disable remote access, {@code false} to allow network access. + * @return This request, never {@code null}. + */ + RepositoryRequest setOffline( boolean offline ); + + /** + * Gets the local repository to use. + * + * @return The local repository to use or {@code null} if not set. + */ ArtifactRepository getLocalRepository(); + /** + * Sets the local repository to use. + * + * @param localRepository The local repository to use. + * @return This request, never {@code null}. + */ RepositoryRequest setLocalRepository( ArtifactRepository localRepository ); + /** + * Gets the remote repositories to use. + * + * @return The remote repositories to use, never {@code null}. + */ List getRemoteRepositories(); + /** + * Sets the remote repositories to use. + * + * @param remoteRepositories The remote repositories to use. + * @return This request, never {@code null}. + */ RepositoryRequest setRemoteRepositories( List remoteRepositories ); + /** + * Gets the repository cache to use. + * + * @return The repository cache to use or {@code null} if none. + */ RepositoryCache getCache(); + /** + * Sets the repository cache to use. + * + * @param cache The repository cache to use, may be {@code null}. + * @return This request, never {@code null}. + */ RepositoryRequest setCache( RepositoryCache cache ); } diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java index 3987f75059..cda75a53f8 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/DefaultRepositoryMetadataManager.java @@ -106,52 +106,60 @@ public class DefaultRepositoryMetadataManager ArtifactRepository localRepository = request.getLocalRepository(); List remoteRepositories = request.getRemoteRepositories(); - for ( ArtifactRepository repository : remoteRepositories ) + if ( !request.isOffline() ) { - ArtifactRepositoryPolicy policy = metadata.isSnapshot() ? repository.getSnapshots() : repository.getReleases(); - - File file = new File( localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata( metadata, repository ) ); - - if ( updateCheckManager.isUpdateRequired( metadata, repository, file ) ) + for ( ArtifactRepository repository : remoteRepositories ) { - getLogger().info( metadata.getKey() + ": checking for updates from " + repository.getId() ); - try - { - wagonManager.getArtifactMetadata( metadata, repository, file, policy.getChecksumPolicy() ); - } - catch ( ResourceDoesNotExistException e ) - { - getLogger().debug( metadata + " could not be found on repository: " + repository.getId() ); + ArtifactRepositoryPolicy policy = + metadata.isSnapshot() ? repository.getSnapshots() : repository.getReleases(); - // delete the local copy so the old details aren't used. - if ( file.exists() ) + File file = + new File( localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata( metadata, + repository ) ); + + if ( updateCheckManager.isUpdateRequired( metadata, repository, file ) ) + { + getLogger().info( metadata.getKey() + ": checking for updates from " + repository.getId() ); + try { - file.delete(); + wagonManager.getArtifactMetadata( metadata, repository, file, policy.getChecksumPolicy() ); + } + catch ( ResourceDoesNotExistException e ) + { + getLogger().debug( metadata + " could not be found on repository: " + repository.getId() ); + + // delete the local copy so the old details aren't used. + if ( file.exists() ) + { + file.delete(); + } + } + catch ( TransferFailedException e ) + { + getLogger().warn( metadata + " could not be retrieved from repository: " + repository.getId() + + " due to an error: " + e.getMessage() ); + getLogger().debug( "Exception", e ); + } + finally + { + updateCheckManager.touch( metadata, repository, file ); } } - catch ( TransferFailedException e ) + else { - getLogger().warn( metadata + " could not be retrieved from repository: " + repository.getId() + " due to an error: " + e.getMessage() ); - getLogger().debug( "Exception", e ); + getLogger().debug( "Skipping metadata update of " + metadata.getKey() + " from " + + repository.getId() ); } - finally - { - updateCheckManager.touch( metadata, repository, file ); - } - } - else - { - getLogger().debug( "Skipping metadata update of " + metadata.getKey() + " from " + repository.getId() ); - } - // TODO: should this be inside the above check? - // touch file so that this is not checked again until interval has passed - if ( file.exists() ) - { - file.setLastModified( System.currentTimeMillis() ); + // TODO: should this be inside the above check? + // touch file so that this is not checked again until interval has passed + if ( file.exists() ) + { + file.setLastModified( System.currentTimeMillis() ); + } } } - + try { mergeMetadata( metadata, remoteRepositories, localRepository ); diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolutionRequest.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolutionRequest.java index b5e284532e..71bb121c87 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolutionRequest.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolutionRequest.java @@ -68,6 +68,8 @@ public class ArtifactResolutionRequest private boolean resolveTransitively = false; + private boolean offline; + public ArtifactResolutionRequest() { // nothing here @@ -78,6 +80,7 @@ public class ArtifactResolutionRequest setLocalRepository( request.getLocalRepository() ); setRemoteRepositories( request.getRemoteRepositories() ); setCache( request.getCache() ); + setOffline( request.isOffline() ); } public Artifact getArtifact() @@ -230,4 +233,16 @@ public class ArtifactResolutionRequest return this; } + public boolean isOffline() + { + return offline; + } + + public ArtifactResolutionRequest setOffline( boolean offline ) + { + this.offline = offline; + + return this; + } + } diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index 5caef4921b..8922e952b7 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -162,7 +162,7 @@ public class DefaultArtifactResolver destination = artifact.getFile(); - if ( force || !destination.exists() || ( artifact.isSnapshot() && !localCopy ) ) + if ( ( force || !destination.exists() || ( artifact.isSnapshot() && !localCopy ) ) && !request.isOffline() ) { try { @@ -175,12 +175,6 @@ public class DefaultArtifactResolver { wagonManager.getArtifact( artifact, remoteRepositories, downloadMonitor ); } - - if ( !artifact.isResolved() && !destination.exists() ) - { - throw new ArtifactResolutionException( "Failed to resolve artifact, possibly due to a repository list that is not appropriately equipped for this artifact's metadata.", - artifact, remoteRepositories ); - } } catch ( ResourceDoesNotExistException e ) { @@ -196,6 +190,20 @@ public class DefaultArtifactResolver { artifact.setResolved( true ); } + else + { + if ( request.isOffline() ) + { + throw new ArtifactResolutionException( "The repository system is offline" + + " and the requested artifact is not locally available", artifact, remoteRepositories ); + } + else + { + throw new ArtifactResolutionException( "Failed to resolve artifact, possibly due to a " + + "repository list that is not appropriately equipped for this artifact's metadata.", artifact, + remoteRepositories ); + } + } // 1.0-SNAPSHOT // diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java index 1f5d3acbca..91564bc457 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/DefaultMetadataResolutionRequest.java @@ -19,6 +19,7 @@ package org.apache.maven.repository.legacy.metadata; * under the License. */ +import java.util.ArrayList; import java.util.List; import org.apache.maven.artifact.Artifact; @@ -45,6 +46,8 @@ public class DefaultMetadataResolutionRequest private boolean resolveManagedVersions; + private boolean offline; + public DefaultMetadataResolutionRequest() { // does nothing @@ -55,6 +58,7 @@ public class DefaultMetadataResolutionRequest setLocalRepository( request.getLocalRepository() ); setRemoteRepositories( request.getRemoteRepositories() ); setCache( request.getCache() ); + setOffline( request.isOffline() ); } public Artifact getArtifact() @@ -83,6 +87,11 @@ public class DefaultMetadataResolutionRequest public List getRemoteRepositories() { + if ( remoteRepositories == null ) + { + remoteRepositories = new ArrayList(); + } + return remoteRepositories; } @@ -98,9 +107,10 @@ public class DefaultMetadataResolutionRequest return resolveManagedVersions; } - public MetadataResolutionRequest setResolveManagedVersions( boolean resolveManagedVersions ) + public DefaultMetadataResolutionRequest setResolveManagedVersions( boolean resolveManagedVersions ) { this.resolveManagedVersions = resolveManagedVersions; + return this; } @@ -109,11 +119,23 @@ public class DefaultMetadataResolutionRequest return cache; } - public MetadataResolutionRequest setCache( RepositoryCache cache ) + public DefaultMetadataResolutionRequest setCache( RepositoryCache cache ) { this.cache = cache; return this; } + public boolean isOffline() + { + return offline; + } + + public DefaultMetadataResolutionRequest setOffline( boolean offline ) + { + this.offline = offline; + + return this; + } + } diff --git a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java index c3c7625942..803057bd84 100644 --- a/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java +++ b/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/MetadataResolutionRequest.java @@ -23,6 +23,7 @@ import java.util.List; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.RepositoryCache; import org.apache.maven.artifact.repository.RepositoryRequest; /** @@ -34,6 +35,21 @@ public interface MetadataResolutionRequest extends RepositoryRequest { + /** + * Indicates whether network access to remote repositories has been disabled. + * + * @return {@code true} if remote access has been disabled, {@code false} otherwise. + */ + boolean isOffline(); + + /** + * Enables/disables network access to remote repositories. + * + * @param offline {@code true} to disable remote access, {@code false} to allow network access. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setOffline( boolean offline ); + /** * Gets the artifact to resolve metadata for. * @@ -67,18 +83,33 @@ public interface MetadataResolutionRequest /** * Gets the remote repositories to use for the resolution. * - * @return The remote repositories to use for the resolution or {@code null} if not set. + * @return The remote repositories to use for the resolution, never {@code null}. */ List getRemoteRepositories(); /** - * Sets the remote repository to use for the resolution. + * Sets the remote repositories to use for the resolution. * - * @param remoteRepositories The remote repository to use for the resolution. + * @param remoteRepositories The remote repositories to use for the resolution. * @return This request, never {@code null}. */ MetadataResolutionRequest setRemoteRepositories( List remoteRepositories ); + /** + * Gets the repository cache to use. + * + * @return The repository cache to use or {@code null} if none. + */ + RepositoryCache getCache(); + + /** + * Sets the repository cache to use. + * + * @param cache The repository cache to use, may be {@code null}. + * @return This request, never {@code null}. + */ + MetadataResolutionRequest setCache( RepositoryCache cache ); + /** * Determines whether the managed version information should be retrieved. * diff --git a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java index 0226d34af4..945a258780 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java +++ b/maven-core/src/main/java/org/apache/maven/execution/DefaultMavenExecutionRequest.java @@ -898,6 +898,7 @@ public class DefaultMavenExecutionRequest projectBuildingRequest.setRemoteRepositories( getRemoteRepositories() ); projectBuildingRequest.setPluginArtifactRepositories( getPluginArtifactRepositories() ); projectBuildingRequest.setRepositoryCache( getRepositoryCache() ); + projectBuildingRequest.setOffline( isOffline() ); projectBuildingRequest.setActiveProfileIds( getActiveProfiles() ); projectBuildingRequest.setInactiveProfileIds( getInactiveProfiles() ); projectBuildingRequest.setProfiles( getProfiles() ); diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java index 7fbd61c685..9c5fe85233 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java @@ -156,9 +156,7 @@ public class DefaultLifecycleExecutor MavenExecutionResult result = session.getResult(); - RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); - repositoryRequest.setLocalRepository( session.getLocalRepository() ); - repositoryRequest.setCache( session.getRepositoryCache() ); + RepositoryRequest repositoryRequest = getRepositoryRequest( session, null ); for ( MavenProject currentProject : session.getProjects() ) { @@ -366,7 +364,11 @@ public class DefaultLifecycleExecutor request.setCache( session.getRepositoryCache() ); request.setLocalRepository( session.getLocalRepository() ); - request.setRemoteRepositories( project.getPluginArtifactRepositories() ); + if ( project != null ) + { + request.setRemoteRepositories( project.getPluginArtifactRepositories() ); + } + request.setOffline( session.isOffline() ); return request; } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 12c4ec3737..9b60ece0fd 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -422,6 +422,7 @@ public class DefaultPluginManager request.setLocalRepository( localRepository ); request.setRemoteRepositories( remoteRepositories ); request.setCache( session.getRepositoryCache() ); + request.setOffline( session.isOffline() ); pluginArtifacts = getPluginArtifacts( pluginArtifact, plugin, request ); } catch ( ArtifactNotFoundException e ) diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java index fe9a4c9dc8..6da7d4ed94 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuilder.java @@ -167,6 +167,7 @@ public class DefaultProjectBuilder RepositoryRequest repositoryRequest = new DefaultRepositoryRequest(); repositoryRequest.setCache( configuration.getRepositoryCache() ); repositoryRequest.setLocalRepository( configuration.getLocalRepository() ); + repositoryRequest.setOffline( configuration.isOffline() ); ModelResolver resolver = new RepositoryModelResolver( repositorySystem, resolutionErrorHandler, repositoryRequest, @@ -504,6 +505,7 @@ public class DefaultProjectBuilder repositoryRequest.setLocalRepository( configuration.getLocalRepository() ); repositoryRequest.setRemoteRepositories( project.getPluginArtifactRepositories() ); repositoryRequest.setCache( configuration.getRepositoryCache() ); + repositoryRequest.setOffline( configuration.isOffline() ); lifecycle.populateDefaultConfigurationForPlugins( model.getBuild().getPlugins(), repositoryRequest ); } diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java index f466f45b2e..22d3ccb612 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultProjectBuildingRequest.java @@ -34,6 +34,8 @@ public class DefaultProjectBuildingRequest implements ProjectBuildingRequest { + private boolean offline; + private RepositoryCache repositoryCache; private ArtifactRepository localRepository; @@ -84,6 +86,18 @@ public class DefaultProjectBuildingRequest this.topProject = mavenProject; } + public DefaultProjectBuildingRequest setOffline( boolean offline ) + { + this.offline = offline; + + return this; + } + + public boolean isOffline() + { + return offline; + } + public ProjectBuildingRequest setRepositoryCache( RepositoryCache repositoryCache ) { this.repositoryCache = repositoryCache; diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java index a05a0801e5..1a6c512c5c 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingRequest.java @@ -33,6 +33,10 @@ import org.apache.maven.wagon.events.TransferListener; public interface ProjectBuildingRequest { + ProjectBuildingRequest setOffline( boolean offline ); + + boolean isOffline(); + ProjectBuildingRequest setRepositoryCache( RepositoryCache repositoryCache ); RepositoryCache getRepositoryCache(); diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index 6e59138a95..7db7de3b72 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -469,6 +469,7 @@ public class MavenMetadataSource configuration.setRepositoryCache( repositoryRequest.getCache() ); configuration.setLocalRepository( repositoryRequest.getLocalRepository() ); configuration.setRemoteRepositories( repositoryRequest.getRemoteRepositories() ); + configuration.setOffline( repositoryRequest.isOffline() ); configuration.setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL ); configuration.setProcessPlugins( false ); configuration.setSystemProperties( System.getProperties() );