Search in sources :

Example 21 with ArchetypeGenerationRequest

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

the class IntegrationTestMojo method setupParentProjects.

private File setupParentProjects(File configFolder, File buildFolder) throws IOException, MojoExecutionException, IntegrationTestFailure {
    // look for 'archetype.pom.properties'
    File archetypePomPropertiesFile = new File(configFolder, "archetype.pom.properties");
    if (!archetypePomPropertiesFile.exists()) {
        getLog().debug("No 'archetype.pom.properties' file found in " + configFolder);
        return buildFolder;
    }
    // go up to the parent configuration folder
    buildFolder = setupParentProjects(configFolder.getParentFile(), buildFolder);
    Properties archetypePomProperties = loadProperties(archetypePomPropertiesFile);
    String groupId = archetypePomProperties.getProperty(Constants.GROUP_ID);
    if (StringUtils.isEmpty(groupId)) {
        throw new MojoExecutionException("Property " + Constants.GROUP_ID + " not set in " + archetypePomPropertiesFile);
    }
    String artifactId = archetypePomProperties.getProperty(Constants.ARTIFACT_ID);
    if (StringUtils.isEmpty(artifactId)) {
        throw new MojoExecutionException("Property " + Constants.ARTIFACT_ID + " not set in " + archetypePomPropertiesFile);
    }
    String version = archetypePomProperties.getProperty(Constants.VERSION);
    if (StringUtils.isEmpty(version)) {
        throw new MojoExecutionException("Property " + Constants.VERSION + " not set in " + archetypePomPropertiesFile);
    }
    File archetypeFile;
    try {
        archetypeFile = getArchetypeFile(groupId, artifactId, version);
    } catch (ArtifactResolverException e) {
        throw new MojoExecutionException("Could not resolve archetype artifact ", e);
    }
    Properties archetypeProperties = getProperties(archetypePomPropertiesFile);
    getLog().info("Setting up parent project in " + buildFolder);
    ArchetypeGenerationRequest request = generate(groupId, artifactId, version, archetypeFile, archetypeProperties, buildFolder.toString());
    return new File(buildFolder, request.getArtifactId());
}
Also used : ArtifactResolverException(org.apache.maven.shared.artifact.resolve.ArtifactResolverException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties) File(java.io.File)

Example 22 with ArchetypeGenerationRequest

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

the class IntegrationTestMojo method generate.

private ArchetypeGenerationRequest generate(String archetypeGroupId, String archetypeArtifactId, String archetypeVersion, File archetypeFile, Properties properties, String basedir) throws IntegrationTestFailure {
    // @formatter:off
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest().setArchetypeGroupId(archetypeGroupId).setArchetypeArtifactId(archetypeArtifactId).setArchetypeVersion(archetypeVersion).setGroupId(properties.getProperty(Constants.GROUP_ID)).setArtifactId(properties.getProperty(Constants.ARTIFACT_ID)).setVersion(properties.getProperty(Constants.VERSION)).setPackage(properties.getProperty(Constants.PACKAGE)).setOutputDirectory(basedir).setProperties(properties);
    // @formatter:on
    ArchetypeGenerationResult result = new ArchetypeGenerationResult();
    archetypeGenerator.generateArchetype(request, archetypeFile, result);
    if (result.getCause() != null) {
        if (result.getCause() instanceof ArchetypeNotConfigured) {
            ArchetypeNotConfigured anc = (ArchetypeNotConfigured) result.getCause();
            throw new IntegrationTestFailure("Missing required properties in archetype.properties: " + StringUtils.join(anc.getMissingProperties().iterator(), ", "), anc);
        }
        throw new IntegrationTestFailure(result.getCause().getMessage(), result.getCause());
    }
    return request;
}
Also used : ArchetypeNotConfigured(org.apache.maven.archetype.exception.ArchetypeNotConfigured) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult)

Example 23 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGeneratorTest method createArchetypeGenerationRequest.

private ArchetypeGenerationRequest createArchetypeGenerationRequest(String project, Archetype archetype) {
    outputDirectory = getBasedir() + "/target/test-classes/projects/" + project;
    projectDirectory = new File(outputDirectory, "file-value");
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    request.setLocalRepository(localRepository);
    request.setArchetypeRepository(remoteRepository);
    request.setOutputDirectory(outputDirectory);
    request.setArchetypeGroupId(archetype.groupId);
    request.setArchetypeArtifactId(archetype.artifactId);
    request.setArchetypeVersion(archetype.version);
    request.setGroupId("file-value");
    request.setArtifactId("file-value");
    request.setVersion("file-value");
    request.setPackage("file.value.package");
    request.setProperties(ADDITIONAL_PROPERTIES);
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
    buildingRequest.setRepositorySession(repositorySession);
    request.setProjectBuildingRequest(buildingRequest);
    return request;
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest)

Example 24 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGeneratorTest method testGenerateArchetypeCompleteWithoutParent.

public void testGenerateArchetypeCompleteWithoutParent() throws Exception {
    System.out.println("testGenerateArchetypeCompleteWithoutParent");
    ArchetypeGenerationRequest request = createArchetypeGenerationRequest("generate-4", ARCHETYPE_BASIC);
    FileUtils.forceDelete(projectDirectory);
    generateProjectFromArchetype(request);
    assertTemplateContent("src/main/java/file/value/package/App.java");
    assertTemplateContent("src/main/java/file/value/package/inner/package/App2.java");
    assertTemplateContent("src/main/c/file/value/package/App.c");
    assertTemplateContent("src/test/java/file/value/package/AppTest.java");
    assertTemplateContent("src/test/c/file/value/package/AppTest.c");
    assertTemplateContent("src/main/resources/App.properties");
    assertTemplateContent("src/main/resources/inner/dir/App2.properties");
    assertTemplateContent("src/main/mdo/App.mdo");
    assertTemplateContent("src/test/resources/AppTest.properties");
    assertTemplateContent("src/test/mdo/AppTest.mdo");
    Model model = readPom(new File(projectDirectory, "pom.xml"));
    assertNull(model.getParent());
    assertEquals("file-value", model.getGroupId());
    assertEquals("file-value", 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 25 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGeneratorTest method testGenerateArchetypePartialOnChild.

public void testGenerateArchetypePartialOnChild() throws Exception {
    System.out.println("testGenerateArchetypePartialOnChild");
    ArchetypeGenerationRequest request = createArchetypeGenerationRequest("generate-8", ARCHETYPE_PARTIAL);
    File parentProjectFile = getProjectFile();
    File parentProjectFileSample = getProjectSampleFile();
    copy(parentProjectFileSample, parentProjectFile);
    File projectFile = new File(projectDirectory, "pom.xml");
    File projectFileSample = new File(projectDirectory, "pom.xml.sample");
    copy(projectFileSample, projectFile);
    FileUtils.forceDelete(new File(projectDirectory, "src"));
    generateProjectFromArchetype(request);
    Model model = readPom(projectFile);
    assertNotNull(model.getParent());
    assertEquals("org.apache.maven.archetype", model.getGroupId());
    assertEquals("file-value", model.getArtifactId());
    assertEquals("1.0-SNAPSHOT", model.getVersion());
    assertTrue(model.getModules().isEmpty());
    assertFalse(model.getDependencies().isEmpty());
    assertFalse(model.getBuild().getPlugins().isEmpty());
    assertFalse(model.getReporting().getPlugins().isEmpty());
}
Also used : ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Model(org.apache.maven.model.Model) File(java.io.File)

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