Search in sources :

Example 56 with ProjectBuildingRequest

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

the class RemoteCatalogArchetypeDataSourceTest method testRemoteCatalog.

public void testRemoteCatalog() throws Exception {
    ArchetypeManager archetype = lookup(ArchetypeManager.class);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(newMavenSession(new MavenProjectStub()));
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    buildingRequest.getRemoteRepositories().add(new MavenArtifactRepository("central", "http://localhost:" + port + "/repo/", new DefaultRepositoryLayout(), null, null));
    ArchetypeCatalog result = archetype.getRemoteCatalog(buildingRequest);
    assertEquals(1, result.getArchetypes().size());
    assertEquals("groupId", result.getArchetypes().get(0).getGroupId());
    assertEquals("artifactId", result.getArchetypes().get(0).getArtifactId());
    assertEquals("1", result.getArchetypes().get(0).getVersion());
    assertEquals("http://localhost:" + port + "/repo/", result.getArchetypes().get(0).getRepository());
}
Also used : ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenProjectStub(org.apache.maven.plugin.testing.stubs.MavenProjectStub) DefaultRepositoryLayout(org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 57 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest 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 58 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest 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)

Example 59 with ProjectBuildingRequest

use of org.apache.maven.project.ProjectBuildingRequest in project yeoman-maven-plugin by trecloux.

the class YeomanMojoTest method getMavenProject.

private MavenProject getMavenProject(String pomPath) throws Exception {
    File pom = new File(pomPath);
    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setPom(pom);
    ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
    return lookup(ProjectBuilder.class).build(pom, configuration).getProject();
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) File(java.io.File)

Example 60 with ProjectBuildingRequest

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

the class DeployFileMojoTest method testDeployIfClassifierIsSet.

public void testDeployIfClassifierIsSet() throws Exception {
    File testPom = new File(getBasedir(), "target/test-classes/unit/deploy-file-classifier/plugin-config.xml");
    mojo = (DeployFileMojo) lookupMojo("deploy-file", testPom);
    MockitoAnnotations.initMocks(this);
    assertNotNull(mojo);
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(LOCAL_REPO));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    String classifier = (String) getVariableValueFromObject(mojo, "classifier");
    String groupId = (String) getVariableValueFromObject(mojo, "groupId");
    String artifactId = (String) getVariableValueFromObject(mojo, "artifactId");
    String version = (String) getVariableValueFromObject(mojo, "version");
    assertEquals("bin", classifier);
    mojo.execute();
    File deployedArtifact = new File(remoteRepo, "deploy-file-classifier/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "-" + classifier + ".jar");
    assertTrue(deployedArtifact.exists());
    mojo.setClassifier("prod");
    assertEquals("prod", mojo.getClassifier());
    mojo.execute();
    File prodDeployedArtifact = new File(remoteRepo, "deploy-file-classifier/" + groupId.replace('.', '/') + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + "-" + mojo.getClassifier() + ".jar");
    assertTrue(prodDeployedArtifact.exists());
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Aggregations

ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)81 File (java.io.File)40 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)37 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)28 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)28 Artifact (org.apache.maven.artifact.Artifact)25 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)22 MavenProject (org.apache.maven.project.MavenProject)22 MavenSession (org.apache.maven.execution.MavenSession)17 IOException (java.io.IOException)14 LegacySupport (org.apache.maven.plugin.LegacySupport)13 ArtifactResolverException (org.apache.maven.shared.artifact.resolve.ArtifactResolverException)12 ArrayList (java.util.ArrayList)9 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)8 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)8 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)7 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)7 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)7 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)7 ArtifactResult (org.apache.maven.shared.artifact.resolve.ArtifactResult)7