diff --git a/maven-embedder/notes.txt b/maven-embedder/notes.txt new file mode 100644 index 0000000000..0a9766ffaa --- /dev/null +++ b/maven-embedder/notes.txt @@ -0,0 +1,4 @@ + -- parse xml to in-memory model + -- run m2 (embedded): including running for downloading dependencies, building, archetypes, etc. + -- introspect available goals + -- introspect available plugins (low priority) diff --git a/maven-embedder/pom.xml b/maven-embedder/pom.xml new file mode 100644 index 0000000000..18c038bb1d --- /dev/null +++ b/maven-embedder/pom.xml @@ -0,0 +1,154 @@ + + + maven + org.apache.maven + 2.0-beta-2-SNAPSHOT + + 4.0.0 + org.apache.maven + maven-embedder + Maven Embedder + 2.0-beta-2-SNAPSHOT + + + + maven-assembly-plugin + RELEASE + + src/assemble/bin.xml + maven-2.0-beta-1 + + + + + + + org.apache.maven + maven-settings + 2.0-beta-2-SNAPSHOT + + + org.apache.maven.wagon + wagon-file + 1.0-alpha-4 + runtime + + + org.apache.maven.wagon + wagon-http-lightweight + 1.0-alpha-4 + runtime + + + org.apache.maven.reporting + maven-reporting-api + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-profile + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-model + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-artifact + 2.0-beta-2-SNAPSHOT + + + plexus + plexus-container-default + + + org.apache.maven.wagon + wagon-provider-api + 1.0-alpha-4 + + + org.apache.maven + maven-repository-metadata + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-project + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-plugin-registry + 2.0-beta-2-SNAPSHOT + + + commons-cli + commons-cli + 1.0 + + + org.apache.maven + maven-plugin-api + 2.0-beta-2-SNAPSHOT + + + plexus + plexus-utils + + + plexus + plexus-input-handler + 1.0-alpha-2 + + + org.apache.maven + maven-plugin-descriptor + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-monitor + 2.0-beta-2-SNAPSHOT + + + org.apache.maven + maven-artifact-manager + 2.0-beta-2-SNAPSHOT + + + org.apache.maven.wagon + wagon-ssh + 1.0-alpha-4 + runtime + + + jsch + jsch + 0.1.21 + + + + + + maven-checkstyle-plugin + + + maven-clover-plugin + + + maven-pmd-plugin + + + maven-project-info-reports-plugin + + + + + + website + scp://minotaur.apache.org//www/maven.apache.org/m2 + + + 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 new file mode 100644 index 0000000000..55da836828 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedder.java @@ -0,0 +1,339 @@ +package org.apache.maven.embedder; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; +import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.repository.DefaultArtifactRepository; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.apache.maven.profiles.ProfileManager; +import org.apache.maven.profiles.DefaultProfileManager; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectBuilder; +import org.apache.maven.project.ProjectBuildingException; +import org.apache.maven.settings.MavenSettingsBuilder; +import org.apache.maven.settings.Settings; +import org.apache.maven.settings.RuntimeInfo; +import org.codehaus.classworlds.ClassWorld; +import org.codehaus.classworlds.DuplicateRealmException; +import org.codehaus.plexus.PlexusContainerException; +import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; +import org.codehaus.plexus.embed.Embedder; +import org.codehaus.plexus.util.xml.pull.XmlPullParserException; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; + +/** + * Class intended to be used by clients who wish to embed Maven into their applications + * + * @author Jason van Zyl + */ +public class MavenEmbedder +{ + // ---------------------------------------------------------------------- + // Embedder + // ---------------------------------------------------------------------- + + private Embedder embedder; + + // ---------------------------------------------------------------------- + // Components + // ---------------------------------------------------------------------- + + private MavenProjectBuilder mavenProjectBuilder; + + private ArtifactRepositoryFactory artifactRepositoryFactory; + + private MavenSettingsBuilder settingsBuilder; + + private MavenXpp3Reader modelReader; + + private ProfileManager profileManager; + + // ---------------------------------------------------------------------- + // Configuration + // ---------------------------------------------------------------------- + + private Settings settings; + + private ArtifactRepository localRepository; + + private File localRepositoryDirectory; + + private ClassLoader classLoader; + + // ---------------------------------------------------------------------- + // User options + // ---------------------------------------------------------------------- + + private boolean pluginUpdateOverride; + + private boolean checkLatestPluginVersion; + + private boolean interactiveMode; + + private boolean usePluginRegistry; + + private boolean offline; + + private boolean updateSnapshots; + + private String globalChecksumPolicy; + + public void setPluginUpdateOverride( boolean pluginUpdateOverride ) + { + this.pluginUpdateOverride = pluginUpdateOverride; + } + + public void setCheckLatestPluginVersion( boolean checkLatestPluginVersion ) + { + this.checkLatestPluginVersion = checkLatestPluginVersion; + } + + public void setInteractiveMode( boolean interactiveMode ) + { + this.interactiveMode = interactiveMode; + } + + public void setUsePluginRegistry( boolean usePluginRegistry ) + { + this.usePluginRegistry = usePluginRegistry; + } + + public void setOffline( boolean offline ) + { + this.offline = offline; + } + + public void setUpdateSnapshots( boolean updateSnapshots ) + { + this.updateSnapshots = updateSnapshots; + } + + public void setGlobalChecksumPolicy( String globalChecksumPolicy ) + { + this.globalChecksumPolicy = globalChecksumPolicy; + } + + // ---------------------------------------------------------------------- + // Accessors + // ---------------------------------------------------------------------- + + /** + * Set the classloader to use with the maven embedder. + * + * @param classLoader + */ + public void setClassLoader( ClassLoader classLoader ) + { + this.classLoader = classLoader; + } + + public void setLocalRepositoryDirectory( File localRepositoryDirectory ) + { + this.localRepositoryDirectory = localRepositoryDirectory; + } + // ---------------------------------------------------------------------- + // Embedder Client Contract + // ---------------------------------------------------------------------- + + public Model readModel( File model ) + throws XmlPullParserException, FileNotFoundException, IOException + { + return modelReader.read( new FileReader( model ) ); + } + + public MavenProject readProject( File mavenProject ) + throws ProjectBuildingException + { + return mavenProjectBuilder.build( mavenProject, localRepository, profileManager ); + } + + // ---------------------------------------------------------------------- + // Internal utility code + // ---------------------------------------------------------------------- + + private ArtifactRepository createLocalRepository( Settings settings ) + throws ComponentLookupException + { + ArtifactRepositoryLayout repositoryLayout = (ArtifactRepositoryLayout) embedder.lookup( ArtifactRepositoryLayout.ROLE, "default" ); + + String url = settings.getLocalRepository(); + + if ( !url.startsWith( "file:" ) ) + { + url = "file://" + url; + } + + ArtifactRepository localRepository = new DefaultArtifactRepository( "local", url, repositoryLayout ); + + boolean snapshotPolicySet = false; + + if ( offline ) + { + settings.setOffline( true ); + + snapshotPolicySet = true; + } + + if ( !snapshotPolicySet && updateSnapshots ) + { + artifactRepositoryFactory.setGlobalUpdatePolicy( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS ); + } + + artifactRepositoryFactory.setGlobalChecksumPolicy( globalChecksumPolicy ); + + return localRepository; + } + + private RuntimeInfo createRuntimeInfo( Settings settings ) + { + RuntimeInfo runtimeInfo = new RuntimeInfo( settings ); + + if ( pluginUpdateOverride ) + { + runtimeInfo.setPluginUpdateOverride( Boolean.TRUE ); + } + else + { + runtimeInfo.setPluginUpdateOverride( Boolean.FALSE ); + } + + if ( checkLatestPluginVersion ) + { + runtimeInfo.setCheckLatestPluginVersion( Boolean.TRUE ); + } + else + { + runtimeInfo.setCheckLatestPluginVersion( Boolean.FALSE ); + } + + return runtimeInfo; + } + + // ---------------------------------------------------------------------- + // Lifecycle + // ---------------------------------------------------------------------- + + public void start() + throws MavenEmbedderException + { + if ( classLoader == null ) + { + throw new IllegalStateException( "A classloader must be specified using setClassLoader(ClassLoader)." ); + } + + embedder = new Embedder(); + + try + { + ClassWorld classWorld = new ClassWorld(); + + classWorld.newRealm( "plexus.core", classLoader ); + + embedder.start( classWorld ); + + // ---------------------------------------------------------------------- + // Lookup each of the components we need to provide the desired + // client interface. + // ---------------------------------------------------------------------- + + modelReader = new MavenXpp3Reader(); + + profileManager = new DefaultProfileManager( embedder.getContainer() ); + + mavenProjectBuilder = (MavenProjectBuilder) embedder.lookup( MavenProjectBuilder.ROLE ); + + artifactRepositoryFactory = (ArtifactRepositoryFactory) embedder.lookup( ArtifactRepositoryFactory.ROLE ); + + // ---------------------------------------------------------------------- + // If an explicit local repository has not been set then we will use the + // setting builder to use the maven defaults to help us find one. + // ---------------------------------------------------------------------- + + if ( localRepositoryDirectory == null ) + { + settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE ); + + try + { + settings = settingsBuilder.buildSettings(); + } + catch ( IOException e ) + { + throw new MavenEmbedderException( "Error creating settings.", e ); + } + catch ( XmlPullParserException e ) + { + throw new MavenEmbedderException( "Error creating settings.", e ); + } + } + else + { + settings = new Settings(); + + settings.setLocalRepository( localRepositoryDirectory.getAbsolutePath() ); + } + + settings.setRuntimeInfo( createRuntimeInfo( settings ) ); + + settings.setOffline( offline ); + + settings.setUsePluginRegistry( usePluginRegistry ); + + settings.setInteractiveMode( interactiveMode ); + + localRepository = createLocalRepository( settings ); + } + catch ( PlexusContainerException e ) + { + throw new MavenEmbedderException( "Cannot start Plexus embedder.", e ); + } + catch ( DuplicateRealmException e ) + { + throw new MavenEmbedderException( "Cannot create Classworld realm for the embedder.", e ); + } + catch ( ComponentLookupException e ) + { + throw new MavenEmbedderException( "Cannot lookup required component.", e ); + } + } + + public void stop() + throws MavenEmbedderException + { + try + { + embedder.release( mavenProjectBuilder ); + + embedder.release( artifactRepositoryFactory ); + + embedder.release( settingsBuilder ); + } + catch ( ComponentLifecycleException e ) + { + throw new MavenEmbedderException( "Cannot stop the embedder.", e ); + } + } +} diff --git a/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderException.java b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderException.java new file mode 100644 index 0000000000..07649a238b --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/embedder/MavenEmbedderException.java @@ -0,0 +1,40 @@ +package org.apache.maven.embedder; + +/* +* Copyright 2001-2005 The Apache Software Foundation. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/** + * @author Jason van Zyl + * @version $Id:$ + */ +public class MavenEmbedderException + extends Exception +{ + public MavenEmbedderException( String message ) + { + super( message ); + } + + public MavenEmbedderException( Throwable cause ) + { + super( cause ); + } + + public MavenEmbedderException( String message, Throwable cause ) + { + super( message, cause ); + } +} diff --git a/maven-embedder/src/main/resources/META-INF/plexus/components.xml b/maven-embedder/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000000..76f97efcb0 --- /dev/null +++ b/maven-embedder/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,376 @@ + + + + org.apache.maven.plugin.PluginManager + org.apache.maven.plugin.DefaultPluginManager + + + org.apache.maven.project.path.PathTranslator + + + org.apache.maven.plugin.MavenPluginCollector + + + org.apache.maven.plugin.version.PluginVersionManager + + + org.apache.maven.artifact.factory.ArtifactFactory + + + org.apache.maven.artifact.resolver.ArtifactResolver + + + org.apache.maven.artifact.metadata.ArtifactMetadataSource + + + org.apache.maven.plugin.PluginMappingManager + + + org.apache.maven.execution.RuntimeInformation + + + org.apache.maven.project.MavenProjectBuilder + + + + + + org.apache.maven.extension.ExtensionManager + org.apache.maven.extension.DefaultExtensionManager + + + org.apache.maven.artifact.resolver.ArtifactResolver + + + org.apache.maven.artifact.metadata.ArtifactMetadataSource + + + + + + org.apache.maven.plugin.PluginMappingManager + org.apache.maven.plugin.DefaultPluginMappingManager + + + org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager + + + + + + + org.apache.maven.project.path.PathTranslator + org.apache.maven.project.path.DefaultPathTranslator + + + + org.apache.maven.Maven + org.apache.maven.DefaultMaven + + + org.apache.maven.project.MavenProjectBuilder + + + org.apache.maven.lifecycle.LifecycleExecutor + + + org.apache.maven.usability.ErrorDiagnoser + errorDiagnosers + + + org.apache.maven.execution.RuntimeInformation + + + + + + org.apache.maven.execution.RuntimeInformation + org.apache.maven.execution.DefaultRuntimeInformation + + + + org.apache.maven.usability.ErrorDiagnoser + PluginConfigurationDiagnoser + org.apache.maven.usability.PluginConfigurationDiagnoser + + + + org.apache.maven.usability.ErrorDiagnoser + ArtifactResolverDiagnoser + org.apache.maven.usability.ArtifactResolverDiagnoser + + + + org.apache.maven.usability.ErrorDiagnoser + InvalidArtifactDiagnoser + org.apache.maven.usability.InvalidArtifactDiagnoser + + + + + org.apache.maven.lifecycle.LifecycleExecutor + org.apache.maven.lifecycle.DefaultLifecycleExecutor + + + org.apache.maven.plugin.PluginManager + + + org.apache.maven.extension.ExtensionManager + + + org.apache.maven.project.injection.ModelDefaultsInjector + + + org.apache.maven.artifact.handler.manager.ArtifactHandlerManager + + + + + + validate + initialize + generate-sources + process-sources + generate-resources + process-resources + compile + process-classes + generate-test-sources + process-test-sources + generate-test-resources + process-test-resources + test-compile + test + package + integration-test + verify + install + deploy + + + + + org.apache.maven.plugins:maven-project-info-reports-plugin + org.apache.maven.plugins:maven-checkstyle-plugin + org.apache.maven.plugins:maven-javadoc-plugin + + + + + + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + pom + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + jar + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-resources-plugin:resources + org.apache.maven.plugins:maven-compiler-plugin:compile + org.apache.maven.plugins:maven-resources-plugin:testResources + org.apache.maven.plugins:maven-compiler-plugin:testCompile + org.apache.maven.plugins:maven-surefire-plugin:test + + org.apache.maven.plugins:maven-jar-plugin:jar + + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + maven-plugin + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-plugin-plugin:descriptor + org.apache.maven.plugins:maven-resources-plugin:resources + org.apache.maven.plugins:maven-compiler-plugin:compile + org.apache.maven.plugins:maven-resources-plugin:testResources + org.apache.maven.plugins:maven-compiler-plugin:testCompile + org.apache.maven.plugins:maven-surefire-plugin:test + + org.apache.maven.plugins:maven-jar-plugin:jar, + org.apache.maven.plugins:maven-plugin-plugin:addPluginArtifactMetadata + + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + ejb + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-resources-plugin:resources + org.apache.maven.plugins:maven-compiler-plugin:compile + org.apache.maven.plugins:maven-resources-plugin:testResources + org.apache.maven.plugins:maven-compiler-plugin:testCompile + org.apache.maven.plugins:maven-surefire-plugin:test + + org.apache.maven.plugins:maven-ejb-plugin:ejb + + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + war + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-resources-plugin:resources + org.apache.maven.plugins:maven-compiler-plugin:compile + org.apache.maven.plugins:maven-resources-plugin:testResources + org.apache.maven.plugins:maven-compiler-plugin:testCompile + org.apache.maven.plugins:maven-surefire-plugin:test + org.apache.maven.plugins:maven-war-plugin:war + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + ear + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-ear-plugin:generate-application-xml + org.apache.maven.plugins:maven-ear-plugin:ear + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + rar + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + org.apache.maven.plugins:maven-resources-plugin:resources + org.apache.maven.plugins:maven-compiler-plugin:compile + org.apache.maven.plugins:maven-resources-plugin:testResources + org.apache.maven.plugins:maven-compiler-plugin:testCompile + org.apache.maven.plugins:maven-surefire-plugin:test + + org.apache.maven.plugins:maven-jar-plugin:jar, + org.apache.maven.plugins:maven-rar-plugin:rar + + org.apache.maven.plugins:maven-install-plugin:install + org.apache.maven.plugins:maven-deploy-plugin:deploy + + + + + + + org.apache.maven.plugin.version.PluginVersionManager + default + org.apache.maven.plugin.version.DefaultPluginVersionManager + per-lookup + + + org.apache.maven.artifact.metadata.ArtifactMetadataSource + + + org.apache.maven.plugin.registry.MavenPluginRegistryBuilder + + + org.apache.maven.artifact.factory.ArtifactFactory + + + org.codehaus.plexus.components.inputhandler.InputHandler + + + org.apache.maven.project.MavenProjectBuilder + + + org.apache.maven.execution.RuntimeInformation + + + + + diff --git a/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java new file mode 100644 index 0000000000..e84e5c47af --- /dev/null +++ b/maven-embedder/src/test/java/org/apache/maven/embedder/MavenEmbedderTest.java @@ -0,0 +1,70 @@ +package org.apache.maven.embedder; + +import junit.framework.TestCase; +import org.apache.maven.model.Model; +import org.apache.maven.project.MavenProject; + +import java.io.File; + +public class MavenEmbedderTest + extends TestCase +{ + private String basedir; + + private MavenEmbedder maven; + + protected void setUp() + throws Exception + { + super.setUp(); + + basedir = System.getProperty( "basedir" ); + } + + public void testMavenEmbedder() + throws Exception + { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + + maven = new MavenEmbedder(); + + maven.setClassLoader( classLoader ); + + maven.start(); + + modelReadingTest(); + + projectReadingTest(); + + maven.stop(); + } + + protected void modelReadingTest() + throws Exception + { + // ---------------------------------------------------------------------- + // Test model reading + // ---------------------------------------------------------------------- + + Model model = maven.readModel( getPomFile() ); + + assertEquals( "org.apache.maven", model.getGroupId() ); + } + + protected void projectReadingTest() + throws Exception + { + MavenProject project = maven.readProject( getPomFile() ); + + assertEquals( "org.apache.maven", project.getGroupId() ); + } + + // ---------------------------------------------------------------------- + // Internal Utilities + // ---------------------------------------------------------------------- + + protected File getPomFile() + { + return new File( basedir, "src/test/resources/pom.xml" ); + } +} diff --git a/maven-embedder/src/test/resources/pom.xml b/maven-embedder/src/test/resources/pom.xml new file mode 100644 index 0000000000..e3f75178a9 --- /dev/null +++ b/maven-embedder/src/test/resources/pom.xml @@ -0,0 +1,70 @@ + + + maven + org.apache.maven + 2.0-beta-2-SNAPSHOT + + 4.0.0 + org.apache.maven + maven-model + Maven Model + 2.0-beta-2-SNAPSHOT + Maven Model + + + + org.codehaus.modello + modello-maven-plugin + + + + xpp3-writer + java + xpp3-reader + xsd + + + + + 4.0.0 + maven.mdo + + + + + + + plexus + plexus-utils + + + + + all-models + + ${pom.artifactId}-${pom.version}-all + + + org.codehaus.modello + modello-maven-plugin + + + v3 + + 3.0.0 + true + + + xpp3-writer + java + xpp3-reader + xsd + + + + + + + + +