Search in sources :

Example 16 with ProjectBuildingRequest

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

the class ArchetypeGenerationTest method testProjectGenerationFromAnArchetype.

public void testProjectGenerationFromAnArchetype() throws Exception {
    ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
    // In the embedder the localRepository will be retrieved from the embedder itself and users won't
    // have to go through this muck.
    ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target/test-classes/repositories/local").toURI().toURL().toExternalForm(), "local-repo");
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager("target/test-classes/repositories/central"));
    buildingRequest.setRepositorySession(repositorySession);
    ArchetypeCatalog catalog = archetype.getLocalCatalog(buildingRequest);
    System.err.println("archetypes => " + catalog.getArchetypes());
    // Here I am just grabbing a OldArchetype but in a UI you would take the OldArchetype objects and present
    // them to the user.
    Archetype selection = (Archetype) catalog.getArchetypes().get(catalog.getArchetypes().size() - 1);
    System.err.println("Selected OldArchetype = " + selection);
    // Now you will present a dialog, or whatever, and grab the following values.
    String groupId = "com.mycompany";
    String artifactId = "app";
    String version = "1.0.0";
    String packageName = "org.mycompany.app";
    // With the selected OldArchetype and the parameters you can create a generation request as follows:
    File outputDirectory = new File(getBasedir(), "target/test-classes/projects/archetyper-generate-1");
    FileUtils.forceDelete(outputDirectory);
    ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest(selection).setOutputDirectory(outputDirectory.getAbsolutePath()).setLocalRepository(localRepository).setGroupId(groupId).setArtifactId(artifactId).setVersion(version).setPackage(packageName);
    Properties archetypeRequiredProperties = new Properties();
    archetypeRequiredProperties.setProperty("property-with-default-1", "value-1");
    archetypeRequiredProperties.setProperty("property-with-default-2", "value-2");
    archetypeRequiredProperties.setProperty("property-with-default-3", "value-3");
    archetypeRequiredProperties.setProperty("property-with-default-4", "value-4");
    archetypeRequiredProperties.setProperty("property-without-default-1", "some-value-1");
    archetypeRequiredProperties.setProperty("property-without-default-2", "some-value-2");
    archetypeRequiredProperties.setProperty("property-without-default-3", "some-value-3");
    archetypeRequiredProperties.setProperty("property-without-default-4", "some-value-4");
    archetypeRequiredProperties.setProperty("property_underscored_1", "prop1");
    archetypeRequiredProperties.setProperty("property_underscored-2", "prop2");
    agr.setProperties(archetypeRequiredProperties);
    agr.setProjectBuildingRequest(buildingRequest);
    // Then generate away!
    ArchetypeGenerationResult result = archetype.generateProjectFromArchetype(agr);
    if (result.getCause() != null) {
        result.getCause().printStackTrace(System.err);
        fail(result.getCause().getMessage());
    }
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) Properties(java.util.Properties) ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 17 with ProjectBuildingRequest

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

the class InternalCatalogArchetypesVerificationTest method testInternalCatalog.

public void testInternalCatalog() throws Exception {
    ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target/test-classes/repositories/local").toURI().toURL().toExternalForm(), "local-repo");
    File outputDirectory = new File(getBasedir(), "target/internal-archetypes-projects");
    outputDirectory.mkdirs();
    FileUtils.cleanDirectory(outputDirectory);
    ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.class.getName());
    ArchetypeCatalog catalog = archetype.getInternalCatalog();
    int count = 1;
    for (Archetype a : catalog.getArchetypes()) {
        Archetype ar = new Archetype();
        ar.setGroupId(a.getGroupId());
        ar.setArtifactId(a.getArtifactId());
        ar.setVersion(a.getVersion());
        ar.setDescription(a.getDescription());
        ar.setGoals(a.getGoals());
        ar.setProperties(a.getProperties());
        ar.setRepository(a.getRepository());
        if (ar.getRepository() == null) {
            ar.setRepository(CENTRAL);
        }
        ArchetypeGenerationRequest request = new ArchetypeGenerationRequest(ar).setGroupId("org.apache.maven.archetype.test").setArtifactId("archetype" + count).setVersion("1.0-SNAPSHOT").setPackage("com.acme").setOutputDirectory(outputDirectory.getPath()).setLocalRepository(localRepository);
        ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
        MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
        repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
        buildingRequest.setRepositorySession(repositorySession);
        request.setProjectBuildingRequest(buildingRequest);
        ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(request);
        assertTrue("Archetype wasn't generated successfully", generationResult.getCause() == null);
        count++;
    }
}
Also used : ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) Archetype(org.apache.maven.archetype.catalog.Archetype) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 18 with ProjectBuildingRequest

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

the class AbstractChangesReport method getSkinArtifactFile.

private File getSkinArtifactFile() throws MojoExecutionException {
    Skin skin = Skin.getDefaultSkin();
    DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
    coordinate.setGroupId(skin.getGroupId());
    coordinate.setArtifactId(skin.getArtifactId());
    coordinate.setVersion(skin.getVersion());
    ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(mavenSession.getProjectBuildingRequest());
    pbr.setRemoteRepositories(project.getRemoteArtifactRepositories());
    try {
        return resolver.resolveArtifact(pbr, coordinate).getArtifact().getFile();
    } catch (ArtifactResolverException e) {
        throw new MojoExecutionException("Couldn't resolve the skin.", e);
    }
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultArtifactCoordinate(org.apache.maven.shared.artifact.DefaultArtifactCoordinate) Skin(org.apache.maven.doxia.site.decoration.Skin) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 19 with ProjectBuildingRequest

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

the class JavadocReportTest method testHelpfile.

/**
 * Method to test the <code>&lt;helpfile/&gt;</code> parameter.
 *
 * @throws Exception if any
 */
public void testHelpfile() throws Exception {
    File testPom = new File(unit, "helpfile-test/pom.xml");
    JavadocReport mojo = lookupMojo(testPom);
    assertNotNull(mojo);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    File apidocs = new File(getBasedir(), "target/test/unit/helpfile-test/target/site/apidocs");
    File helpfile = new File(apidocs, "help-doc.html");
    File options = new File(apidocs, "options");
    // helpfile by default
    mojo.execute();
    String content = readFile(helpfile);
    assertTrue(content.contains("<!-- Generated by javadoc"));
    String optionsContent = readFile(options);
    assertFalse(optionsContent.contains("-helpfile"));
    // helpfile defined in a javadoc plugin dependency
    setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile/help-doc.html");
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from artefact -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    File help = new File(apidocs, "help-doc.html");
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // helpfile defined as a project resource
    setVariableValueToObject(mojo, "helpfile", "com/mycompany/app/javadoc/helpfile2/help-doc.html");
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from file -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    help = new File(unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
    // helpfile defined as file
    help = new File(unit, "helpfile-test/src/main/resources/com/mycompany/app/javadoc/helpfile2/help-doc.html");
    setVariableValueToObject(mojo, "helpfile", help.getAbsolutePath());
    mojo.execute();
    content = readFile(helpfile);
    assertTrue(content.contains("<!--  Help file from file -->"));
    optionsContent = readFile(options);
    assertTrue(optionsContent.contains("-helpfile"));
    assertTrue(optionsContent.contains("'" + help.getAbsolutePath().replaceAll("\\\\", "/") + "'"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File)

Example 20 with ProjectBuildingRequest

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

the class JavadocReportTest method testDoclets.

/**
 * Method to test the doclet artifact configuration
 *
 * @throws Exception if any
 */
public void testDoclets() throws Exception {
    // ----------------------------------------------------------------------
    // doclet-test: check if the file generated by UmlGraph exists and if
    // doclet path contains the UmlGraph artifact
    // ----------------------------------------------------------------------
    File testPom = new File(unit, "doclet-test/doclet-test-plugin-config.xml");
    JavadocReport mojo = lookupMojo(testPom);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    File generatedFile = new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    File optionsFile = new File(mojo.getOutputDirectory(), "options");
    assertTrue(optionsFile.exists());
    String options = readFile(optionsFile);
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar"));
    // ----------------------------------------------------------------------
    // doclet-path: check if the file generated by UmlGraph exists and if
    // doclet path contains the twice UmlGraph artifacts
    // ----------------------------------------------------------------------
    testPom = new File(unit, "doclet-path-test/doclet-path-test-plugin-config.xml");
    mojo = lookupMojo(testPom);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    generatedFile = new File(getBasedir(), "target/test/unit/doclet-test/target/site/apidocs/graph.dot");
    assertTrue(FileUtils.fileExists(generatedFile.getAbsolutePath()));
    optionsFile = new File(mojo.getOutputDirectory(), "options");
    assertTrue(optionsFile.exists());
    options = readFile(optionsFile);
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph/2.1/UMLGraph-2.1.jar"));
    assertTrue(options.contains("/target/local-repo/umlgraph/UMLGraph-bis/2.1/UMLGraph-bis-2.1.jar"));
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) LegacySupport(org.apache.maven.plugin.LegacySupport) 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