Search in sources :

Example 11 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGeneratorTest method testGenerateFileSetArchetype.

public void testGenerateFileSetArchetype() throws Exception {
    System.out.println("testGenerateFileSetArchetype");
    ArchetypeGenerationRequest request = createArchetypeGenerationRequest("generate-12", ARCHETYPE_FILESET);
    File projectFile = new File(projectDirectory, "pom.xml");
    FileUtils.forceDelete(projectDirectory);
    generateProjectFromArchetype(request);
    assertTemplateContentGeneratedWithFileSetArchetype("src/main/java/file/value/package/App.java", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype("src/main/java/file/value/package/inner/package/App2.java", "file-value");
    assertTemplateCopiedWithFileSetArchetype("src/main/java/file/value/package/App.ogg");
    assertTemplateContentGeneratedWithFileSetArchetype("src/main/resources/App.properties", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype("src/main/resources/file-value/touch.txt", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype("src/main/resources/file-value/touch_root.txt", "file-value");
    assertTemplateCopiedWithFileSetArchetype("src/main/resources/some-dir/App.png");
    assertTemplateContentGeneratedWithFileSetArchetype("src/site/site.xml", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype("src/site/apt/usage.apt", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype(".classpath", "file-value");
    assertTemplateContentGeneratedWithFileSetArchetype("profiles.xml", "file-value");
    Model model = readPom(projectFile);
    assertNull(model.getParent());
    assertEquals("file-value", model.getGroupId());
    assertEquals("file-value", model.getArtifactId());
    assertEquals("file-value", model.getVersion());
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/src/main/java/file/value/package/App.java", "subproject");
    model = readPom(new File(projectDirectory, "subproject/pom.xml"));
    assertNotNull(model.getParent());
    assertEquals("file-value", model.getParent().getGroupId());
    assertEquals("file-value", model.getParent().getArtifactId());
    assertEquals("file-value", model.getParent().getVersion());
    assertEquals("file-value", model.getGroupId());
    assertEquals("subproject", model.getArtifactId());
    assertEquals("file-value", model.getVersion());
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/subsubproject/src/main/java/file/value/package/App.java", "subsubproject");
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/subsubproject/src/main/java/file/value/package/" + "file-value/inner/subsubproject/innest/Somefile-valueClasssubsubproject.java", "subsubproject");
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/subsubproject/src/main/java/file/value/package/" + /* + "file-value/inner/subsubproject/innest/" + */
    "ArbitraryProperty-file-value.java", "subsubproject");
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/subsubproject/src/main/java/file/value/package/" + /* + "file-value/inner/subsubproject/innest/" + */
    "M_subsubproject_prop1Testprop2file-value.java", "subsubproject");
    // Test that undefined properties are safely ignored (and skipped)
    assertTemplateContentGeneratedWithFileSetArchetype("subproject/subsubproject/src/main/java/file/value/package/" + /* + "file-value/inner/subsubproject/innest/" + */
    "SkipsUndefinedProperty-__undefined-property__-file-value.java", "subsubproject");
    model = readPom(new File(projectDirectory, "subproject/subsubproject/pom.xml"));
    assertNotNull(model.getParent());
    assertEquals("file-value", model.getParent().getGroupId());
    assertEquals("subproject", model.getParent().getArtifactId());
    assertEquals("file-value", model.getParent().getVersion());
    assertEquals("file-value", model.getGroupId());
    assertEquals("subsubproject", model.getArtifactId());
    assertEquals("file-value", model.getVersion());
}
Also used : ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Model(org.apache.maven.model.Model) File(java.io.File)

Example 12 with ArchetypeGenerationRequest

use of org.apache.maven.archetype.ArchetypeGenerationRequest 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 13 with ArchetypeGenerationRequest

use of org.apache.maven.archetype.ArchetypeGenerationRequest 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 14 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGenerationConfigurator2Test method testJIRA_509_FileSetArchetypeDefaultsWithVariables.

public void testJIRA_509_FileSetArchetypeDefaultsWithVariables() throws Exception {
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    request.setArchetypeGroupId("archetypeGroupId");
    request.setArchetypeArtifactId("archetypeArtifactId");
    request.setArchetypeVersion("archetypeVersion");
    Properties properties = new Properties();
    properties.setProperty("groupName", "myGroupName");
    properties.setProperty("serviceName", "myServiceName");
    configurator.configureArchetype(request, Boolean.FALSE, properties);
    assertEquals("com.example.myGroupName", request.getGroupId());
    assertEquals("myServiceName", request.getArtifactId());
    assertEquals("1.0-SNAPSHOT", request.getVersion());
    assertEquals("com.example.myGroupName", request.getPackage());
}
Also used : ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties)

Example 15 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGenerationConfiguratorTest method testOldArchetypeGeneratedFieldsDefaultsMissingGroupId.

// TODO: should test this in interactive mode to check for prompting
public void testOldArchetypeGeneratedFieldsDefaultsMissingGroupId() throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype, ArchetypeNotDefined, ArchetypeGenerationConfigurationFailure, ArchetypeNotConfigured {
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    request.setArchetypeGroupId("archetypeGroupId");
    request.setArchetypeArtifactId("archetypeArtifactId");
    request.setArchetypeVersion("archetypeVersion");
    Properties properties = new Properties();
    properties.setProperty("artifactId", "preset-artifactId");
    try {
        configurator.configureArchetype(request, Boolean.FALSE, properties);
        fail("An exception must be thrown");
    } catch (ArchetypeNotConfigured e) {
    }
}
Also used : ArchetypeNotConfigured(org.apache.maven.archetype.exception.ArchetypeNotConfigured) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties)

Aggregations

ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)33 File (java.io.File)19 Model (org.apache.maven.model.Model)14 Properties (java.util.Properties)13 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)10 Archetype (org.apache.maven.archetype.catalog.Archetype)8 DefaultProjectBuildingRequest (org.apache.maven.project.DefaultProjectBuildingRequest)7 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)7 MavenRepositorySystemSession (org.apache.maven.repository.internal.MavenRepositorySystemSession)7 SimpleLocalRepositoryManager (org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager)7 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)6 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)5 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)5 MavenArtifactRepository (org.apache.maven.artifact.repository.MavenArtifactRepository)5 MockControl (org.easymock.MockControl)5 FileWriter (java.io.FileWriter)3 Writer (java.io.Writer)3 ArchetypeCreationRequest (org.apache.maven.archetype.ArchetypeCreationRequest)3 ArchetypeCreationResult (org.apache.maven.archetype.ArchetypeCreationResult)3 ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)3