Search in sources :

Example 26 with Archetype

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

the class DefaultArchetypeSelectionQueryerTest method testDefaultArchetypeNotInMap.

public void testDefaultArchetypeNotInMap() throws PrompterException {
    Map<String, List<Archetype>> map = createDefaultArchetypeCatalog();
    MockControl control = MockControl.createControl(Prompter.class);
    Prompter prompter = (Prompter) control.getMock();
    prompter.prompt("");
    control.setMatcher(createArgumentMatcher());
    control.setReturnValue("1");
    queryer.setPrompter(prompter);
    control.replay();
    ArchetypeDefinition defaultDefinition = new ArchetypeDefinition();
    defaultDefinition.setGroupId("invalid-groupId");
    defaultDefinition.setArtifactId("invalid-artifactId");
    defaultDefinition.setVersion("invalid-version");
    Archetype archetype = queryer.selectArchetype(map, defaultDefinition);
    control.verify();
    assertEquals("set-groupId", archetype.getGroupId());
    assertEquals("set-artifactId", archetype.getArtifactId());
    assertEquals("set-version", archetype.getVersion());
}
Also used : Prompter(org.codehaus.plexus.components.interactivity.Prompter) ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) MockControl(org.easymock.MockControl) Archetype(org.apache.maven.archetype.catalog.Archetype) List(java.util.List) ArrayList(java.util.ArrayList)

Example 27 with Archetype

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

the class DefaultArchetypeSelectorTest method testArchetypeArtifactIdNotInRequest.

public void testArchetypeArtifactIdNotInRequest() 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(), new ArchetypeDefinition());
    control.setMatcher(MockControl.ALWAYS_MATCHER);
    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 : ArchetypeDefinition(org.apache.maven.archetype.ui.ArchetypeDefinition) 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 28 with Archetype

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

the class ArchetypeSelectorUtils method getFilteredArchetypesByCatalog.

/**
 * apply some filtering on archetypes.
 * currently only on artifactId contains filter
 *
 * @param archetypesPerCatalog
 * @return
 */
public static Map<String, List<Archetype>> getFilteredArchetypesByCatalog(Map<String, List<Archetype>> archetypesPerCatalog, String filter) {
    if (archetypesPerCatalog == null || archetypesPerCatalog.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, List<Archetype>> filtered = new LinkedHashMap<String, List<Archetype>>(archetypesPerCatalog.size());
    for (Map.Entry<String, List<Archetype>> entry : archetypesPerCatalog.entrySet()) {
        List<Archetype> archetypes = new ArrayList<Archetype>();
        for (Archetype archetype : entry.getValue()) {
            String groupId = ArchetypeSelectorUtils.extractGroupIdFromFilter(filter);
            String artifactId = ArchetypeSelectorUtils.extractArtifactIdFromFilter(filter);
            if (((groupId == null) || StringUtils.contains(archetype.getGroupId(), groupId)) && StringUtils.contains(archetype.getArtifactId(), artifactId)) {
                archetypes.add(archetype);
            }
        }
        if (!archetypes.isEmpty()) {
            filtered.put(entry.getKey(), archetypes);
        }
    }
    return filtered;
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 29 with Archetype

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

the class ArchetyperRoundtripWithProxyIT method testArchetyper.

public void testArchetyper() throws Exception {
    ArchetypeManager archetype = (ArchetypeManager) lookup(ArchetypeManager.ROLE);
    ProjectBuilder projectBuilder = lookup(ProjectBuilder.class);
    ArtifactRepository localRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "local").toURI().toURL().toExternalForm(), "local-repo");
    ArtifactRepository centralRepository = createRepository(new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "repositories" + File.separator + "central").toURI().toURL().toExternalForm(), "central-repo");
    // (1) create a project from scratch
    // (2) create an archetype from the project
    // (3) create our own archetype catalog properties in memory
    // (4) create our own archetype catalog describing the archetype we just created
    // (5) deploy the archetype we just created
    // (6) create a project form the archetype we just created
    // ------------------------------------------------------------------------
    // 
    // ------------------------------------------------------------------------
    // (1) create a project from scratch
    // File sourceProject = new File( getBasedir(  ), "target/test-classes/projects/roundtrip-1-project" );
    File workingProject = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-2-project");
    FileUtils.forceDelete(new File(workingProject, "target"));
    // (2) create an archetype from the project
    File pom = new File(workingProject, "pom.xml");
    ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepository.getBasedir()));
    buildingRequest.setRepositorySession(repositorySession);
    MavenProject project = projectBuilder.build(pom, buildingRequest).getProject();
    ArchetypeCreationRequest acr = new ArchetypeCreationRequest().setProject(project).setLocalRepository(localRepository).setPostPhase("package");
    ArchetypeCreationResult creationResult = archetype.createArchetypeFromProject(acr);
    if (creationResult.getCause() != null) {
        throw creationResult.getCause();
    }
    // (3) create our own archetype catalog properties in memory
    File catalogDirectory = new File(getBasedir(), "target" + File.separator + "catalog");
    File catalogFile = new File(catalogDirectory, "archetype-catalog.xml");
    File catalogProperties = new File(catalogDirectory, "archetype-catalog.properties");
    catalogDirectory.mkdirs();
    Properties p = new Properties();
    p.setProperty("sources", "catalog");
    p.setProperty("catalog.file", catalogFile.getAbsolutePath());
    OutputStream os = new FileOutputStream(catalogProperties);
    p.store(os, "Generated catalog properties");
    // (5) install the archetype we just created
    File generatedArchetypeDirectory = new File(project.getBasedir(), "target" + File.separator + "generated-sources" + File.separator + "archetype");
    File generatedArchetypePom = new File(generatedArchetypeDirectory, "pom.xml");
    MavenProject generatedArchetypeProject = projectBuilder.build(generatedArchetypePom, buildingRequest).getProject();
    ModelInterpolator modelInterpolator = (ModelInterpolator) lookup(ModelInterpolator.ROLE);
    Model generatedArchetypeModel = modelInterpolator.interpolate(generatedArchetypeProject.getModel(), generatedArchetypePom.getParentFile(), new DefaultProjectBuilderConfiguration(), true);
    File archetypeDirectory = new File(generatedArchetypeDirectory, "src" + File.separator + "main" + File.separator + "resources");
    File archetypeArchive = archetype.archiveArchetype(archetypeDirectory, new File(generatedArchetypeModel.getBuild().getDirectory()), generatedArchetypeModel.getBuild().getFinalName());
    String baseName = StringUtils.replace(generatedArchetypeProject.getGroupId(), ".", File.separator) + File.separator + generatedArchetypeProject.getArtifactId() + File.separator + generatedArchetypeProject.getVersion() + File.separator + generatedArchetypeModel.getBuild().getFinalName();
    File archetypeInRepository = new File(centralRepository.getBasedir(), baseName + ".jar");
    File archetypePomInRepository = new File(centralRepository.getBasedir(), baseName + ".pom");
    archetypeInRepository.getParentFile().mkdirs();
    FileUtils.copyFile(archetypeArchive, archetypeInRepository);
    FileUtils.copyFile(generatedArchetypePom, archetypePomInRepository);
    // (4) create our own archetype catalog describing the archetype we just created
    ArchetypeCatalog catalog = new ArchetypeCatalog();
    Archetype generatedArchetype = new Archetype();
    generatedArchetype.setGroupId(generatedArchetypeProject.getGroupId());
    generatedArchetype.setArtifactId(generatedArchetypeProject.getArtifactId());
    generatedArchetype.setVersion(generatedArchetypeProject.getVersion());
    generatedArchetype.setRepository("http://localhost:" + port + "/repo");
    catalog.addArchetype(generatedArchetype);
    ArchetypeCatalogXpp3Writer catalogWriter = new ArchetypeCatalogXpp3Writer();
    Writer writer = new FileWriter(catalogFile);
    catalogWriter.write(writer, catalog);
    IOUtils.closeQuietly(writer);
    // (6) create a project form the archetype we just created
    String outputDirectory = new File(getBasedir(), "target" + File.separator + "test-classes" + File.separator + "projects" + File.separator + "roundtrip-2-recreatedproject").getAbsolutePath();
    FileUtils.forceDelete(outputDirectory);
    // WagonManager manager = (WagonManager) lookup( WagonManager.class.getName() );
    // manager.addProxy( "http", "localhost", proxyPort, null, null, null );
    ArchetypeGenerationRequest agr = new ArchetypeGenerationRequest().setArchetypeGroupId(generatedArchetypeProject.getGroupId()).setArchetypeArtifactId(generatedArchetypeProject.getArtifactId()).setArchetypeVersion(generatedArchetypeProject.getVersion()).setGroupId("com.mycompany").setArtifactId("myapp").setVersion("1.0-SNAPSHOT").setPackage("com.mycompany.myapp").setOutputDirectory(outputDirectory).setLocalRepository(localRepository).setArchetypeRepository("http://localhost:" + port + "/repo").setProjectBuildingRequest(buildingRequest);
    ArchetypeGenerationResult generationResult = archetype.generateProjectFromArchetype(agr);
    if (generationResult.getCause() != null) {
        throw generationResult.getCause();
    }
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer) FileWriter(java.io.FileWriter) ArchetypeGenerationRequest(org.apache.maven.archetype.ArchetypeGenerationRequest) Properties(java.util.Properties) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) MavenProject(org.apache.maven.project.MavenProject) ArchetypeCreationRequest(org.apache.maven.archetype.ArchetypeCreationRequest) ModelInterpolator(org.apache.maven.project.interpolation.ModelInterpolator) DefaultProjectBuilderConfiguration(org.apache.maven.project.DefaultProjectBuilderConfiguration) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) ArchetypeCreationResult(org.apache.maven.archetype.ArchetypeCreationResult) ArchetypeManager(org.apache.maven.archetype.ArchetypeManager) ProjectBuilder(org.apache.maven.project.ProjectBuilder) FileOutputStream(java.io.FileOutputStream) Model(org.apache.maven.model.Model) ArchetypeGenerationResult(org.apache.maven.archetype.ArchetypeGenerationResult) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) File(java.io.File) DefaultProjectBuildingRequest(org.apache.maven.project.DefaultProjectBuildingRequest) FileWriter(java.io.FileWriter) Writer(java.io.Writer) ArchetypeCatalogXpp3Writer(org.apache.maven.archetype.catalog.io.xpp3.ArchetypeCatalogXpp3Writer)

Example 30 with Archetype

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

the class LocalCatalogArchetypeDataSource method updateCatalog.

public void updateCatalog(ProjectBuildingRequest buildingRequest, Archetype archetype) throws ArchetypeDataSourceException {
    File localRepo = repositoryManager.getLocalRepositoryBasedir(buildingRequest);
    File catalogFile = new File(localRepo, ARCHETYPE_CATALOG_FILENAME);
    getLogger().debug("Using catalog " + catalogFile.getAbsolutePath());
    ArchetypeCatalog catalog;
    if (catalogFile.exists()) {
        try {
            getLogger().debug("Reading the catalog " + catalogFile);
            catalog = readCatalog(ReaderFactory.newXmlReader(catalogFile));
        } catch (FileNotFoundException ex) {
            getLogger().debug("Catalog file don't exist");
            catalog = new ArchetypeCatalog();
        } catch (IOException e) {
            throw new ArchetypeDataSourceException("Error reading archetype catalog.", e);
        }
    } else {
        getLogger().debug("Catalog file don't exist");
        catalog = new ArchetypeCatalog();
    }
    Iterator<Archetype> archetypes = catalog.getArchetypes().iterator();
    boolean found = false;
    Archetype newArchetype = archetype;
    while (!found && archetypes.hasNext()) {
        Archetype a = archetypes.next();
        if (a.getGroupId().equals(archetype.getGroupId()) && a.getArtifactId().equals(archetype.getArtifactId())) {
            newArchetype = a;
            found = true;
        }
    }
    if (!found) {
        catalog.addArchetype(newArchetype);
    }
    newArchetype.setVersion(archetype.getVersion());
    newArchetype.setRepository(archetype.getRepository());
    newArchetype.setDescription(archetype.getDescription());
    newArchetype.setProperties(archetype.getProperties());
    newArchetype.setGoals(archetype.getGoals());
    writeLocalCatalog(catalog, catalogFile);
}
Also used : Archetype(org.apache.maven.archetype.catalog.Archetype) FileNotFoundException(java.io.FileNotFoundException) ArchetypeCatalog(org.apache.maven.archetype.catalog.ArchetypeCatalog) IOException(java.io.IOException) 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