From da776815ac6567f4c5eebb990a69c8572a74e308 Mon Sep 17 00:00:00 2001 From: Jason van Zyl Date: Mon, 9 Mar 2009 04:51:13 +0000 Subject: [PATCH] o a test which demonstrates the difficulting in testing the execution of a single mojo, indicates some work to isolate the system and make it easier for people to reuse this code. igor had to jump through these same hoops to get isolated mojo execution working in m2e. needs some love. git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@751582 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/maven/execution/MavenSession.java | 26 ++++- .../lifecycle/DefaultLifecycleExecutor.java | 2 +- .../maven/plugin/DefaultPluginManager.java | 7 +- .../lifecycle/LifecycleExecutorTest.java | 92 ++++++++++++++++++ .../plan/DefaultBuildPlannerTest.xml | 28 ------ .../test-custom-lifecycle-buildPlan-1.jar | Bin 2240 -> 0 bytes .../apache/maven/embedder/MavenEmbedder.java | 3 +- .../repository/LegacyRepositorySystem.java | 43 +++++--- .../maven/repository/RepositorySystem.java | 5 + 9 files changed, 153 insertions(+), 53 deletions(-) create mode 100644 maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java delete mode 100644 maven-core/src/test/resources/org/apache/maven/lifecycle/plan/DefaultBuildPlannerTest.xml delete mode 100644 maven-core/src/test/resources/org/apache/maven/lifecycle/plan/test-custom-lifecycle-buildPlan-1.jar diff --git a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java index f5b2cbc359..df1b141b0a 100644 --- a/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java +++ b/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java @@ -63,6 +63,17 @@ public class MavenSession private Map reports = new LinkedHashMap(); + //For testing + private ArtifactRepository localRepository; + + private MavenRealmManager realmManager; + + public MavenSession( ArtifactRepository localRepository, MavenRealmManager realmManager ) + { + this.localRepository = localRepository; + this.realmManager = realmManager; + } + public MavenSession( PlexusContainer container, MavenExecutionRequest request, EventDispatcher eventDispatcher, ReactorManager reactorManager ) { this.container = container; @@ -76,11 +87,15 @@ public class MavenSession public MavenRealmManager getRealmManager() { + if ( realmManager != null ) + { + return realmManager; + } + return request.getRealmManager(); } - public Map getPluginContext( PluginDescriptor pluginDescriptor, - MavenProject project ) + public Map getPluginContext( PluginDescriptor pluginDescriptor, MavenProject project ) { return reactorManager.getPluginContext( pluginDescriptor, project ); } @@ -89,9 +104,14 @@ public class MavenSession { return container; } - + public ArtifactRepository getLocalRepository() { + if ( localRepository != null ) + { + return localRepository; + } + return request.getLocalRepository(); } 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 0b1e70c762..c9e8be7079 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 @@ -927,7 +927,7 @@ public class DefaultLifecycleExecutor return lifecycle; } - private MojoDescriptor getMojoDescriptor( PluginDescriptor pluginDescriptor, String goal ) + MojoDescriptor getMojoDescriptor( PluginDescriptor pluginDescriptor, String goal ) throws LifecycleExecutionException { MojoDescriptor desc = pluginDescriptor.getMojo( goal ); 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 ca8872fd96..79258446b2 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 @@ -258,7 +258,7 @@ public class DefaultPluginManager try { pluginRealm = realmManager.createPluginRealm( projectPlugin, pluginArtifact, artifacts, coreArtifactFilterManager.getArtifactFilter() ); - + logger.debug( "Created realm: " + pluginRealm + " for plugin: " + projectPlugin.getKey() ); } catch ( RealmManagementException e ) @@ -288,7 +288,7 @@ public class DefaultPluginManager logger.debug( "Checking for plugin descriptor for: " + projectPlugin.getKey() + " with version: " + projectPlugin.getVersion() + " in collector: " + pluginCollector ); PluginDescriptor pluginDescriptor = pluginCollector.getPluginDescriptor( projectPlugin ); - + if ( pluginDescriptor == null ) { if ( ( pluginRealm != null ) && logger.isDebugEnabled() ) @@ -1825,8 +1825,7 @@ public class DefaultPluginManager // Plugin Mapping Manager - public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories, - ArtifactRepository localRepository ) + public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories, ArtifactRepository localRepository ) { // if not found, try from the remote repository if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) ) diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java new file mode 100644 index 0000000000..ad65fbd0ca --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java @@ -0,0 +1,92 @@ +package org.apache.maven.lifecycle; + +import java.io.File; + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.MavenPluginCollector; +import org.apache.maven.plugin.MavenPluginDiscoverer; +import org.apache.maven.plugin.PluginManager; +import org.apache.maven.plugin.descriptor.MojoDescriptor; +import org.apache.maven.plugin.descriptor.PluginDescriptor; +import org.apache.maven.project.DefaultProjectBuilderConfiguration; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuilderConfiguration; +import org.apache.maven.realm.DefaultMavenRealmManager; +import org.apache.maven.realm.MavenRealmManager; +import org.apache.maven.repository.RepositorySystem; +import org.codehaus.plexus.ContainerConfiguration; +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.logging.console.ConsoleLogger; + +public class LifecycleExecutorTest + extends PlexusTestCase +{ + private MavenProjectBuilder projectBuilder; + + private RepositorySystem repositorySystem; + + private PluginManager pluginManager; + + private DefaultLifecycleExecutor lifecycleExecutor; + + protected void setUp() + throws Exception + { + projectBuilder = lookup( MavenProjectBuilder.class ); + repositorySystem = lookup( RepositorySystem.class ); + pluginManager = lookup( PluginManager.class ); + lifecycleExecutor = (DefaultLifecycleExecutor) lookup( LifecycleExecutor.class ); + } + + public void testMojoExecution() + throws Exception + { + // - find the plugin [extension point: any client may wish to do whatever they choose] + // - load the plugin into a classloader [extension point: we want to take them from a repository, some may take from disk or whatever] + // - configure the plugin [extension point] + // - execute the plugin + + File pom = new File( getBasedir(), "src/test/pom.xml" ); + + // For testing I want to use my standard local repository and settings. + + ArtifactRepository localRepository = repositorySystem.createLocalRepository( new File( "/Users/jvanzyl/.m2/repository" ) ); + + ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration() + .setLocalRepository( localRepository ) + .setRemoteRepositories( null ); + + MavenProject project = projectBuilder.build( pom, configuration ); + + // now i want to grab the configuration for the remote resources plugin + + assertEquals( "maven", project.getArtifactId() ); + + Plugin plugin = new Plugin(); + plugin.setGroupId( "org.apache.maven.plugins" ); + plugin.setArtifactId( "maven-remote-resources-plugin" ); + // The version should be specified in the POM. + + MavenRealmManager realmManager = new DefaultMavenRealmManager( getContainer(), new ConsoleLogger( 0, "logger" ) ); + MavenSession session = new MavenSession( localRepository, realmManager ); + + PluginDescriptor pd = pluginManager.loadPlugin( plugin, project, session ); + assertNotNull( pd ); + assertEquals( "org.apache.maven.plugins", pd.getGroupId() ); + assertEquals( "maven-remote-resources-plugin", pd.getArtifactId() ); + assertEquals( "1.0", pd.getVersion() ); + + MojoDescriptor mojoDescriptor = pd.getMojo( "process" ); + assertNotNull( mojoDescriptor ); + System.out.println( "configuration >>> " + mojoDescriptor.getConfiguration() ); + } + + protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration ) + { + containerConfiguration.addComponentDiscoverer( new MavenPluginDiscoverer() ); + containerConfiguration.addComponentDiscoveryListener( new MavenPluginCollector() ); + } +} diff --git a/maven-core/src/test/resources/org/apache/maven/lifecycle/plan/DefaultBuildPlannerTest.xml b/maven-core/src/test/resources/org/apache/maven/lifecycle/plan/DefaultBuildPlannerTest.xml deleted file mode 100644 index a6ce99fff7..0000000000 --- a/maven-core/src/test/resources/org/apache/maven/lifecycle/plan/DefaultBuildPlannerTest.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - org.apache.maven.plugin.PluginLoader - default - org.apache.maven.lifecycle.plan.testutils.TestPluginLoader - - - diff --git a/maven-core/src/test/resources/org/apache/maven/lifecycle/plan/test-custom-lifecycle-buildPlan-1.jar b/maven-core/src/test/resources/org/apache/maven/lifecycle/plan/test-custom-lifecycle-buildPlan-1.jar deleted file mode 100644 index 5c9bef6e8d570dee04975a110c4a424d49ba3e8b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2240 zcmWIWW@h1H0D(=>&E{YRl;8x?zOEsTx}JV+`TRsgZ*3~+9=KSU$gDb`lo)+nNojal9t?R_W{$xqm6fx}s zDiu5DbO#B02L+eR)mkee!!0z~I7qZc_;k=`pp2JsvDbk|k(wtvkFK05uuvp+{gj*u zKbKXpePnyKCuUC!1I%TZ9uPowSwT)}MQJgTKS18q6>FYr&d9*v$-=-OhedUAer`d2 zUTR)Rv0g=P&e|!Cc`XJ!uHS8s#xQJ>S6s3xw&dcwD#mUJ579{)hKq}Svk56o@R~bu z*JJzJ|ErI6th(5IkxNvRZ)M%Cm{q+yB9|#-_^+83k$rn=!h)9@WgfJxV)0(o+IC7# zWO~Nw=N$K%*4;3gK1*cphixr=PKGbGI`gtBg;o|9a#Pt7{% zsL`HvC+lt&x3#anQ>&^Om9xz4+%1WPyFS&b$}|4?J@?aziWN3L4|?aG%WK#4d$~tr zeWk}Y&p#n1wmTO4&j{>uIQ1u7(z0w{MQq5yiypS3jB(AM6bpakNWH4BJofUc$*HcR zFCYKXxqp9u?N9lM4&P5`PxG*!zT(5fAB>0y+KiUM_>qG=H?b@=4<0`Z4CsorFcs$) zrRyaYBqnF1>Vb9ZCFd8V>XoDxm*6qSizIVEhUq4k7MJAb>gHsor6yM<=cMW;m1gFo z1mq+l+{?kx2lU)bre{k&0nI-ROjChWwWlCI7nB5hC!Y2@Y#`wJ{b!d(wSCmCE}w5x zqXJh7C57{zGb#P3ylF0begEVD4Q`gxJBy#4@lcL>zfL3BPAd0sa$)C2T}k5$i$10(PBk%qylDD4 zAI->AK&`UkeU$l^yq6eDWV#?LEdSJkI0dWM?1Gu0lzaX`! zBr~;G^o%c8vw?ua!&*PX2Udz&OG666HDftizcD>d+0r<}qmCv2{;rx2A6Kzg_szI@ zpyWV@b#-9x!NYSoOSnt#nr>gW_rQmL4vW8rzLA;CyKntbA?KU(mhl$`?oh0aT>IQ1 zuw#wUavg&-Yvrf=t)hcGE!SMSIrF8$@gl8jGp9T2*9CYpGKnzbE|!5l2Lb_xw~inh zsgQxim)GB@e0$wr13o#-Kd2v zvP;m50#LaD0U(zoF(J7GR*JwJg1sC;Xzs_L701;By$OuGBA6wmoqS% zmo(ZlV*~~s{}8elTIwL;0(&k*Sib;Rk-}32BrYhl9+4*lyjj^mHgN)>DA1czSwTDi DzqRC% 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 b7575cc34f..59e208ff82 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 @@ -482,7 +482,8 @@ public class MavenEmbedder ContainerConfiguration cc = new DefaultContainerConfiguration() .addComponentDiscoverer( new MavenPluginDiscoverer() ) .addComponentDiscoveryListener( new MavenPluginCollector() ) - .setClassWorld( classWorld ).setName( "embedder" ); + .setClassWorld( classWorld ) + .setName( "embedder" ); container = new DefaultPlexusContainer( cc ); } diff --git a/maven-repository/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java index 112e63af55..bb029a3bbf 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/LegacyRepositorySystem.java @@ -17,6 +17,7 @@ package org.apache.maven.repository; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -96,7 +97,7 @@ public class LegacyRepositorySystem private Map authenticationInfoMap = new HashMap(); private Map serverPermissionsMap = new HashMap(); - + // Artifact Creation public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type ) @@ -171,17 +172,17 @@ public class LegacyRepositorySystem /** * @return {@link Set} < {@link Artifact} > - * @todo desperately needs refactoring. It's just here because it's implementation is maven-project specific + * @todo desperately needs refactoring. It's just here because it's implementation is + * maven-project specific */ - public Set createArtifacts( List dependencies, String inheritedScope, - ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor ) + public Set createArtifacts( List dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor ) throws VersionNotFoundException { return createArtifacts( artifactFactory, dependencies, inheritedScope, dependencyFilter, reactor ); } @Deprecated - public static Set createArtifacts( ArtifactFactory artifactFactory,List dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor ) + public static Set createArtifacts( ArtifactFactory artifactFactory, List dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor ) throws VersionNotFoundException { Set projectArtifacts = new LinkedHashSet( dependencies.size() ); @@ -208,9 +209,7 @@ public class LegacyRepositorySystem { throw new VersionNotFoundException( reactor.getId(), d, reactor.getFile(), e ); } - Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), - versionRange, d.getType(), d.getClassifier(), - scope, inheritedScope, d.isOptional() ); + Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), scope, inheritedScope, d.isOptional() ); if ( Artifact.SCOPE_SYSTEM.equals( scope ) ) { @@ -257,8 +256,8 @@ public class LegacyRepositorySystem } return projectArtifacts; - } - + } + public ArtifactRepository buildArtifactRepository( Repository repo ) throws InvalidRepositoryException { @@ -319,6 +318,19 @@ public class LegacyRepositorySystem // From MavenExecutionRequestPopulator + public ArtifactRepository createLocalRepository( File localRepository ) + throws InvalidRepositoryException + { + try + { + return createRepository( localRepository.toURI().toURL().toString(), "maven.repo.local" ); + } + catch ( MalformedURLException e ) + { + throw new InvalidRepositoryException( "Error creating local repository.", "maven.repo.local", e ); + } + } + public ArtifactRepository createLocalRepository( String url, String repositoryId ) throws IOException { @@ -373,7 +385,7 @@ public class LegacyRepositorySystem } public ArtifactResolutionResult resolve( ArtifactResolutionRequest request ) - { + { return artifactResolver.resolve( request ); } @@ -400,8 +412,7 @@ public class LegacyRepositorySystem proxies.put( protocol, proxyInfo ); } - public void addAuthenticationInfo( String repositoryId, String username, String password, String privateKey, - String passphrase ) + public void addAuthenticationInfo( String repositoryId, String username, String password, String privateKey, String passphrase ) { AuthenticationInfo authInfo = new AuthenticationInfo(); authInfo.setUserName( username ); @@ -437,14 +448,14 @@ public class LegacyRepositorySystem serverPermissionsMap.put( repositoryId, permissions ); } } - + // Mirror - + public void addMirror( String id, String mirrorOf, String url ) { mirrorBuilder.addMirror( id, mirrorOf, url ); } - + public List getMirrors( List repositories ) { return mirrorBuilder.getMirrors( repositories ); diff --git a/maven-repository/src/main/java/org/apache/maven/repository/RepositorySystem.java b/maven-repository/src/main/java/org/apache/maven/repository/RepositorySystem.java index 8b400c5a59..f47d07ec27 100644 --- a/maven-repository/src/main/java/org/apache/maven/repository/RepositorySystem.java +++ b/maven-repository/src/main/java/org/apache/maven/repository/RepositorySystem.java @@ -15,6 +15,7 @@ package org.apache.maven.repository; * the License. */ +import java.io.File; import java.io.IOException; import java.util.List; import java.util.Set; @@ -61,9 +62,13 @@ public interface RepositorySystem ArtifactRepository buildArtifactRepository( Repository repository ) throws InvalidRepositoryException; + //!!jvz Change this to use a file ArtifactRepository createLocalRepository( String url, String repositoryId ) throws IOException; + ArtifactRepository createLocalRepository( File localRepository ) + throws InvalidRepositoryException; + ArtifactResolutionResult resolve( ArtifactResolutionRequest request ); //MetadataResolutionResult resolveMetadata( MetadataResolutionRequest request );