Search in sources :

Example 11 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project gradle by gradle.

the class MavenProjectsCreator method createNow.

private Set<MavenProject> createNow(Settings settings, File pomFile) throws PlexusContainerException, ComponentLookupException, MavenExecutionRequestPopulationException, ProjectBuildingException {
    ContainerConfiguration containerConfiguration = new DefaultContainerConfiguration().setClassWorld(new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader())).setName("mavenCore").setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true);
    DefaultPlexusContainer container = new DefaultPlexusContainer(containerConfiguration);
    ProjectBuilder builder = container.lookup(ProjectBuilder.class);
    MavenExecutionRequest executionRequest = new DefaultMavenExecutionRequest();
    final Properties properties = SystemProperties.getInstance().withSystemProperties(() -> {
        final Properties currentProperties = new Properties();
        currentProperties.putAll(System.getProperties());
        return currentProperties;
    });
    executionRequest.setSystemProperties(properties);
    MavenExecutionRequestPopulator populator = container.lookup(MavenExecutionRequestPopulator.class);
    populateFromSettings(settings, executionRequest, populator);
    populator.populateDefaults(executionRequest);
    ProjectBuildingRequest buildingRequest = executionRequest.getProjectBuildingRequest();
    buildingRequest.getRemoteRepositories().forEach(repository -> {
        if (repository.getId().equals(RepositorySystem.DEFAULT_REMOTE_REPO_ID)) {
            repository.setUrl(RepositoryHandler.MAVEN_CENTRAL_URL);
        }
    });
    buildingRequest.setProcessPlugins(false);
    MavenProject mavenProject = builder.build(pomFile, buildingRequest).getProject();
    Set<MavenProject> reactorProjects = new LinkedHashSet<>();
    // TODO adding the parent project first because the converter needs it this way ATM. This is oversimplified.
    // the converter should not depend on the order of reactor projects.
    // we should add coverage for nested multi-project builds with multiple parents.
    reactorProjects.add(mavenProject);
    List<ProjectBuildingResult> allProjects = builder.build(ImmutableList.of(pomFile), true, buildingRequest);
    // noinspection NullableProblems
    CollectionUtils.collect(allProjects, reactorProjects, ProjectBuildingResult::getProject);
    MavenExecutionResult result = new DefaultMavenExecutionResult();
    result.setProject(mavenProject);
    RepositorySystemSession repoSession = new DefaultRepositorySystemSession();
    @SuppressWarnings("deprecation") MavenSession session = new MavenSession(container, repoSession, executionRequest, result);
    session.setCurrentProject(mavenProject);
    return reactorProjects;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) DefaultMavenExecutionResult(org.apache.maven.execution.DefaultMavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) Properties(java.util.Properties) SystemProperties(org.gradle.internal.SystemProperties) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 12 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project spring-cloud-function by spring-cloud.

the class DependencyResolutionModule method initialize.

private void initialize() {
    if (this.container == null) {
        synchronized (lock) {
            if (this.container == null) {
                ClassWorld classWorld = new ClassWorld("plexus.core", Thread.currentThread().getContextClassLoader());
                ContainerConfiguration config = new DefaultContainerConfiguration().setClassWorld(classWorld).setRealm(classWorld.getClassRealm("plexus.core")).setClassPathScanning(PlexusConstants.SCANNING_INDEX).setAutoWiring(true).setName("maven");
                PlexusContainer container;
                try {
                    container = new DefaultPlexusContainer(config, new AetherModule(), new DependencyResolutionModule());
                    localRepositoryManagerFactory = container.lookup(LocalRepositoryManagerFactory.class);
                    container.addComponent(new ClassRealmManager((MutablePlexusContainer) container, new DefaultBeanLocator()), ClassRealmManager.class.getName());
                    projectBuilder = container.lookup(ProjectBuilder.class);
                    repositorySystem = container.lookup(RepositorySystem.class);
                } catch (Exception e) {
                    throw new IllegalStateException("Cannot create container", e);
                }
                this.container = container;
                this.settings = new MavenSettingsReader().readSettings();
            }
        }
    }
}
Also used : ClassRealmManager(org.eclipse.sisu.plexus.ClassRealmManager) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) MutablePlexusContainer(org.codehaus.plexus.MutablePlexusContainer) PlexusContainer(org.codehaus.plexus.PlexusContainer) AetherModule(org.eclipse.aether.impl.guice.AetherModule) DefaultBeanLocator(org.eclipse.sisu.inject.DefaultBeanLocator) MutablePlexusContainer(org.codehaus.plexus.MutablePlexusContainer) ClassWorld(org.codehaus.plexus.classworlds.ClassWorld) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) NoLocalRepositoryManagerException(org.eclipse.aether.repository.NoLocalRepositoryManagerException) IOException(java.io.IOException) RepositorySystem(org.eclipse.aether.RepositorySystem) LocalRepositoryManagerFactory(org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) ProjectBuilder(org.apache.maven.project.ProjectBuilder)

Example 13 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project maven-plugins by apache.

the class ModuleSetAssemblyPhaseTest method createPhase.

private ModuleSetAssemblyPhase createPhase(final Logger logger, final MockAndControlForAddDependencySetsTask macTask) {
    ProjectBuilder projectBuilder = null;
    if (macTask != null) {
        projectBuilder = macTask.projectBuilder;
    }
    DependencyResolver dr = EasyMock.createMock(DependencyResolver.class);
    return new ModuleSetAssemblyPhase(projectBuilder, dr, logger);
}
Also used : ProjectBuilder(org.apache.maven.project.ProjectBuilder) DependencyResolver(org.apache.maven.plugins.assembly.artifact.DependencyResolver)

Example 14 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project maven-archetype by apache.

the class ArchetyperRoundtripWithProxyIT method testArchetyper.

public void testArchetyper() throws Exception {
    ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "local").toURI().toURL().toExternalForm(), "local-repo");
    ArtifactRepository centralRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "central").toURI().toURL().toExternalForm(), "central-repo");
    // (1) create a project from scratch
    // (2) create an archetype from the project
    // (3) create our own archetype catalog properties in memory
    // (4) create our own archetype catalog describing the archetype we just created
    // (5) deploy the archetype we just created
    // (6) create a project form the archetype we just created
    // ------------------------------------------------------------------------
    // 
    // ------------------------------------------------------------------------
    // (1) create a project from scratch
    // File sourceProject = new File( getBasedir(  ), "target/test-classes/projects/roundtrip-1-project" );
    File workingProject = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-2-project");
    FileUtils.forceDelete(new File(workingProject, "target"));
    // (2) create an archetype from the project
    File pom = new File(workingProject, "pom.xml");
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
    buildingRequest.setRepositorySession(repositorySession);
    MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
    ArchetypeCreationRequest acr = new ArchetypeCreationRequest().setProject(project).setLocalRepository(localRepository).setPostPhase("package");
    ArchetypeCreationResult creationResult = archetype.createArchetypeFromProject(acr);
    if (creationResult.getCause() != null) {
        throw creationResult.getCause();
    }
    // (3) create our own archetype catalog properties in memory
    File catalogDirectory = new File(getBasedir(), "target" + File.separator + "catalog");
    File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
    File catalogProperties = new File(catalogDirectory, "archetype-catalog.properties");
    catalogDirectory.mkdirs();
    Properties p = new Properties();
    p.setProperty("sources", "catalog");
    p.setProperty("catalog.file", catalogFile.getAbsolutePath());
    OutputStream os = new FileOutputStream(catalogProperties);
    p.store(os, "Generated catalog properties");
    // (5) install the archetype we just created
    File generatedArchetypeDirectory = new File(project.getBasedir(), "target" + File.separator + "generated-sources" + File.separator + "archetype");
    File generatedArchetypePom = new File(generatedArchetypeDirectory, "pom.xml");
    MavenProject generatedArchetypeProject = projectBuilder.build(generatedArchetypePom, buildingRequest).getProject();
    ModelInterpolator modelInterpolator = (ModelInterpolator) lookup(ModelInterpolator.ROLE);
    Model generatedArchetypeModel = modelInterpolator.interpolate(generatedArchetypeProject.getModel(), generatedArchetypePom.getParentFile(), new DefaultProjectBuilderConfiguration(), true);
    File archetypeDirectory = new File(generatedArchetypeDirectory, "src" + File.separator + "main" + File.separator + "resources");
    File archetypeArchive = archetype.archiveArchetype(archetypeDirectory, new File(generatedArchetypeModel.getBuild().getDirectory()), generatedArchetypeModel.getBuild().getFinalName());
    String baseName = StringUtils.replace(generatedArchetypeProject.getGroupId(), ".", File.separator) + File.separator + generatedArchetypeProject.getArtifactId() + File.separator + generatedArchetypeProject.getVersion() + File.separator + generatedArchetypeModel.getBuild().getFinalName();
    File archetypeInRepository = new File(centralRepository.getBasedir(), baseName + ".jar");
    File archetypePomInRepository = new File(centralRepository.getBasedir(), baseName + ".pom");
    archetypeInRepository.getParentFile().mkdirs();
    FileUtils.copyFile(archetypeArchive, archetypeInRepository);
    FileUtils.copyFile(generatedArchetypePom, archetypePomInRepository);
    // (4) create our own archetype catalog describing the archetype we just created
    ArchetypeCatalog catalog = new ArchetypeCatalog();
    Archetype generatedArchetype = new Archetype();
    generatedArchetype.setGroupId(generatedArchetypeProject.getGroupId());
    generatedArchetype.setArtifactId(generatedArchetypeProject.getArtifactId());
    generatedArchetype.setVersion(generatedArchetypeProject.getVersion());
    generatedArchetype.setRepository("http://localhost:" + port + "/repo");
    catalog.addArchetype(generatedArchetype);
    ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
    Writer writer = new FileWriter(catalogFile);
    catalogWriter.write(writer, catalog);
    IOUtils.closeQuietly(writer);
    // (6) create a project form the archetype we just created
    String outputDirectory = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-2-recreatedproject").getAbsolutePath();
    FileUtils.forceDelete(outputDirectory);
    // WagonManager manager = (WagonManager) lookup( WagonManager.class.getName() );
    // manager.addProxy( "http", "localhost", proxyPort, null, null, null );
    ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest().setArchetypeGroupId(generatedArchetypeProject.getGroupId()).setArchetypeArtifactId(generatedArchetypeProject.getArtifactId()).setArchetypeVersion(generatedArchetypeProject.getVersion()).setGroupId("com.mycompany").setArtifactId("myapp").setVersion("1.0-SNAPSHOT").setPackage("com.mycompany.myapp").setOutputDirectory(outputDirectory).setLocalRepository(localRepository).setArchetypeRepository("http://localhost:" + port + "/repo").setProjectBuildingRequest(buildingRequest);
    ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(agr);
    if (generationResult.getCause() != null) {
        throw generationResult.getCause();
    }
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) FileWriter(java.io.FileWriter) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) ArchetypeCreationRequest(org.apache.maven.archetype.ArchetypeCreationRequest) ModelInterpolator(org.apache.maven.project.interpolation.ModelInterpolator) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) ArchetypeCreationResult(org.apache.maven.archetype.ArchetypeCreationResult) ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuilder(org.apache.maven.project.ProjectBuilder) FileOutputStream(java.io.FileOutputStream) Model(org.apache.maven.model.Model) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) FileWriter(java.io.FileWriter) Writer(java.io.Writer) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)

Example 15 with ProjectBuilder

use of org.apache.maven.project.ProjectBuilder in project swagger-core by swagger-api.

the class BetterAbstractMojoTestCase method lookupConfiguredMojo.

/**
 * As {@link #lookupConfiguredMojo(MavenProject, String)} but taking the pom file
 * and creating the {@link MavenProject}.
 */
protected Mojo lookupConfiguredMojo(File pom, String goal) throws Exception {
    assertNotNull(pom);
    assertTrue(pom.exists());
    this.getContainer().addComponent(new IncludeProjectDependenciesComponentConfigurator(), org.codehaus.plexus.component.configurator.ComponentConfigurator.class, "include-project-dependencies");
    ProjectBuildingRequest buildingRequest = newMavenSession().getProjectBuildingRequest();
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
    return lookupConfiguredMojo(project, goal);
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject)

Aggregations

ProjectBuilder (org.apache.maven.project.ProjectBuilder)15 MavenProject (org.apache.maven.project.MavenProject)11 ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)8 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)6 File (java.io.File)5 Properties (java.util.Properties)5 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)5 IOException (java.io.IOException)4 ArchetypeCreationRequest (org.apache.maven.archetype.ArchetypeCreationRequest)4 ArchetypeCreationResult (org.apache.maven.archetype.ArchetypeCreationResult)4 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)4 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)4 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)4 FileWriter (java.io.FileWriter)3 Writer (java.io.Writer)3 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)3 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)3 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)3 Archetype (org.apache.maven.archetype.catalog.Archetype)3 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)3