From a1b2df1fd13586b9fe64d54c90ce2c0390430800 Mon Sep 17 00:00:00 2001 From: Milos Kleint Date: Sat, 6 May 2006 07:42:19 +0000 Subject: [PATCH] MNG-1979 throw IllegalStateException when calling methods in wrong order. git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@400260 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/embedder/MavenEmbedder.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java index 00efc76e02..376087a778 100644 --- a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -151,6 +151,8 @@ public class MavenEmbedder * @deprecated not used */ private boolean alignWithUserInstallation; + + private boolean started = false; // ---------------------------------------------------------------------- // Accessors @@ -254,11 +256,11 @@ public class MavenEmbedder } /** - * @deprecated not used. + * */ public File getLocalRepositoryDirectory() { - return localRepositoryDirectory; + return new File(getLocalRepositoryPath(settings)); } public ArtifactRepository getLocalRepository() @@ -284,15 +286,25 @@ public class MavenEmbedder // Model // ---------------------------------------------------------------------- + /** + * read the model. + * requires a start()-ed embedder. + */ public Model readModel( File model ) throws XmlPullParserException, FileNotFoundException, IOException { + checkStarted(); return modelReader.read( new FileReader( model ) ); } + /** + * write the model. + * requires a start()-ed embedder. + */ public void writeModel( Writer writer, Model model ) throws IOException { + checkStarted(); modelWriter.write( writer, model ); } @@ -300,27 +312,35 @@ public class MavenEmbedder // Project // ---------------------------------------------------------------------- + /** + * read the project. + * requires a start()-ed embedder. + */ public MavenProject readProject( File mavenProject ) throws ProjectBuildingException { + checkStarted(); return mavenProjectBuilder.build( mavenProject, localRepository, profileManager ); } public MavenProject readProjectWithDependencies( File mavenProject, TransferListener transferListener ) throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException { + checkStarted(); return mavenProjectBuilder.buildWithDependencies( mavenProject, localRepository, profileManager, transferListener ); } public MavenProject readProjectWithDependencies( File mavenProject ) throws ProjectBuildingException, ArtifactResolutionException, ArtifactNotFoundException { + checkStarted(); return mavenProjectBuilder.buildWithDependencies( mavenProject, localRepository, profileManager ); } public List collectProjects( File basedir, String[] includes, String[] excludes ) throws MojoExecutionException { + checkStarted(); List projects = new ArrayList(); List poms = getPomFiles( basedir, includes, excludes ); @@ -351,17 +371,20 @@ public class MavenEmbedder public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) { + checkStarted(); return artifactFactory.createArtifact( groupId, artifactId, version, scope, type ); } public Artifact createArtifactWithClassifier( String groupId, String artifactId, String version, String type, String classifier ) { + checkStarted(); return artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, type, classifier ); } public void resolve( Artifact artifact, List remoteRepositories, ArtifactRepository localRepository ) throws ArtifactResolutionException, ArtifactNotFoundException { + checkStarted(); artifactResolver.resolve( artifact, remoteRepositories, localRepository ); } @@ -415,6 +438,7 @@ public class MavenEmbedder public List getLifecyclePhases() throws MavenEmbedderException { + checkStarted(); List phases = new ArrayList(); ComponentDescriptor descriptor = embedder.getContainer().getComponentDescriptor( LifecycleExecutor.ROLE ); @@ -473,6 +497,7 @@ public class MavenEmbedder public ArtifactRepository createRepository( String url, String repositoryId ) { + checkStarted(); // snapshots vs releases // offline = to turning the update policy off @@ -606,6 +631,8 @@ public class MavenEmbedder null ); profileManager.loadSettingsProfiles( settings ); + + started = true; localRepository = createLocalRepository( settings ); @@ -636,6 +663,7 @@ public class MavenEmbedder public void stop() throws MavenEmbedderException { + started = false; try { embedder.release( mavenProjectBuilder ); @@ -657,6 +685,7 @@ public class MavenEmbedder public void execute( MavenExecutionRequest request ) throws MavenExecutionException { + checkStarted(); maven.execute( request ); } @@ -668,6 +697,7 @@ public class MavenEmbedder Boolean pluginUpdateOverride ) throws SettingsConfigurationException { + checkStarted(); return mavenTools.buildSettings( userSettingsPath, globalSettingsPath, interactive, @@ -681,6 +711,7 @@ public class MavenEmbedder Boolean pluginUpdateOverride ) throws SettingsConfigurationException { + checkStarted(); return mavenTools.buildSettings( userSettingsPath, globalSettingsPath, pluginUpdateOverride ); @@ -689,16 +720,25 @@ public class MavenEmbedder public File getUserSettingsPath( String optionalSettingsPath ) { + checkStarted(); return mavenTools.getUserSettingsPath( optionalSettingsPath ); } public File getGlobalSettingsPath() { + checkStarted(); return mavenTools.getGlobalSettingsPath(); } public String getLocalRepositoryPath( Settings settings ) { + checkStarted(); return mavenTools.getLocalRepositoryPath( settings ); } + + private void checkStarted() { + if (!started) { + throw new IllegalStateException("The embedder is not started, you need to call start() on the embedder prior to calling this method"); + } + } }