Search in sources :

Example 16 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project maven-archetype by apache.

the class RemoteCatalogArchetypeDataSourceTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    System.setProperty("org.apache.maven.archetype.repository.directory", getTestPath("target/test-classes/repositories/test-catalog"));
    // Start Jetty
    server = new Server(0);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/repo");
    webapp.setWar("target/wars/archetype-repository.war");
    server.setHandler(webapp);
    server.start();
    port = server.getConnectors()[0].getLocalPort();
    File catalogDirectory = getTestFile("target/test-classes/repositories/test-catalog");
    catalogDirectory.mkdirs();
    getTestFile("target/test-classes/repositories/test-catalog/dummy").createNewFile();
    ArchetypeCatalog catalog = new ArchetypeCatalog();
    Archetype generatedArchetype = new Archetype();
    generatedArchetype.setGroupId("groupId");
    generatedArchetype.setArtifactId("artifactId");
    generatedArchetype.setVersion("1");
    generatedArchetype.setRepository("http://localhost:" + port + "/repo/");
    catalog.addArchetype(generatedArchetype);
    File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
    ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
    Writer writer = new FileWriter(catalogFile);
    catalogWriter.write(writer, catalog);
    IOUtils.closeQuietly(writer);
}
Also used : WebAppContext(org.mortbay.jetty.webapp.WebAppContext) Server(org.mortbay.jetty.Server) Archetype(org.apache.maven.archetype.catalog.Archetype) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) FileWriter(java.io.FileWriter) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)

Example 17 with Archetype

use of org.apache.maven.archetype.catalog.Archetype 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 18 with Archetype

use of org.apache.maven.archetype.catalog.Archetype 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 19 with Archetype

use of org.apache.maven.archetype.catalog.Archetype in project intellij-community by JetBrains.

the class Maven2ServerIndexerImpl method doCollectArchetypes.

private void doCollectArchetypes(String roleHint, Set<MavenArchetype> result) throws RemoteException {
    try {
        ArchetypeDataSource source = myEmbedder.getComponent(ArchetypeDataSource.class, roleHint);
        ArchetypeCatalog catalog = source.getArchetypeCatalog(new Properties());
        for (Archetype each : (Iterable<? extends Archetype>) catalog.getArchetypes()) {
            result.add(Maven2ModelConverter.convertArchetype(each));
        }
    } catch (ArchetypeDataSourceException e) {
        Maven2ServerGlobals.getLogger().warn(e);
    }
}
Also used : MavenArchetype(org.jetbrains.idea.maven.model.MavenArchetype) Archetype(org.apache.maven.archetype.catalog.Archetype) ArchetypeDataSourceException(org.apache.maven.archetype.source.ArchetypeDataSourceException) ArchetypeDataSource(org.apache.maven.archetype.source.ArchetypeDataSource) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog)

Example 20 with Archetype

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

the class NewMavenPluginProjectProvider method createNewProject.

@Override
public IStatus createNewProject(NewLiferayPluginProjectOp op, IProgressMonitor monitor) throws CoreException {
    ElementList<ProjectName> projectNames = op.getProjectNames();
    IStatus retval = null;
    IMavenConfiguration mavenConfiguration = MavenPlugin.getMavenConfiguration();
    IMavenProjectRegistry mavenProjectRegistry = MavenPlugin.getMavenProjectRegistry();
    IProjectConfigurationManager projectConfigurationManager = MavenPlugin.getProjectConfigurationManager();
    String groupId = op.getGroupId().content();
    String artifactId = op.getProjectName().content();
    String version = op.getArtifactVersion().content();
    String javaPackage = op.getGroupId().content();
    String activeProfilesValue = op.getActiveProfilesValue().content();
    IPortletFramework portletFramework = op.getPortletFramework().content(true);
    String frameworkName = NewLiferayPluginProjectOpMethods.getFrameworkName(op);
    IPath location = PathBridge.create(op.getLocation().content());
    if (location.lastSegment().equals(artifactId)) {
        // use parent dir since maven archetype will generate new dir under this
        // location
        location = location.removeLastSegments(1);
    }
    String archetypeArtifactId = op.getArchetype().content(true);
    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);
    ArchetypeManager archetypeManager = MavenPluginActivator.getDefault().getArchetypeManager();
    ArtifactRepository remoteArchetypeRepository = archetypeManager.getArchetypeRepository(archetype);
    Properties properties = new Properties();
    try {
        List<?> archProps = archetypeManager.getRequiredProperties(archetype, remoteArchetypeRepository, monitor);
        if (ListUtil.isNotEmpty(archProps)) {
            for (Object prop : archProps) {
                if (prop instanceof RequiredProperty) {
                    RequiredProperty rProp = (RequiredProperty) prop;
                    Value<PluginType> pluginType = op.getPluginType();
                    if (pluginType.content().equals(PluginType.theme)) {
                        String key = rProp.getKey();
                        if (key.equals("themeParent")) {
                            properties.put(key, op.getThemeParent().content(true));
                        } else if (key.equals("themeType")) {
                            properties.put(key, ThemeUtil.getTemplateExtension(op.getThemeFramework().content(true)));
                        }
                    } else {
                        properties.put(rProp.getKey(), rProp.getDefaultValue());
                    }
                }
            }
        }
    } catch (UnknownArchetype e1) {
        LiferayMavenCore.logError("Unable to find archetype required properties", e1);
    }
    ResolverConfiguration resolverConfig = new ResolverConfiguration();
    if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
        resolverConfig.setSelectedProfiles(activeProfilesValue);
    }
    ProjectImportConfiguration configuration = new ProjectImportConfiguration(resolverConfig);
    List<IProject> newProjects = projectConfigurationManager.createArchetypeProjects(location, archetype, groupId, artifactId, version, javaPackage, properties, configuration, monitor);
    if (ListUtil.isNotEmpty(newProjects)) {
        op.setImportProjectStatus(true);
        for (IProject project : newProjects) {
            projectNames.insert().setName(project.getName());
        }
    }
    if (ListUtil.isEmpty(newProjects)) {
        retval = LiferayMavenCore.createErrorStatus("New project was not created due to unknown error");
    } else {
        IProject firstProject = newProjects.get(0);
        if (!CoreUtil.isNullOrEmpty(activeProfilesValue)) {
            String[] activeProfiles = activeProfilesValue.split(",");
            // find all profiles that should go in user settings file
            List<NewLiferayProfile> newUserSettingsProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.userSettings);
            if (ListUtil.isNotEmpty(newUserSettingsProfiles)) {
                String userSettingsFile = mavenConfiguration.getUserSettingsFile();
                String userSettingsPath = null;
                if (CoreUtil.isNullOrEmpty(userSettingsFile)) {
                    userSettingsPath = SettingsXmlConfigurationProcessor.DEFAULT_USER_SETTINGS_FILE.getAbsolutePath();
                } else {
                    userSettingsPath = userSettingsFile;
                }
                try {
                    // backup user's settings.xml file
                    File settingsXmlFile = new File(userSettingsPath);
                    File backupFile = _getBackupFile(settingsXmlFile);
                    FileUtils.copyFile(settingsXmlFile, backupFile);
                    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                    Document pomDocument = docBuilder.parse(settingsXmlFile.getCanonicalPath());
                    for (NewLiferayProfile newProfile : newUserSettingsProfiles) {
                        MavenUtil.createNewLiferayProfileNode(pomDocument, newProfile);
                    }
                    TransformerFactory transformerFactory = TransformerFactory.newInstance();
                    Transformer transformer = transformerFactory.newTransformer();
                    DOMSource source = new DOMSource(pomDocument);
                    StreamResult result = new StreamResult(settingsXmlFile);
                    transformer.transform(source, result);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to save new Liferay profile to user settings.xml.", e);
                }
            }
            // find all profiles that should go in the project pom
            List<NewLiferayProfile> newProjectPomProfiles = getNewProfilesToSave(activeProfiles, op.getNewLiferayProfiles(), ProfileLocation.projectPom);
            // only need to set the first project as nested projects should pickup the
            // parent setting
            IMavenProjectFacade newMavenProject = mavenProjectRegistry.getProject(firstProject);
            IFile pomFile = newMavenProject.getPom();
            IDOMModel domModel = null;
            try {
                domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(pomFile);
                for (NewLiferayProfile newProfile : newProjectPomProfiles) {
                    MavenUtil.createNewLiferayProfileNode(domModel.getDocument(), newProfile);
                }
                domModel.save();
            } catch (IOException ioe) {
                LiferayMavenCore.logError("Unable to save new Liferay profiles to project pom.", ioe);
            } finally {
                if (domModel != null) {
                    domModel.releaseFromEdit();
                }
            }
            for (IProject project : newProjects) {
                try {
                    projectConfigurationManager.updateProjectConfiguration(new MavenUpdateRequest(project, mavenConfiguration.isOffline(), true), monitor);
                } catch (Exception e) {
                    LiferayMavenCore.logError("Unable to update configuration for " + project.getName(), e);
                }
            }
            String pluginVersion = getNewLiferayProfilesPluginVersion(activeProfiles, op.getNewLiferayProfiles(), archetypeVersion);
            String archVersion = MavenUtil.getMajorMinorVersionOnly(archetypeVersion);
            updateDtdVersion(firstProject, pluginVersion, archVersion);
        }
        Value<PluginType> pluginType = op.getPluginType();
        if (pluginType.content().equals(PluginType.portlet)) {
            String portletName = op.getPortletName().content(false);
            retval = portletFramework.postProjectCreated(firstProject, frameworkName, portletName, monitor);
        }
    }
    if (retval == null) {
        retval = Status.OK_STATUS;
    }
    return retval;
}
Also used : ResolverConfiguration(org.eclipse.m2e.core.project.ResolverConfiguration) MavenUpdateRequest(org.eclipse.m2e.core.project.MavenUpdateRequest) IStatus(org.eclipse.core.runtime.IStatus) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) IFile(org.eclipse.core.resources.IFile) ProjectName(com.liferay.ide.project.core.model.ProjectName) Archetype(org.apache.maven.archetype.catalog.Archetype) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NewLiferayProfile(com.liferay.ide.project.core.model.NewLiferayProfile) IProjectConfigurationManager(org.eclipse.m2e.core.project.IProjectConfigurationManager) IMavenProjectRegistry(org.eclipse.m2e.core.project.IMavenProjectRegistry) Properties(java.util.Properties) Document(org.w3c.dom.Document) IMavenConfiguration(org.eclipse.m2e.core.embedder.IMavenConfiguration) RequiredProperty(org.apache.maven.archetype.metadata.RequiredProperty) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) TransformerFactory(javax.xml.transform.TransformerFactory) IPath(org.eclipse.core.runtime.IPath) StreamResult(javax.xml.transform.stream.StreamResult) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) IOException(java.io.IOException) PluginType(com.liferay.ide.project.core.model.PluginType) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ArchetypeManager(org.eclipse.m2e.core.internal.archetype.ArchetypeManager) IPortletFramework(com.liferay.ide.project.core.IPortletFramework) ProjectImportConfiguration(org.eclipse.m2e.core.project.ProjectImportConfiguration) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UnknownArchetype(org.apache.maven.archetype.exception.UnknownArchetype) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

Archetype (org.apache.maven.archetype.catalog.Archetype)30 ArchetypeCatalog (org.apache.maven.archetype.catalog.ArchetypeCatalog)12 File (java.io.File)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)8 Properties (java.util.Properties)7 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)7 MockControl (org.easymock.MockControl)7 ArchetypeCatalogXpp3Writer (org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)6 UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)6 FileWriter (java.io.FileWriter)5 Writer (java.io.Writer)5 Map (java.util.Map)5 ArchetypeGenerationResult (org.apache.maven.archetype.ArchetypeGenerationResult)5 ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)5 Prompter (org.codehaus.plexus.components.interactivity.Prompter)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 ArchetypeManager (org.apache.maven.archetype.ArchetypeManager)4