Search in sources :

Example 1 with ArchetypeGenerationRequest

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

the class NewMavenJSFModuleProjectProvider method createArchetypeProject.

protected IPath createArchetypeProject(NewLiferayJSFModuleProjectOp op, IProgressMonitor monitor) throws CoreException {
    IPath projectLocation = null;
    String javaPackage = "com.example";
    String projectName = op.getProjectName().content();
    IPath location = PathBridge.create(op.getLocation().content());
    if (location.lastSegment().equals(projectName)) {
        // use parent dir since maven archetype will generate new dir under this
        // location
        location = location.removeLastSegments(1);
    }
    String groupId = op.getProjectName().content();
    String artifactId = op.getProjectName().content();
    String version = "1.0.0";
    String archetypeArtifactId = op.getArchetype().content();
    Archetype archetype = new Archetype();
    String[] gav = archetypeArtifactId.split(":");
    String archetypeVersion = gav[gav.length - 1];
    archetype.setGroupId(gav[0]);
    archetype.setArtifactId(gav[1]);
    archetype.setVersion(archetypeVersion);
    Artifact artifact = AetherUtil.getLatestAvailableArtifact(archetypeArtifactId);
    Properties properties = new Properties();
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    if (location == null) {
        location = workspaceRoot.getLocation();
    }
    try {
        MavenPluginActivator pluginActivator = MavenPluginActivator.getDefault();
        ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
        request.setTransferListener(pluginActivator.getMaven().createTransferListener(monitor));
        request.setArchetypeGroupId(artifact.getGroupId());
        request.setArchetypeArtifactId(artifact.getArtifactId());
        request.setArchetypeVersion(artifact.getVersion());
        request.setArchetypeRepository(AetherUtil.newCentralRepository().getUrl());
        request.setGroupId(groupId);
        request.setArtifactId(artifactId);
        request.setVersion(version);
        request.setPackage(javaPackage);
        // the model does not have a package field
        request.setLocalRepository(pluginActivator.getMaven().getLocalRepository());
        request.setRemoteArtifactRepositories(pluginActivator.getMaven().getArtifactRepositories(true));
        request.setProperties(properties);
        request.setOutputDirectory(location.toPortableString());
        ArchetypeGenerationResult result = _getArchetyper().generateProjectFromArchetype(request);
        Exception cause = result.getCause();
        if (cause != null) {
            throw new CoreException(LiferayCore.createErrorStatus("Unable to create project from archetype."));
        }
        projectLocation = location.append(artifactId);
        if (FileUtil.notExists(projectLocation)) {
            throw new CoreException(LiferayCore.createErrorStatus("Can't create gradle JSF project. "));
        }
    } catch (Exception e) {
        throw new CoreException(LiferayCore.createErrorStatus("Failed to create JSF project. ", e));
    }
    return projectLocation;
}
Also used : MavenPluginActivator(org.eclipse.m2e.core.internal.MavenPluginActivator) IPath(org.eclipse.core.runtime.IPath) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) Archetype(org.apache.maven.archetype.catalog.Archetype) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) Properties(java.util.Properties) Artifact(org.eclipse.aether.artifact.Artifact) CoreException(org.eclipse.core.runtime.CoreException) JSONException(org.json.JSONException)

Example 2 with ArchetypeGenerationRequest

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

the class DefaultArchetypeGenerationConfiguratorTest method testOldArchetypeGeneratedFieldsDefaultsBatchMode.

public void testOldArchetypeGeneratedFieldsDefaultsBatchMode() 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("groupId", "preset-groupId");
    properties.setProperty("artifactId", "preset-artifactId");
    configurator.configureArchetype(request, Boolean.FALSE, properties);
    assertEquals("preset-groupId", request.getGroupId());
    assertEquals("preset-artifactId", request.getArtifactId());
    assertEquals("1.0-SNAPSHOT", request.getVersion());
    assertEquals("preset-groupId", request.getPackage());
}
Also used : ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties)

Example 3 with ArchetypeGenerationRequest

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

the class DefaultArchetypeSelectorTest method testArchetypeNotInRequestDefaults.

public void testArchetypeNotInRequestDefaults() throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype, ArchetypeNotDefined {
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    MockControl control = MockControl.createControl(ArchetypeSelectionQueryer.class);
    ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
    queryer.selectArchetype(Collections.<String, List<Archetype>>emptyMap(), createDefaultArchetypeDefinition());
    control.setMatcher(createArgumentMatcher());
    Archetype archetype = new Archetype();
    archetype.setArtifactId("set-artifactId");
    archetype.setGroupId("set-groupId");
    archetype.setVersion("set-version");
    control.setReturnValue(archetype);
    control.replay();
    selector.setArchetypeSelectionQueryer(queryer);
    selector.selectArchetype(request, Boolean.TRUE, "");
    control.verify();
    assertEquals("set-groupId", request.getArchetypeGroupId());
    assertEquals("set-artifactId", request.getArchetypeArtifactId());
    assertEquals("set-version", request.getArchetypeVersion());
}
Also used : MockControl(org.easymock.MockControl) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest)

Example 4 with ArchetypeGenerationRequest

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

the class DefaultArchetypeSelectorTest method testArchetypeArtifactIdInRequest.

public void testArchetypeArtifactIdInRequest() throws PrompterException, IOException, UnknownGroup, ArchetypeSelectionFailure, UnknownArchetype, ArchetypeNotDefined {
    ArchetypeGenerationRequest request = new ArchetypeGenerationRequest();
    request.setArchetypeArtifactId("preset-artifactId");
    MockControl control = MockControl.createControl(ArchetypeSelectionQueryer.class);
    ArchetypeSelectionQueryer queryer = (ArchetypeSelectionQueryer) control.getMock();
    // expect it to not be called
    control.replay();
    selector.setArchetypeSelectionQueryer(queryer);
    selector.selectArchetype(request, Boolean.TRUE, "");
    control.verify();
    assertEquals(DefaultArchetypeSelector.DEFAULT_ARCHETYPE_GROUPID, request.getArchetypeGroupId());
    assertEquals("preset-artifactId", request.getArchetypeArtifactId());
    assertEquals(DefaultArchetypeSelector.DEFAULT_ARCHETYPE_VERSION, request.getArchetypeVersion());
}
Also used : MockControl(org.easymock.MockControl) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest)

Example 5 with ArchetypeGenerationRequest

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

the class IntegrationTestMojo method processIntegrationTest.

private void processIntegrationTest(File goalFile, File archetypeFile) throws IntegrationTestFailure, MojoExecutionException {
    getLog().info("Processing Archetype IT project: " + goalFile.getParentFile().getName());
    try {
        Properties properties = getProperties(goalFile);
        File basedir = new File(goalFile.getParentFile(), "project");
        FileUtils.deleteDirectory(basedir);
        FileUtils.mkdir(basedir.toString());
        basedir = setupParentProjects(goalFile.getParentFile().getParentFile(), basedir);
        ArchetypeGenerationRequest request = generate(project.getGroupId(), project.getArtifactId(), project.getVersion(), archetypeFile, properties, basedir.toString());
        File reference = new File(goalFile.getParentFile(), "reference");
        if (reference.exists()) {
            // compare generated project with reference
            getLog().info("Comparing generated project with reference content: " + reference);
            assertDirectoryEquals(reference, new File(basedir, request.getArtifactId()));
        }
        String goals = FileUtils.fileRead(goalFile);
        if (StringUtils.isNotEmpty(goals)) {
            invokePostArchetypeGenerationGoals(goals.trim(), new File(basedir, request.getArtifactId()), goalFile);
        }
    } catch (IOException ioe) {
        throw new IntegrationTestFailure(ioe);
    }
}
Also used : ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) IOException(java.io.IOException) Properties(java.util.Properties) 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